Example #1
0
def kotti_migrate_command():
    __doc__ = """Migrate Kotti and Kotti add-ons.

    Usage:
      kotti-migrate <config_uri> list_all
      kotti-migrate <config_uri> upgrade [--scripts=<location>] [--rev=<rev>]
      kotti-migrate <config_uri> upgrade_all
      kotti-migrate <config_uri> stamp_head [--scripts=<location>] [--rev=<rev>]

    o 'list_all' prints a list of all available migrations of Kotti
      and registered add-ons.

    o 'upgrade' will run Kotti's upgrades to upgrade the database to
    the latest version.

      Use '--scripts=kotti_myaddon:alembic' to run the upgrades of the
      'kotti_myaddon' package instead.

    o 'upgrade_all' will run all upgrades of all packages registered
      with Kotti.

    o 'stamp_head' allows you to manually set the stamped version to
      the latest version inside the 'kotti_alembic_version' table,
      that is, without actually running any migrations.

      You may use this command for a different package by using the
      '--scripts' option.

    Options:
      -h --help     Show this screen.
    """
    # We need to turn off populators and root_factory when we run
    # migrations, because they would access the database, which may
    # not be possible prior to the migration.
    #
    # Unfortunately, we're not able to just set the 'kotti.populators'
    # setting to an empty list.  Since add-ons might add to this list
    # again later, when we call 'bootstrap' (and thus their
    # 'includeme' function).
    save_conf_defaults = conf_defaults.copy()

    os.environ['KOTTI_DISABLE_POPULATORS'] = '1'
    conf_defaults['kotti.root_factory'] = [lambda req: None]

    def callback(arguments):
        args = ()
        args_with_location = (arguments['--scripts'] or DEFAULT_LOCATION,)
        if arguments['list_all']:
            func = list_all
        elif arguments['upgrade']:
            func = upgrade
            args = args_with_location + (arguments['--rev'],)
        elif arguments['upgrade_all']:
            func = upgrade_all
        elif arguments['stamp_head']:
            func = stamp_head
            args = args_with_location + (arguments['--rev'],)
        func(*args)

    try:
        return command(callback, __doc__)
    finally:
        conf_defaults.clear()
        conf_defaults.update(save_conf_defaults)
        del os.environ['KOTTI_DISABLE_POPULATORS']
Example #2
0
def kotti_migrate_command():
    __doc__ = """Migrate Kotti and Kotti add-ons.

    Usage:
      kotti-migrate <config_uri> list_all
      kotti-migrate <config_uri> upgrade [--scripts=<location>] [--rev=<rev>]
      kotti-migrate <config_uri> upgrade_all
      kotti-migrate <config_uri> stamp_head [--scripts=<location>] [--rev=<rev>]

    o 'list_all' prints a list of all available migrations of Kotti
      and registered add-ons.

    o 'upgrade' will run Kotti's upgrades to upgrade the database to
    the latest version.

      Use '--scripts=kotti_myaddon:alembic' to run the upgrades of the
      'kotti_myaddon' package instead.

    o 'upgrade_all' will run all upgrades of all packages registered
      with Kotti.

    o 'stamp_head' allows you to manually set the stamped version to
      the latest version inside the 'kotti_alembic_version' table,
      that is, without actually running any migrations.

      You may use this command for a different package by using the
      '--scripts' option.

    Options:
      -h --help     Show this screen.
    """
    # We need to turn off populators and root_factory when we run
    # migrations, because they would access the database, which may
    # not be possible prior to the migration.
    #
    # Unfortunately, we're not able to just set the 'kotti.populators'
    # setting to an empty list.  Since add-ons might add to this list
    # again later, when we call 'bootstrap' (and thus their
    # 'includeme' function).
    save_conf_defaults = conf_defaults.copy()

    os.environ["KOTTI_DISABLE_POPULATORS"] = "1"
    conf_defaults["kotti.root_factory"] = [lambda req: None]

    def callback(arguments):
        args = ()
        args_with_location = (arguments["--scripts"] or DEFAULT_LOCATION,)
        if arguments["list_all"]:
            func = list_all
        elif arguments["upgrade"]:
            func = upgrade
            args = args_with_location + (arguments["--rev"],)
        elif arguments["upgrade_all"]:
            func = upgrade_all
        elif arguments["stamp_head"]:
            func = stamp_head
            args = args_with_location + (arguments["--rev"],)
        else:
            raise ValueError("Unknown command")
        func(*args)

    try:
        return command(callback, __doc__)
    finally:
        conf_defaults.clear()
        conf_defaults.update(save_conf_defaults)
        del os.environ["KOTTI_DISABLE_POPULATORS"]