def swap_tables(self): """Swap tables around to present the exported data. Swaps the current tables to old tables, then swaps write tables to current. Finally drops the old tables leaving just the current tables. """ connection = self.engine.connect() ctx = MigrationContext.configure(connection) op = Operations(ctx) def gen_table_names(write_table): """Generate current and old table names from write tables.""" # Current tables do not have the prefix 'w' current_table = write_table[1:] old_table = current_table + "_old" return write_table, current_table, old_table tables = dict(Base.metadata.tables) tables.pop("kvittering") tables = tables.keys() tables = list(map(gen_table_names, tables)) # Drop any left-over old tables that may exist with ctx.begin_transaction(): for _, _, old_table in tables: try: op.drop_table(old_table) except Exception: pass # Rename current to old and write to current with ctx.begin_transaction(): for write_table, current_table, old_table in tables: # Rename current table to old table # No current tables is OK try: op.rename_table(current_table, old_table) except Exception: pass # Rename write table to current table op.rename_table(write_table, current_table) # Drop any old tables that may exist with ctx.begin_transaction(): for _, _, old_table in tables: # Drop old tables try: op.drop_table(old_table) except Exception: pass
def rename_table(self, from_name, to_name, fields=None, keep_table=True): """Rename table. """ from alembic.migration import MigrationContext from alembic.operations import Operations # Refresh the connection again self._engine = create_engine(self._connection) conn = self._engine.connect() ctx = MigrationContext.configure(conn) op = Operations(ctx) op.rename_table(from_name, to_name) if keep_table: assert fields is not None, ( "Fields must be provided to create the table") self.create_table(table_name=from_name, fields=fields)
def recheck_alembic_table(conn): """check and update alembic version table. Should check current alembic version table against conf and rename the existing table if the two values don't match. """ conf_table = getattr(CONF, 'version_table') conf_table_version = get_table_version(conn, conf_table) current_table, default_table = get_db_tables(conn) if current_table[0]: if current_table[0] != conf_table: context = alembic_migration.MigrationContext.configure(conn) op = Operations(context) if conf_table and not conf_table_version: # make sure there is not present-but-empty table # that will prevent us from renaming the current table op.drop_table(conf_table) op.rename_table(current_table[0], conf_table)
def recheck_alembic_table(conn): """check and update alembic version table. Should check current alembic version table against conf and rename the existing table if the two values don't match. """ conf_table = getattr(CONF, 'version_table') conf_table_version = get_table_version(conn, conf_table) current_table, default_table = get_db_tables(conn) if current_table[0]: if current_table[0] != conf_table: context = alembic_migration.MigrationContext.configure(conn) op = Operations(context) if conf_table and not conf_table_version: # make sure there is not present-but-empty table # that will prevent us from renaming the current table op.drop_table(conf_table) op.rename_table(current_table[0], conf_table)
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. """ engine = engine_from_config( config.get_section(config.config_ini_section), prefix='sqlalchemy.', poolclass=pool.NullPool) logger.info('Testing for an old alembic_version table.') connection = engine.connect() context.configure( connection=connection, target_metadata=target_metadata, version_table='alembic_version' ) script_location = config.get_main_option('script_location') found = False mc = context.get_context() current_db_revision = mc.get_current_revision() script = ScriptDirectory.from_config(config) """ If there was an existing alembic_version table, we need to check that it's current revision is in the history for the tree we're working with. """ for x in script.iterate_revisions('head', 'base'): if x.revision == current_db_revision: """ An alembic_versions table was found and it belongs to this alembic tree """ logger.info( ('An old alembic_version table at revision %s was ' 'found for %s. Renaming to alembic_version_%s.'), current_db_revision, script_location, script_location) op = Operations(mc) try: with context.begin_transaction(): op.rename_table( 'alembic_version', 'alembic_version_%s' % script_location) found = True except: logger.error(('Unable to rename alembic_version to ' 'alembic_version_%s.'), script_location) connection.close() return break if not found: logger.info('Didn\'t find an old alembic_version table.') logger.info('Trying alembic_version_%s.' % script_location) """ We MAY have an alembic_version table that doesn't belong to this tree but if we still don't have an alembic_version_<tree> table, alembic will create it. """ context.configure( connection=connection, target_metadata=target_metadata, version_table='alembic_version_' + script_location ) mc = context.get_context() current_db_revision = mc.get_current_revision() if current_db_revision: logger.info( 'Using the alembic_version_%s table at revision %s.', script_location, current_db_revision) else: logger.info('Creating new alembic_version_%s table.', script_location) try: with context.begin_transaction(): context.run_migrations() finally: connection.close()
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. """ engine = engine_from_config(config.get_section(config.config_ini_section), prefix='sqlalchemy.', poolclass=pool.NullPool) logger.info('Testing for an old alembic_version table.') connection = engine.connect() context.configure(connection=connection, target_metadata=target_metadata, version_table='alembic_version') script_location = config.get_main_option('script_location') found = False mc = context.get_context() current_db_revision = mc.get_current_revision() script = ScriptDirectory.from_config(config) """ If there was an existing alembic_version table, we need to check that it's current revision is in the history for the tree we're working with. """ for x in script.iterate_revisions('head', 'base'): if x.revision == current_db_revision: """ An alembic_versions table was found and it belongs to this alembic tree """ logger.info(('An old alembic_version table at revision %s was ' 'found for %s. Renaming to alembic_version_%s.'), current_db_revision, script_location, script_location) op = Operations(mc) try: with context.begin_transaction(): op.rename_table('alembic_version', 'alembic_version_%s' % script_location) found = True except: logger.error(('Unable to rename alembic_version to ' 'alembic_version_%s.'), script_location) connection.close() return break if not found: logger.info('Didn\'t find an old alembic_version table.') logger.info('Trying alembic_version_%s.' % script_location) """ We MAY have an alembic_version table that doesn't belong to this tree but if we still don't have an alembic_version_<tree> table, alembic will create it. """ context.configure(connection=connection, target_metadata=target_metadata, version_table='alembic_version_' + script_location) mc = context.get_context() current_db_revision = mc.get_current_revision() if current_db_revision: logger.info('Using the alembic_version_%s table at revision %s.', script_location, current_db_revision) else: logger.info('Creating new alembic_version_%s table.', script_location) try: with context.begin_transaction(): context.run_migrations() finally: connection.close()
def rename_table(name, schema, engine, rename_to): table = reflect_table(name, schema, engine) with engine.begin() as conn: ctx = MigrationContext.configure(conn) op = Operations(ctx) op.rename_table(table.name, rename_to, schema=table.schema)