Skip to content

Commit

Permalink
Fix trigger creation during alembic migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
lyndsysimon committed Jan 11, 2019
1 parent a49ddcc commit fb6b1e9
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions sqlalchemy_continuum/transaction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from functools import partial

try:
from collections import OrderedDict
Expand Down Expand Up @@ -73,10 +74,25 @@ def changed_entities(self):
"""


def create_triggers(cls):
def _after_create(cls, ddl):
"""Execute DDL after a table is created
This is required for compatibility with alembic, as the `after_create` event
does not fire during alembic migrations"""
def listener(tablename, ddl, table, bind, **kw):
if table.name == tablename:
ddl(table, bind, **kw)

sa.event.listen(
cls.__table__,
'after_create',
sa.Table,
'after_create',
partial(listener, cls.__table__.name, ddl)
)


def create_triggers(cls):
_after_create(
cls,
sa.schema.DDL(
procedure_sql.format(
temporary_transaction_sql=CreateTemporaryTransactionTableSQL(),
Expand All @@ -88,9 +104,8 @@ def create_triggers(cls):
)
)
)
sa.event.listen(
cls.__table__,
'after_create',
_after_create(
cls,
sa.schema.DDL(str(TransactionTriggerSQL(cls)))
)
sa.event.listen(
Expand Down

0 comments on commit fb6b1e9

Please sign in to comment.