fix: Ensure migration_state updates are committed to database

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 <noreply@anthropic.com>
This commit is contained in:
2025-12-23 20:26:10 +01:00
parent 1708969616
commit 26b3ccb06e

View File

@@ -255,8 +255,7 @@ class FullMigrator:
# Update PostgreSQL migration_state table # Update PostgreSQL migration_state table
try: try:
# Use COALESCE to handle both insert (first time) and update (resume) with pg_conn.connection.cursor() as cursor:
# For resume: total_rows_migrated will be the full accumulated count
query = f""" query = f"""
INSERT INTO migration_state INSERT INTO migration_state
(table_name, last_migrated_timestamp, last_migrated_id, total_rows_migrated, migration_completed_at, status) (table_name, last_migrated_timestamp, last_migrated_id, total_rows_migrated, migration_completed_at, status)
@@ -268,7 +267,7 @@ class FullMigrator:
migration_completed_at = EXCLUDED.migration_completed_at, migration_completed_at = EXCLUDED.migration_completed_at,
status = EXCLUDED.status status = EXCLUDED.status
""" """
pg_conn.execute( cursor.execute(
query, query,
( (
pg_table, pg_table,
@@ -279,6 +278,7 @@ class FullMigrator:
status status
) )
) )
pg_conn.connection.commit()
logger.debug(f"Migration state updated: {rows_migrated} rows total, last_id={last_id}, status={status}") logger.debug(f"Migration state updated: {rows_migrated} rows total, last_id={last_id}, status={status}")
except Exception as e: except Exception as e:
logger.warning(f"Failed to update migration state in PostgreSQL: {e}") logger.warning(f"Failed to update migration state in PostgreSQL: {e}")