From a7b73b0564348ae35350c6cead77f80759831a71 Mon Sep 17 00:00:00 2001 From: bonamin Date: Fri, 17 Apr 2026 15:15:43 +0300 Subject: [PATCH] fix: move SET LOCAL inside transaction in quotation/media/comms migration scripts --- backend/migration/migrate_crm_comms_log.py | 3 +-- backend/migration/migrate_crm_media.py | 3 +-- backend/migration/migrate_crm_quotation_items.py | 3 +-- backend/migration/migrate_crm_quotations.py | 6 ++---- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/backend/migration/migrate_crm_comms_log.py b/backend/migration/migrate_crm_comms_log.py index 1bda412..7c84691 100644 --- a/backend/migration/migrate_crm_comms_log.py +++ b/backend/migration/migrate_crm_comms_log.py @@ -63,13 +63,12 @@ async def run() -> None: }) async with AsyncPgSession() as session: - await session.execute(text("SET session_replication_role = replica")) async with session.begin(): + await session.execute(text("SET LOCAL session_replication_role = replica")) stmt = pg_insert(CrmCommsLog).values(records) stmt = stmt.on_conflict_do_nothing(index_elements=["id"]) await session.execute(stmt) dest_count = await pg_count(session, "crm_comms_log") - await session.execute(text("SET session_replication_role = DEFAULT")) if dest_count < source_count: msg = f"Count mismatch: source={source_count} postgres={dest_count}" diff --git a/backend/migration/migrate_crm_media.py b/backend/migration/migrate_crm_media.py index 114faae..8901d71 100644 --- a/backend/migration/migrate_crm_media.py +++ b/backend/migration/migrate_crm_media.py @@ -54,13 +54,12 @@ async def run() -> None: }) async with AsyncPgSession() as session: - await session.execute(text("SET session_replication_role = replica")) async with session.begin(): + await session.execute(text("SET LOCAL session_replication_role = replica")) stmt = pg_insert(CrmMedia).values(records) stmt = stmt.on_conflict_do_nothing(index_elements=["id"]) await session.execute(stmt) dest_count = await pg_count(session, "crm_media") - await session.execute(text("SET session_replication_role = DEFAULT")) if dest_count < source_count: msg = f"Count mismatch: source={source_count} postgres={dest_count}" diff --git a/backend/migration/migrate_crm_quotation_items.py b/backend/migration/migrate_crm_quotation_items.py index 62cde37..5decdf7 100644 --- a/backend/migration/migrate_crm_quotation_items.py +++ b/backend/migration/migrate_crm_quotation_items.py @@ -63,13 +63,12 @@ async def run() -> None: }) async with AsyncPgSession() as session: - await session.execute(text("SET session_replication_role = replica")) async with session.begin(): + await session.execute(text("SET LOCAL session_replication_role = replica")) stmt = pg_insert(CrmQuotationItem).values(records) stmt = stmt.on_conflict_do_nothing(index_elements=["id"]) await session.execute(stmt) dest_count = await pg_count(session, "crm_quotation_items") - await session.execute(text("SET session_replication_role = DEFAULT")) if dest_count < source_count: msg = f"Count mismatch: source={source_count} postgres={dest_count}" diff --git a/backend/migration/migrate_crm_quotations.py b/backend/migration/migrate_crm_quotations.py index 97bd0eb..474fe93 100644 --- a/backend/migration/migrate_crm_quotations.py +++ b/backend/migration/migrate_crm_quotations.py @@ -96,15 +96,13 @@ async def run() -> None: }) async with AsyncPgSession() as session: - # Disable FK enforcement for this session so we can insert quotations - # before the referenced crm_customers rows arrive in Phase 2. - await session.execute(text("SET session_replication_role = replica")) async with session.begin(): + # Disable FK enforcement so we can insert before crm_customers arrives in Phase 2. + await session.execute(text("SET LOCAL session_replication_role = replica")) stmt = pg_insert(CrmQuotation).values(records) stmt = stmt.on_conflict_do_nothing(index_elements=["id"]) await session.execute(stmt) dest_count = await pg_count(session, "crm_quotations") - await session.execute(text("SET session_replication_role = DEFAULT")) if dest_count < source_count: msg = f"Count mismatch: source={source_count} postgres={dest_count}"