mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Check current migration on startup
This commit is contained in:
parent
9518bbe168
commit
37dd5ba3e8
2 changed files with 34 additions and 2 deletions
26
database/migrations.py
Normal file
26
database/migrations.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import logging
|
||||
|
||||
from alembic import config, script
|
||||
from alembic.runtime import migration
|
||||
|
||||
from database.engine import engine
|
||||
|
||||
|
||||
async def ensure_latest_migration():
|
||||
"""Make sure we are currently on the latest revision, otherwise raise an exception"""
|
||||
alembic_config = config.Config("alembic.ini")
|
||||
alembic_script = script.ScriptDirectory.from_config(alembic_config)
|
||||
|
||||
async with engine.begin() as connection:
|
||||
current_revision = await connection.run_sync(
|
||||
lambda sync_connection: migration.MigrationContext.configure(sync_connection).get_current_revision()
|
||||
)
|
||||
|
||||
alembic_head = alembic_script.get_current_head()
|
||||
|
||||
if current_revision != alembic_head:
|
||||
error_message = (
|
||||
f"Pending migrations (current revision is {current_revision}, while head is at {alembic_head})"
|
||||
)
|
||||
logging.error(error_message)
|
||||
raise RuntimeError(error_message)
|
||||
Loading…
Add table
Add a link
Reference in a new issue