#!/usr/bin/env python3 """Sync migration_state with actual data in PostgreSQL tables.""" import sys sys.path.insert(0, '/home/alex/devel/mysql2postgres') from src.connectors.postgres_connector import PostgreSQLConnector from src.migrator.state_manager import StateManager def sync_table_state(table_name: str): """Sync migration_state for a table with its actual data.""" with PostgreSQLConnector() as pg_conn: cursor = pg_conn.connection.cursor() # Find the row with MAX(created_at) - most recently inserted # Exclude corrupted data (Java strings) cursor.execute(f""" SELECT unit_name, tool_name_id, DATE(event_timestamp)::text as event_date, event_timestamp::time::text as event_time, created_at, event_timestamp FROM {table_name} WHERE unit_name NOT LIKE '[L%' -- Exclude corrupted Java strings ORDER BY created_at DESC LIMIT 1 """) result = cursor.fetchone() if not result: print(f"No data found in {table_name}") return unit_name, tool_name_id, event_date, event_time, created_at, event_timestamp = result print(f"\n{table_name.upper()}:") print(f" Most recently inserted row (by created_at):") print(f" created_at: {created_at}") print(f" event_timestamp: {event_timestamp}") print(f" Consolidation key: ({unit_name}, {tool_name_id}, {event_date}, {event_time})") # Update global migration_state with this key state_mgr = StateManager(pg_conn, table_name, partition_name="_global") last_key = { "unit_name": unit_name, "tool_name_id": tool_name_id, "event_date": event_date, "event_time": event_time } state_mgr.update_state(last_key=last_key) print(f" ✓ Updated migration_state with this key") if __name__ == "__main__": print("Syncing migration_state with actual PostgreSQL data...") print("="*80) sync_table_state("elabdatadisp") sync_table_state("rawdatacor") print("\n" + "="*80) print("✓ Done! Incremental migration will now start from the correct position.")