Skip to content
Snippets Groups Projects
Verified Commit db2473dc authored by Karel van Klink's avatar Karel van Klink :smiley_cat:
Browse files

add global locking mechanism to database migrations

parent 1956ad7f
Branches
Tags
1 merge request!95add global locking mechanism to database migrations
Pipeline #84350 passed
import logging import logging
import os
import orchestrator
from alembic import context from alembic import context
from orchestrator.db.database import BaseModel from orchestrator.db.database import BaseModel
from orchestrator.settings import app_settings 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 # this is the Alembic Config object, which provides
# access to the values within the .ini file in use. # access to the values within the .ini file in use.
...@@ -17,17 +15,8 @@ logger = logging.getLogger("alembic.env") ...@@ -17,17 +15,8 @@ logger = logging.getLogger("alembic.env")
config.set_main_option("sqlalchemy.url", app_settings.DATABASE_URI) 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 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: def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode. """Run migrations in 'offline' mode.
...@@ -85,8 +74,10 @@ def run_migrations_online() -> None: ...@@ -85,8 +74,10 @@ def run_migrations_online() -> None:
) )
try: try:
with context.begin_transaction(): with context.begin_transaction():
connection.execute(text("SELECT pg_advisory_xact_lock(1000);"))
context.run_migrations() context.run_migrations()
finally: finally:
connection.execute(text("SELECT pg_advisory_unlock(1000);"))
connection.close() connection.close()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment