alembic_cfg = Config("alembic.ini")
context = MigrationContext.configure(db.engine.connect())
current_rev = context.get_current_revision()
context._ensure_version_table()
script = ScriptDirectory.from_config(alembic_cfg)
head_revision = script.get_current_head()
context.stamp(script, head_revision)
command.downgrade(alembic_cfg, "base")--
You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy-alembic" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy-alembic/1EMiDnOlQjI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy-alem...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.
Thanks Michael for the tip. Here is the code I came up. Please feel free to make changes and use in your cookbook:Use Case 1: If someone new come to the project and they use db.create_all() or some script to create a fresh new database. There is no alembic_version table and the person will run into problems when doing the upgrade. The following code snippet can be added to database (re)create script to make sure the new database has most up-to-date version.# create db tablesdb.create_all()# add database version table and add current head versionalembic_cfg = Config("alembic.ini")connection = db.engine.connect()context = MigrationContext.configure(connection)context._ensure_version_table()script = ScriptDirectory.from_config(alembic_cfg)head_revision = script.get_current_head()context.stamp(script, head_revision)
Use Case 2: When doing tests, you may want to test your database migration script as well. Here is the code snippet:# in your testsdef test_migration(self):# create databasedb.create_all()# create config objectalembic_cfg = Config("alembic.ini")# get connection from db objectconnection = db.engine.connect()alembic_cfg.connection = connection# make sure version table exists or create onecontext = MigrationContext.configure(connection)context._ensure_version_table()# insert/update the head versionscript = ScriptDirectory.from_config(alembic_cfg)head_revision = script.get_current_head()context.stamp(script, head_revision)