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,30 +255,30 @@ 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) VALUES (%s, %s, %s, %s, %s, %s)
VALUES (%s, %s, %s, %s, %s, %s) ON CONFLICT (table_name) DO UPDATE SET
ON CONFLICT (table_name) DO UPDATE SET last_migrated_timestamp = EXCLUDED.last_migrated_timestamp,
last_migrated_timestamp = EXCLUDED.last_migrated_timestamp, last_migrated_id = EXCLUDED.last_migrated_id,
last_migrated_id = EXCLUDED.last_migrated_id, total_rows_migrated = EXCLUDED.total_rows_migrated,
total_rows_migrated = EXCLUDED.total_rows_migrated, migration_completed_at = EXCLUDED.migration_completed_at,
migration_completed_at = EXCLUDED.migration_completed_at, status = EXCLUDED.status
status = EXCLUDED.status """
""" cursor.execute(
pg_conn.execute( query,
query, (
( pg_table,
pg_table, migration_start_time or now.isoformat(),
migration_start_time or now.isoformat(), last_id,
last_id, rows_migrated,
rows_migrated, now if status == "completed" else None,
now if status == "completed" else None, 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}")