Ejemplo n.º 1
0
def run_migrations_online():
    """Run migrations in 'online' mode.

  In this scenario we need to create an Engine
  and associate a connection with the context.

  """

    if (isinstance(db.obj, SqliteDatabase) and not "GENMIGRATE" in os.environ
            and not "DB_URI" in os.environ):
        print "Skipping Sqlite migration!"
        return

    progress_reporter = get_progress_reporter()
    engine = engine_from_config(config.get_section(config.config_ini_section),
                                prefix="sqlalchemy.",
                                poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        transactional_ddl=False,
        on_version_apply=report_success,
    )

    try:
        with context.begin_transaction():
            try:
                context.run_migrations(tables=tables,
                                       tester=get_tester(),
                                       progress_reporter=progress_reporter)
            except (CommandError, ResolutionError) as ex:
                if "No such revision" not in str(ex):
                    raise

                if not REGION or not GIT_HEAD:
                    raise

                from data.model.release import get_recent_releases

                # ignore revision error if we're running the previous release
                releases = list(
                    get_recent_releases(SERVICE, REGION).offset(1).limit(1))
                if releases and releases[0].version == GIT_HEAD:
                    logger.warn(
                        "Skipping database migration because revision not found"
                    )
                else:
                    raise
    finally:
        connection.close()
Ejemplo n.º 2
0
Archivo: env.py Proyecto: kleesc/quay
def run_migrations_online():
    """
    Run migrations in 'online' mode.

    In this scenario we need to create an Engine and associate a connection with the context.
    """
    if isinstance(db.obj, SqliteDatabase) and "DB_URI" not in os.environ:
        logger.info("Skipping Sqlite migration!")
        return

    engine = get_engine()
    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        transactional_ddl=False,
    )

    try:
        with context.begin_transaction():
            try:
                context.run_migrations(op=alembic_op,
                                       tables=tables,
                                       tester=get_tester())
            except (CommandError, ResolutionError) as ex:
                if "No such revision" not in str(ex):
                    raise

                if not REGION or not GIT_HEAD:
                    raise

                from data.model.release import get_recent_releases

                # ignore revision error if we're running the previous release
                releases = list(
                    get_recent_releases(SERVICE, REGION).offset(1).limit(1))
                if releases and releases[0].version == GIT_HEAD:
                    logger.warn(
                        "Skipping database migration because revision not found"
                    )
                else:
                    raise
    finally:
        connection.close()
Ejemplo n.º 3
0
def run_migrations_online():
    """
    Run migrations in 'online' mode.

    In this scenario we need to create an Engine and associate a connection with the context.
    """

    if isinstance(db.obj, SqliteDatabase) and not "DB_URI" in os.environ:
        print "Skipping Sqlite migration!"
        return

    progress_reporter = get_progress_reporter()
    context.config.attributes["progress_reporter"] = progress_reporter
    op = ProgressWrapper(alembic_op, progress_reporter)

    migration = Migration()
    version_apply_callback = report_success
    if truthy_bool(
            context.get_x_argument(as_dictionary=True).get(
                "generatedbaopmigrations", False)):
        op = OpLogger(alembic_op, migration)
        version_apply_callback = partial(finish_migration, migration)

    engine = engine_from_config(config.get_section(config.config_ini_section),
                                prefix="sqlalchemy.",
                                poolclass=pool.NullPool)

    revision_to_migration = {}

    def process_revision_directives(context, revision, directives):
        script = directives[0]
        migration = Migration()
        revision_to_migration[(script.rev_id, revision)] = migration
        migration.add_hints_from_ops(script.upgrade_ops)

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        transactional_ddl=False,
        on_version_apply=version_apply_callback,
        process_revision_directives=process_revision_directives,
    )

    try:
        with context.begin_transaction():
            try:
                context.run_migrations(op=op,
                                       tables=tables,
                                       tester=get_tester())
            except (CommandError, ResolutionError) as ex:
                if "No such revision" not in str(ex):
                    raise

                if not REGION or not GIT_HEAD:
                    raise

                from data.model.release import get_recent_releases

                # ignore revision error if we're running the previous release
                releases = list(
                    get_recent_releases(SERVICE, REGION).offset(1).limit(1))
                if releases and releases[0].version == GIT_HEAD:
                    logger.warn(
                        "Skipping database migration because revision not found"
                    )
                else:
                    raise
    finally:
        connection.close()

    for (revision,
         previous_revision), migration in revision_to_migration.items():
        write_dba_operator_migration(migration, revision, previous_revision)