def produce_migrations(self): """Generate the :class:`~alembic.autogenerate.MigrationScript` object that would generate a new revision. """ db = current_app.extensions["sqlalchemy"].db return autogenerate.produce_migrations(self.migration_context, db.metadata)
def upgrade(): ''' First migration. Generates a migration script by difference between model and database and executes it ''' target_metadata = Base.metadata mc = context.get_context() migration_script = produce_migrations(mc, target_metadata) autogen_context = api.AutogenContext( mc, autogenerate=True ) # Seems to be the only way to apply changes to the database template_args = {} render._render_python_into_templatevars( autogen_context, migration_script, template_args ) code = template_args['upgrades'] code = code.replace('\n ', '\n') print('\nPerforming database changes:') print(code) exec(code)
def test_alembic_autogenerate_nextbigid(Foo, connection, Base, engine): from sqlalchemy_bigid import migration # noqa context = MigrationContext.configure( connection=connection, ) migration_script = produce_migrations(context, Base.metadata) first_upgrade_op = migration_script.upgrade_ops.ops[0] assert isinstance(first_upgrade_op, CreateNextBigIdFunctionOp)
def generate_migration(metadata, engine): """Create MigrationContext and MigrationScript""" mc = MigrationContext.configure(engine.connect()) ms = produce_migrations(mc, metadata) ac = AutogenContext(None, opts={ 'sqlalchemy_module_prefix': 'sa.', 'alembic_module_prefix': 'op.', }) lines = [] for x in ms.upgrade_ops.ops: lines.append(render_op_text(ac, x)) return mc, lines
def migrate(): from alembic.runtime.migration import MigrationContext # use `db.session.connection()` instead of `db.engine.connect()` # to avoid lock hang context = MigrationContext.configure( db.session.connection(), opts={ "compare_type": True, }, ) if request.method == "GET": import pprint from alembic.autogenerate import compare_metadata diff = compare_metadata(context, db.metadata) diff_str = pprint.pformat(diff, indent=2, width=20) logger.info("Migrate steps: %s", diff_str) return respond_success(migration=diff_str) from alembic.autogenerate import produce_migrations from alembic.operations import Operations from alembic.operations.ops import OpContainer migration = produce_migrations(context, db.metadata) operation = Operations(context) for outer_op in migration.upgrade_ops.ops: logger.info("Invoking %s", outer_op) if isinstance(outer_op, OpContainer): for inner_op in outer_op.ops: logger.info("Invoking %s", inner_op) operation.invoke(inner_op) else: operation.invoke(outer_op) db.session.commit() db.session.close() return respond_success()
id integer not null primary key, old_data varchar, x integer )""") engine.execute(''' create table bar ( data varchar )''') metadata = MetaData() Table('foo', metadata, Column('id', Integer, primary_key=True), Column('data', Integer), Column('x', Integer, nullable=False)) Table('bat', metadata, Column('info', String)) ctx = MigrationContext.configure(engine.connect()) mc = produce_migrations(ctx, metadata) ops = Operations(ctx) op = mc.upgrade_ops.ops[0] if __name__ == '__main__': print(op) print(ops.invoke(op)) # diff = compare_metadata(mc, metadata) # pprint.pprint(diff, indent=2, width=20)
def get_outstanding_migrations(self): return produce_migrations( MigrationContext.configure(connection=Session.connection()), metadata)