Created
May 10, 2019 22:00
-
-
Save m-aciek/118d450ee59a41176214b5f93a02cc6f to your computer and use it in GitHub Desktop.
Alembic database freshness check.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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