Ejemplo n.º 1
0
def main():
    current_dir = os.path.abspath(os.getcwd())
    module_path = pathlib.Path(__file__).parent
    os.chdir(current_dir)

    cmd = CommandLine()
    cmd.parser.add_argument('--app-config',
                            dest='db_config',
                            required=True,
                            help='service configuration file path')
    cmd_options = cmd.parser.parse_args()

    config = ConfigParser()
    config.read(cmd_options.db_config)
    db_url = config.get(section='db', option='url')
    if not db_url:
        cmd.parser.error(
            f'source config {cmd_options.db_config} contains empty "url" in [db] section'
        )

    alembic_ini = Config(file_=str(module_path / 'alembic.ini'),
                         ini_section=cmd_options.name,
                         cmd_opts=cmd_options)
    alembic_ini.set_main_option('script_location',
                                str(module_path / 'alembic'))
    alembic_ini.set_main_option('sqlalchemy.url', db_url)
    exit(cmd.run_cmd(alembic_ini, cmd_options))
def main(graph, Base):
    """
    Entry point for invoking Alembic's `CommandLine`.

    Runs revision --autogenerate to make sure no migration is necessary.

    Alembic's CLI defines its own argument parsing and command invocation; we want
    to use these directly but define configuration our own way. This function takes
    the behavior of `CommandLine.main()` and reinterprets it with our patching.

    :param graph: an initialized object graph

    """
    name = Base.resolve().__name__
    migrations_dir = get_migrations_dir(graph, name)

    cli = CommandLine()
    args = [
        "revision",
        "--autogenerate",
        "-m",
        "'Should be empty.'",
    ]
    options = cli.parser.parse_args(args)

    with patch_script_directory(
            graph=graph,
            Base=Base,
            process_revision_directives=process_revision_directives,
    ) as tmp_script_dir:
        with make_tmp_migrations_dir(migrations_dir) as tmp_migrations_dir:
            config = make_alembic_config(tmp_script_dir,
                                         str(tmp_migrations_dir))
            cli.run_cmd(config, options)
Ejemplo n.º 3
0
def main():
    current_dir = os.path.abspath(os.getcwd())

    try:
        logging.basicConfig(level=logging.DEBUG)
        alembic = CommandLine()
        alembic.parser.add_argument('--db-url',
                                    default=os.getenv('STAFF_DB_URL'),
                                    help='Database URL [env: STAFF_DB_URL]')

        options = alembic.parser.parse_args()

        if not options.db_url:
            alembic.parser.error('--db-url is required')
            exit(127)

        if options.config == 'alembic.ini':
            options.config = os.path.join(MODULE_PATH, options.config)

        cfg = Config(file_=options.config,
                     ini_section=options.name,
                     cmd_opts=options)

        cfg.set_main_option('script_location',
                            str(os.path.join(MODULE_PATH, 'alembic')))
        cfg.set_main_option('sqlalchemy.url', options.db_url)

        if 'cmd' not in options:
            alembic.parser.error('please specify command')
            exit(128)
        else:
            exit(alembic.run_cmd(cfg, options))
    finally:
        os.chdir(current_dir)
Ejemplo n.º 4
0
def main(graph, *args):
    """
    Entry point for invoking Alembic's `CommandLine`.

    Alembic's CLI defines its own argument parsing and command invocation; we want
    to use these directly but define configuration our own way. This function takes
    the behavior of `CommandLine.main()` and reinterprets it with our patching.

    :param graph: an initialized object graph
    :param migration_dir: the path to the migrations directory

    """
    migrations_dir = get_migrations_dir(graph)

    cli = CommandLine()
    options = cli.parser.parse_args(args if args else argv[1:])
    if not hasattr(options, "cmd"):
        cli.parser.error("too few arguments")
    if options.cmd[0].__name__ == "init":
        cli.parser.error(
            "Alembic 'init' command should not be used in the microcosm!")

    with patch_script_directory(graph) as temporary_dir:
        config = make_alembic_config(temporary_dir, migrations_dir)
        cli.run_cmd(config, options)
Ejemplo n.º 5
0
def main() -> None:
    """Setup and run alembic."""

    alembic = CommandLine()
    options = alembic.parser.parse_args()

    if "cmd" not in options:
        alembic.parser.error("too few arguments")
        exit(128)
    else:
        config = make_alembic_config(options)
        exit(alembic.run_cmd(config, options))
Ejemplo n.º 6
0
def main():
    alembic = CommandLine()
    alembic.parser.add_argument(
        "--db-url",
        default=os.getenv("ANALYZER_DB_URL", DEFAULT_PG_URL),
        help="Database URL [env var: ANALYZER_DB_URL]",
    )

    options = alembic.parser.parse_args()
    config = make_alembic_config(options)

    exit(alembic.run_cmd(config, options))
Ejemplo n.º 7
0
def alembic_command():
    logging.basicConfig(level=logging.DEBUG)
    alembic = CommandLine()
    options = alembic.parser.parse_args()
    if 'cmd' not in options:
        alembic.parser.error('too few arguments')
        exit(128)

    config = Config(ALEMBIC_INI)
    config.set_main_option('script_location', str(ALEMBIC_SCRIPT_LOCATION))
    config.set_main_option('sqlalchemy.url',
                           os.getenv('SHORTENER_PG_URL', DEFAULT_PG_URL))
    exit(alembic.run_cmd(config, options))
Ejemplo n.º 8
0
async def migrate(database, extra=""):
    from alembic.config import CommandLine as AlembicCommandLine, Config as AlembicConfig
    from alembic.script import ScriptDirectory

    class Script(ScriptDirectory):
        def run_env(script):
            from alembic import context as alembic_context

            target_metadata = Base.metadata

            def run_migrations_offline():
                alembic_context.configure(url=database.uri,
                                          target_metadata=target_metadata,
                                          literal_binds=True)
                with alembic_context.begin_transaction():
                    alembic_context.run_migrations()

            def run_migrations_online():
                connectable = create_engine(database.uri,
                                            poolclass=pool.NullPool)

                with connectable.connect() as connection:
                    alembic_context.configure(connection=connection,
                                              target_metadata=target_metadata)
                    with alembic_context.begin_transaction():
                        alembic_context.run_migrations()

            if alembic_context.is_offline_mode():
                run_migrations_offline()
            else:
                run_migrations_online()

    def from_config(cfg):
        return Script(database.db_migrations)

    ScriptDirectory.from_config = from_config

    parts = []
    for p in sys.argv:
        if p == "--":
            break
        parts.append(p)

    commandline = AlembicCommandLine(prog=f"{' '.join(parts)} -- ")
    options = commandline.parser.parse_args(shlex.split(extra))
    if not hasattr(options, "cmd"):
        commandline.parser.error("too few arguments after the ' -- '")
    else:
        cfg = AlembicConfig(cmd_opts=options)
        commandline.run_cmd(cfg, options)
Ejemplo n.º 9
0
def migrate(args):
    alembic_command_line = CommandLine()
    options = alembic_command_line.parser.parse_args(args)
    if not hasattr(options, "cmd"):
        # see http://bugs.python.org/issue9253, argparse
        # behavior changed incompatibly in py3.3
        alembic_command_line.parser.error("too few arguments")
    else:
        alembic_ini = Path(__file__).parent / 'alembic.ini'
        cfg = AlembicConfig(
            file_=str(alembic_ini),
            ini_section=options.name,
            cmd_opts=options,
        )
        alembic_command_line.run_cmd(cfg, options)
Ejemplo n.º 10
0
def upgrade() -> None:
    """
    Update the memezer database to the latest version.
    """
    from alembic.config import CommandLine

    CommandLine(prog="alembic").main(argv=["upgrade", "head"])
Ejemplo n.º 11
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    alembic = CommandLine()
    alembic.parser.formatter_class = argparse.ArgumentDefaultsHelpFormatter
    alembic.parser.add_argument('--pg-url',
                                default=os.getenv('ANALYZER_PG_URL',
                                                  DEFAULT_PG_URL),
                                help='Database URL [env var: ANALYZER_PG_URL]')

    options = alembic.parser.parse_args()
    if 'cmd' not in options:
        alembic.parser.error('too few arguments')
        exit(128)
    else:
        config = make_alembic_config(options)
        exit(alembic.run_cmd(config, options))
Ejemplo n.º 12
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    alembic = CommandLine()
    alembic.parser.formatter_class = argparse.ArgumentDefaultsHelpFormatter
    alembic.parser.add_argument(
        "--pg-url",
        default=os.getenv("POSTGRES_URI", POSTGRES_URI),
        help="Database URL [env var: POSTGRES_URI]",
    )

    options = alembic.parser.parse_args()
    if "cmd" not in options:
        alembic.parser.error("too few arguments")
        exit(128)
    else:
        config = make_alembic_config(options)
        exit(alembic.run_cmd(config, options))
Ejemplo n.º 13
0
def main():
    alembic = CommandLine()
    options = alembic.parser.parse_args()

    # Если указан относительный путь (alembic.ini), добавляем в начало
    # абсолютный путь до приложения
    if not os.path.isabs(options.config):
        options.config = os.path.join(PROJECT_PATH, options.config)

    # Создаем объект конфигурации Alembic
    config = Config(file_=options.config,
                    ini_section=options.name,
                    cmd_opts=options)

    # Подменяем путь до папки с alembic на абсолютный (требуется, чтобы alembic
    # мог найти env.py, шаблон для генерации миграций и сами миграции)
    alembic_location = config.get_main_option('script_location')
    if not os.path.isabs(alembic_location):
        config.set_main_option('script_location',
                               os.path.join(PROJECT_PATH, alembic_location))

    # Запускаем команду alembic
    exit(alembic.run_cmd(config, options))
Ejemplo n.º 14
0
def main(graph, *args):
    """
    Entry point for invoking Alembic's `CommandLine`.

    Alembic's CLI defines its own argument parsing and command invocation; we want
    to use these directly but define configuration our own way. This function takes
    the behavior of `CommandLine.main()` and reinterprets it with our patching.

    :param graph: an initialized object graph
    :param migration_dir: the path to the migrations directory

    """
    migrations_dir = get_migrations_dir(graph)

    cli = CommandLine()
    options = cli.parser.parse_args(args if args else argv[1:])
    if not hasattr(options, "cmd"):
        cli.parser.error("too few arguments")
    if options.cmd[0].__name__ == "init":
        cli.parser.error("Alembic 'init' command should not be used in the microcosm!")

    with patch_script_directory(graph) as temporary_dir:
        config = make_alembic_config(temporary_dir, migrations_dir)
        cli.run_cmd(config, options)
Ejemplo n.º 15
0
def alembic(alembic_args):
    from alembic.config import CommandLine
    CommandLine().main(argv=alembic_args)
Ejemplo n.º 16
0
def alembic(*args):
    """
    Frontend for alembic script with additional uber specific facilities.

    "sep alembic" supports all the same arguments as the regular "alembic"
    command, with the addition of the "--plugin PLUGIN_NAME" option.

    Passing "--plugin PLUGIN_NAME" will choose the correct alembic version path
    for the given plugin. If "--plugin" is omitted, it will default to "uber".

    If "--version-path PATH" is also specified, it will override the version
    path chosen for the plugin. This functionality is rarely needed, and best
    left unused.

    If a new migration revision is created for a plugin that previously did
    not have any revisions, then a new branch label is applied using the
    plugin name. For example::

        sep alembic --plugin new_plugin revision --autogenerate -m "Initial migration"

    A new revision script will be created in "new_plugin/alembic/versions/"
    with a branch label of "new_plugin". The "new_plugin/alembic/versions/"
    directory will be created if it does not already exist.
    """
    from alembic.config import CommandLine
    from sideboard.config import config as sideboard_config
    from sideboard.internal.imports import plugin_dirs
    from uber.migration import create_alembic_config, \
        get_plugin_head_revision, version_locations

    argv = args if args else sys.argv[1:]

    # Extract the "--plugin" option from argv.
    plugin_name = 'uber'
    for plugin_opt in ('-p', '--plugin'):
        if plugin_opt in argv:
            plugin_index = argv.index(plugin_opt)
            argv.pop(plugin_index)
            plugin_name = argv.pop(plugin_index)

    assert plugin_name in version_locations, (
        'Plugin "{}" does not exist in {}'.format(
            plugin_name, sideboard_config['plugins_dir']))

    commandline = CommandLine(prog='sep alembic')
    if {'-h', '--help'}.intersection(argv):
        # If "--help" is passed, add a description of the "--plugin" option
        commandline.parser.add_argument(
            '-p',
            '--plugin',
            type=str,
            default='uber',
            help='Name of plugin in which to add new versions')

    options = commandline.parser.parse_args(argv)

    if not hasattr(options, 'cmd'):
        commandline.parser.error('too few arguments')

    kwarg_names = options.cmd[2]
    if 'version_path' in kwarg_names and not options.version_path:
        # If the command supports the "--version-path" option and it was not
        # specified, default to the version path of the given plugin.
        options.version_path = version_locations[plugin_name]

        if 'branch_label' in kwarg_names and options.version_path and \
                not glob(join(options.version_path, '*.py')):
            # If the command supports the "--branch-label" option and there
            # aren't any existing revisions, then always apply the plugin
            # name as the branch label.
            options.branch_label = plugin_name

    if 'head' in kwarg_names and not options.head:
        # If the command supports the "--head" option and it was not specified:
        # -> if we're not creating a new branch for the plugin, then make this
        #    this revision on top of the plugin's branch head
        # -> if we're creating the initial migration for a plugin, automatically
        #    set the head to the uber branch head revision
        if options.branch_label != plugin_name:
            revision = get_plugin_head_revision(plugin_name)
            options.head = revision.revision
            if revision.is_branch_point:
                options.splice = True
        elif not glob(join(options.version_path, '*.py')):
            revision = get_plugin_head_revision('uber')
            options.head = revision.revision

    commandline.run_cmd(create_alembic_config(cmd_opts=options), options)
Ejemplo n.º 17
0
Archivo: manager.py Proyecto: zhuf/june
 def run(self, args):
     prog = '%s %s' % (os.path.basename(sys.argv[0]), sys.argv[1])
     return CommandLine(prog=prog).main(argv=args)
Ejemplo n.º 18
0
def alembic(ctx, alembic_args):
    app = ctx.obj['app']
    with app.test_request_context():
        CommandLine().main(argv=alembic_args)
Ejemplo n.º 19
0
    def __init__(self, *args, **kwargs):
        from alembic.config import CommandLine

        kwargs['parser'] = CommandLine(self.__class__.__name__).parser
        super(Alembic, self).__init__(*args, **kwargs)
Ejemplo n.º 20
0
def alembic(*args):
    """
    Frontend for alembic script with additional uber specific facilities.

    "sep alembic" supports all the same arguments as the regular "alembic"
    command, with the addition of the "--plugin PLUGIN_NAME" option.

    Passing "--plugin PLUGIN_NAME" will choose the correct alembic version path
    for the given plugin. If "--plugin" is omitted, it will default to "uber".

    If "--version-path PATH" is also specified, it will override the version
    path chosen for the plugin. This functionality is rarely needed, and best
    left unused.

    If a new migration revision is created for a plugin that previously did
    not have any revisions, then a new branch label is applied using the
    plugin name. For example::

        sep alembic --plugin new_plugin revision --autogenerate -m "Initial migration"

    A new revision script will be created in "new_plugin/alembic/versions/"
    with a branch label of "new_plugin". The "new_plugin/alembic/versions/"
    directory will be created if it does not already exist.
    """
    from alembic.config import CommandLine
    from sideboard.config import config as sideboard_config
    from uber.migration import create_alembic_config, get_plugin_head_revision, version_locations

    argv = args if args else sys.argv[1:]

    # Extract the "--plugin" option from argv.
    plugin_name = 'uber'
    for plugin_opt in ('-p', '--plugin'):
        if plugin_opt in argv:
            plugin_index = argv.index(plugin_opt)
            argv.pop(plugin_index)
            plugin_name = argv.pop(plugin_index)

    assert plugin_name in version_locations, (
        'Plugin "{}" does not exist in {}'.format(
            plugin_name, sideboard_config['plugins_dir']))

    commandline = CommandLine(prog='sep alembic')
    if {'-h', '--help'}.intersection(argv):
        # If "--help" is passed, add a description of the "--plugin" option
        commandline.parser.add_argument(
            '-p', '--plugin',
            type=str,
            default='uber',
            help='Name of plugin in which to add new versions')

    options = commandline.parser.parse_args(argv)

    if not hasattr(options, 'cmd'):
        commandline.parser.error('too few arguments')

    kwarg_names = options.cmd[2]
    if 'version_path' in kwarg_names and not options.version_path:
        # If the command supports the "--version-path" option and it was not
        # specified, default to the version path of the given plugin.
        options.version_path = version_locations[plugin_name]

        if 'branch_label' in kwarg_names and options.version_path and \
                not glob(join(options.version_path, '*.py')):
            # If the command supports the "--branch-label" option and there
            # aren't any existing revisions, then always apply the plugin
            # name as the branch label.
            options.branch_label = plugin_name

    if 'head' in kwarg_names and not options.head:
        # If the command supports the "--head" option and it was not specified:
        # -> if we're not creating a new branch for the plugin, then make this
        #    this revision on top of the plugin's branch head
        # -> if we're creating the initial migration for a plugin, automatically
        #    set the head to the uber branch head revision
        if options.branch_label != plugin_name:
            revision = get_plugin_head_revision(plugin_name)
            options.head = revision.revision
            if revision.is_branch_point:
                options.splice = True
        elif not glob(join(options.version_path, '*.py')):
            revision = get_plugin_head_revision('uber')
            options.head = revision.revision

    commandline.run_cmd(create_alembic_config(cmd_opts=options), options)
Ejemplo n.º 21
0
def alembic(alembic_args):
    from alembic.config import CommandLine
    logging.getLogger('alembic').setLevel(logging.INFO)
    CommandLine().main(argv=alembic_args)
Ejemplo n.º 22
0
def migrate(downgrade, revision):
    revision = revision or ("-1" if downgrade else "head")
    action = "downgrade" if downgrade else "upgrade"
    CommandLine(prog=None).main(argv=[action, revision])