Skip to content

Instantly share code, notes, and snippets.

@m-aciek
Created May 10, 2019 22:00
Show Gist options
  • Save m-aciek/118d450ee59a41176214b5f93a02cc6f to your computer and use it in GitHub Desktop.
Save m-aciek/118d450ee59a41176214b5f93a02cc6f to your computer and use it in GitHub Desktop.
Alembic database freshness check.
from alembic import config
from alembic import script
from alembic.runtime import migration
import sqlalchemy
import exceptions
engine = sqlalchemy.create_engine(DATABASE_URL)
alembic_cfg = config.Config('alembic.ini')
script_ = script.ScriptDirectory.from_config(alembic_cfg)
with engine.begin() as conn:
context = migration.MigrationContext.configure(conn)
if context.get_current_revision() != script_.get_current_head():
raise exceptions.DatabaseIsNotUpToDate('Upgrade the database.')
@m-aciek
Copy link
Author

m-aciek commented Jan 17, 2024

Above was published in Alembic Cookbook and improved/updated over time: https://alembic.sqlalchemy.org/en/latest/cookbook.html#test-current-database-revision-is-at-head-s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment