Skip to content
Snippets Groups Projects

add global locking mechanism to database migrations

Merged Karel van Klink requested to merge feature/update-alembic-lock into develop
1 file
+ 3
12
Compare changes
  • Side-by-side
  • Inline
+ 3
12
import logging
import os
import orchestrator
from alembic import context
from orchestrator.db.database import BaseModel
from orchestrator.settings import app_settings
from sqlalchemy import engine_from_config, pool
from sqlalchemy import engine_from_config, pool, text
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
@@ -17,17 +15,8 @@ logger = logging.getLogger("alembic.env")
config.set_main_option("sqlalchemy.url", app_settings.DATABASE_URI)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = BaseModel.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
@@ -85,8 +74,10 @@ def run_migrations_online() -> None:
)
try:
with context.begin_transaction():
connection.execute(text("SELECT pg_advisory_xact_lock(1000);"))
context.run_migrations()
finally:
connection.execute(text("SELECT pg_advisory_unlock(1000);"))
connection.close()
Loading