Skip to content

Commit

Permalink
Acquire the global DDL lock if BDR is enabled
Browse files Browse the repository at this point in the history
This should allow users to run Alembic in "online" mode with a Postgres
BDR system. It will also emit the correct SQL for offline migrations.

It introduces a new boolean setting in alembic.ini, "bdr", which
defaults to false.

Signed-off-by: Jeremy Cline <[email protected]>
  • Loading branch information
jeremycline authored and bowlofeggs committed Jun 1, 2017
1 parent 1d7a767 commit 05ab85a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ script_location = alembic
# Don't bother, this is obtained from the Bodhi config file
sqlalchemy.url = sqlite://bodhi.db

# Set to true to aquire the global DDL lock for BDR
# See http://bdr-project.org/docs/stable/ddl-replication-advice.html
bdr = false


# Logging configuration
[loggers]
Expand Down
10 changes: 9 additions & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from logging.config import fileConfig

from alembic import context
from sqlalchemy import engine_from_config, pool
from sqlalchemy import engine_from_config, pool, event

from bodhi.server.config import config as bodhi_config
from bodhi.server.models import Base
Expand Down Expand Up @@ -43,6 +43,8 @@ def run_migrations_offline():
context.configure(url=url)

with context.begin_transaction():
if config.get_main_option('bdr').strip().lower() == 'true':
context.execute('SET LOCAL bdr.permit_ddl_locking = true')
context.run_migrations()


Expand All @@ -58,6 +60,12 @@ def run_migrations_online():
prefix='sqlalchemy.',
poolclass=pool.NullPool)

if config.get_main_option('bdr').strip().lower() == 'true':
def enable_bdr(connection, connection_record):
with connection.cursor() as cursor:
cursor.execute('SET LOCAL bdr.permit_ddl_locking = true')
event.listen(engine, 'connect', enable_bdr)

connection = engine.connect()
context.configure(
connection=connection,
Expand Down

0 comments on commit 05ab85a

Please sign in to comment.