From 26b3ccb06ea3eeac04aaad6bbecd86b85e4e5710 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Dec 2025 20:26:10 +0100 Subject: [PATCH] fix: Ensure migration_state updates are committed to database MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The _update_migration_state() method was using pg_conn.execute() which has its own connection management. This could cause issues with transaction handling when called at end of migration. Changed to use explicit cursor with guaranteed commit: - Use pg_conn.connection.cursor() to get a direct cursor - Execute the INSERT ... ON CONFLICT query - Explicitly call pg_conn.connection.commit() - This matches the pattern used in other parts of the code This ensures that final migration state (completed status, final counts) are properly persisted to the database. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Haiku 4.5 --- src/migrator/full_migration.py | 46 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/migrator/full_migration.py b/src/migrator/full_migration.py index 426161e..f59d175 100644 --- a/src/migrator/full_migration.py +++ b/src/migrator/full_migration.py @@ -255,30 +255,30 @@ class FullMigrator: # Update PostgreSQL migration_state table try: - # Use COALESCE to handle both insert (first time) and update (resume) - # For resume: total_rows_migrated will be the full accumulated count - query = f""" - INSERT INTO migration_state - (table_name, last_migrated_timestamp, last_migrated_id, total_rows_migrated, migration_completed_at, status) - VALUES (%s, %s, %s, %s, %s, %s) - ON CONFLICT (table_name) DO UPDATE SET - last_migrated_timestamp = EXCLUDED.last_migrated_timestamp, - last_migrated_id = EXCLUDED.last_migrated_id, - total_rows_migrated = EXCLUDED.total_rows_migrated, - migration_completed_at = EXCLUDED.migration_completed_at, - status = EXCLUDED.status - """ - pg_conn.execute( - query, - ( - pg_table, - migration_start_time or now.isoformat(), - last_id, - rows_migrated, - now if status == "completed" else None, - status + with pg_conn.connection.cursor() as cursor: + query = f""" + INSERT INTO migration_state + (table_name, last_migrated_timestamp, last_migrated_id, total_rows_migrated, migration_completed_at, status) + VALUES (%s, %s, %s, %s, %s, %s) + ON CONFLICT (table_name) DO UPDATE SET + last_migrated_timestamp = EXCLUDED.last_migrated_timestamp, + last_migrated_id = EXCLUDED.last_migrated_id, + total_rows_migrated = EXCLUDED.total_rows_migrated, + migration_completed_at = EXCLUDED.migration_completed_at, + status = EXCLUDED.status + """ + cursor.execute( + query, + ( + pg_table, + migration_start_time or now.isoformat(), + last_id, + rows_migrated, + now if status == "completed" else None, + status + ) ) - ) + pg_conn.connection.commit() logger.debug(f"Migration state updated: {rows_migrated} rows total, last_id={last_id}, status={status}") except Exception as e: logger.warning(f"Failed to update migration state in PostgreSQL: {e}")