feat: Add MySQL to PostgreSQL migration tool with JSONB transformation
Implement comprehensive migration solution with: - Full and incremental migration modes - JSONB schema transformation for RAWDATACOR and ELABDATADISP tables - Native PostgreSQL partitioning (2014-2031) - Optimized GIN indexes for JSONB queries - Rich logging with progress tracking - Complete benchmark system for MySQL vs PostgreSQL comparison - CLI interface with multiple commands (setup, migrate, benchmark) - Configuration management via .env file - Error handling and retry logic - Batch processing for performance (configurable batch size) Database transformations: - RAWDATACOR: 16 Val columns + units → single JSONB measurements - ELABDATADISP: 25+ measurement fields → structured JSONB with categories 🤖 Generated with Claude Code Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
149
src/migrator/full_migration.py
Normal file
149
src/migrator/full_migration.py
Normal file
@@ -0,0 +1,149 @@
|
||||
"""Full migration from MySQL to PostgreSQL."""
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
from config import get_settings, TABLE_CONFIGS
|
||||
from src.connectors.mysql_connector import MySQLConnector
|
||||
from src.connectors.postgres_connector import PostgreSQLConnector
|
||||
from src.transformers.data_transformer import DataTransformer
|
||||
from src.utils.logger import get_logger, setup_logger
|
||||
from src.utils.progress import ProgressTracker
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class FullMigrator:
|
||||
"""Perform full migration of a table from MySQL to PostgreSQL."""
|
||||
|
||||
def __init__(self, table: str):
|
||||
"""Initialize migrator for a table.
|
||||
|
||||
Args:
|
||||
table: Table name to migrate ('RAWDATACOR' or 'ELABDATADISP')
|
||||
"""
|
||||
if table not in TABLE_CONFIGS:
|
||||
raise ValueError(f"Unknown table: {table}")
|
||||
|
||||
self.table = table
|
||||
self.config = TABLE_CONFIGS[table]
|
||||
self.settings = get_settings()
|
||||
|
||||
def migrate(self, dry_run: bool = False) -> int:
|
||||
"""Perform full migration of the table.
|
||||
|
||||
Args:
|
||||
dry_run: If True, log what would be done but don't modify data
|
||||
|
||||
Returns:
|
||||
Total number of rows migrated
|
||||
"""
|
||||
setup_logger(__name__)
|
||||
|
||||
mysql_table = self.config["mysql_table"]
|
||||
pg_table = self.config["postgres_table"]
|
||||
|
||||
logger.info(f"Starting full migration of {mysql_table} -> {pg_table}")
|
||||
|
||||
try:
|
||||
with MySQLConnector() as mysql_conn:
|
||||
# Get total row count
|
||||
total_rows = mysql_conn.get_row_count(mysql_table)
|
||||
logger.info(f"Total rows to migrate: {total_rows}")
|
||||
|
||||
if dry_run:
|
||||
logger.info("[DRY RUN] Would migrate all rows")
|
||||
return total_rows
|
||||
|
||||
with PostgreSQLConnector() as pg_conn:
|
||||
# Check if table exists
|
||||
if not pg_conn.table_exists(pg_table):
|
||||
raise ValueError(
|
||||
f"PostgreSQL table {pg_table} does not exist. "
|
||||
"Run 'setup --create-schema' first."
|
||||
)
|
||||
|
||||
migrated = 0
|
||||
|
||||
with ProgressTracker(
|
||||
total_rows,
|
||||
f"Migrating {mysql_table}"
|
||||
) as progress:
|
||||
# Fetch and migrate rows in batches
|
||||
for batch in mysql_conn.fetch_all_rows(mysql_table):
|
||||
# Transform batch
|
||||
transformed = DataTransformer.transform_batch(
|
||||
mysql_table,
|
||||
batch
|
||||
)
|
||||
|
||||
# Insert batch
|
||||
columns = DataTransformer.get_column_order(pg_table)
|
||||
inserted = pg_conn.insert_batch(
|
||||
pg_table,
|
||||
transformed,
|
||||
columns
|
||||
)
|
||||
|
||||
migrated += inserted
|
||||
progress.update(inserted)
|
||||
|
||||
logger.info(
|
||||
f"✓ Migration complete: {migrated} rows migrated "
|
||||
f"to {pg_table}"
|
||||
)
|
||||
|
||||
# Update migration state
|
||||
self._update_migration_state(pg_conn, migrated)
|
||||
|
||||
return migrated
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Migration failed: {e}")
|
||||
raise
|
||||
|
||||
def _update_migration_state(
|
||||
self,
|
||||
pg_conn: PostgreSQLConnector,
|
||||
rows_migrated: int
|
||||
) -> None:
|
||||
"""Update migration state tracking table.
|
||||
|
||||
Args:
|
||||
pg_conn: PostgreSQL connection
|
||||
rows_migrated: Number of rows migrated
|
||||
"""
|
||||
try:
|
||||
pg_table = self.config["postgres_table"]
|
||||
query = f"""
|
||||
INSERT INTO migration_state
|
||||
(table_name, last_migrated_timestamp, total_rows_migrated, migration_completed_at, status)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
ON CONFLICT (table_name) DO UPDATE SET
|
||||
last_migrated_timestamp = EXCLUDED.last_migrated_timestamp,
|
||||
total_rows_migrated = EXCLUDED.total_rows_migrated,
|
||||
migration_completed_at = EXCLUDED.migration_completed_at,
|
||||
status = EXCLUDED.status
|
||||
"""
|
||||
now = datetime.utcnow()
|
||||
pg_conn.execute(query, (pg_table, now, rows_migrated, now, "completed"))
|
||||
logger.debug("Migration state updated")
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to update migration state: {e}")
|
||||
|
||||
|
||||
def run_full_migration(
|
||||
table: str,
|
||||
dry_run: bool = False
|
||||
) -> int:
|
||||
"""Run full migration for a table.
|
||||
|
||||
Args:
|
||||
table: Table name to migrate
|
||||
dry_run: If True, show what would be done without modifying data
|
||||
|
||||
Returns:
|
||||
Number of rows migrated
|
||||
"""
|
||||
migrator = FullMigrator(table)
|
||||
return migrator.migrate(dry_run=dry_run)
|
||||
Reference in New Issue
Block a user