Skip to content

Commit d24ee48

Browse files
authored
make trigger cleanup schema aware (Netflix#436)
1 parent 2fb2ebf commit d24ee48

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

services/data/postgres_async_db.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
operator_match = re.compile('([^:]*):([=><]+)$')
4040

4141
# use a ddmmyyy timestamp as the version for triggers
42-
TRIGGER_VERSION = "23082024"
42+
TRIGGER_VERSION = "05092024"
4343
TRIGGER_NAME_PREFIX = "notify_ui"
4444

4545

@@ -442,30 +442,31 @@ async def create_trigger_if_missing(db: _AsyncPostgresDB, table_name, trigger_na
442442
cur.close()
443443

444444
@staticmethod
445-
async def cleanup_triggers(db: _AsyncPostgresDB, table_name):
445+
async def cleanup_triggers(db: _AsyncPostgresDB, table_name, schema=DB_SCHEMA_NAME):
446446
"Cleans up old versions of table triggers"
447447
with (await db.pool.cursor()) as cur:
448448
try:
449449
await cur.execute(
450450
"""
451-
SELECT DISTINCT trigger_name, trigger_schema
451+
SELECT DISTINCT trigger_name
452452
FROM information_schema.triggers
453453
WHERE event_object_table = %s
454+
AND trigger_schema = %s
454455
""",
455-
[table_name]
456+
[table_name, schema]
456457
)
457458
results = await cur.fetchall()
458459

459460
triggers_to_cleanup = [
460-
(res[0], res[1]) for res in results
461+
res[0] for res in results
461462
if res[0].startswith(TRIGGER_NAME_PREFIX) and TRIGGER_VERSION not in res[0]
462463
]
463464
if triggers_to_cleanup:
464465
logging.getLogger("TriggerSetup").info("Cleaning up old triggers: %s" % triggers_to_cleanup)
465466
commands = []
466-
for trigger_name, schema in triggers_to_cleanup:
467+
for trigger_name in triggers_to_cleanup:
467468
commands += [
468-
(f"DROP TRIGGER IF EXISTS {trigger_name} ON {table_name}"),
469+
(f"DROP TRIGGER IF EXISTS {trigger_name} ON {schema}.{table_name}"),
469470
(f"DROP FUNCTION IF EXISTS {schema}.{trigger_name}")
470471
]
471472

0 commit comments

Comments
 (0)