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}")