Skip to content

vailtom/SQLite-to-MySQL-Migrator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SQLite to MySQL Migrator

High-performance SQLite to MySQL database migration tool with performance optimizations, robust error handling, and automated post-migration scripts.

Features

  • Performance optimized: Batch inserts with temporary check disabling
  • Real-time feedback: Live progress monitoring during migration
  • Execution time tracking: Duration display for each migrated table
  • Smart error handling: Detailed logging created only when necessary
  • Prerequisites validation: Pre-migration requirement checks
  • Connection compression: Uses MySQL compression when available
  • Transaction safety: Automatic rollback on failure
  • Selective migration: Migrate specific tables, multiple tables, or from file lists
  • Type optimization: Analyzes real data to optimize VARCHAR/CHAR types
  • Strict mode: Exact sizing for controlled data (no growth expected)
  • Configurable fields: Customizable list of indexable fields
  • Sync verification: Compare record counts between SQLite and MySQL
  • Bulk comparison: Analyze all tables simultaneously
  • Database analysis: Detailed overview of SQLite and MySQL databases
  • Post-migration scripts: Automated execution of custom indexes, views, and procedures
  • Automation mode: --force option for scripts and automation
  • Exit codes: Specific exit codes for script integration
  • DBMS compatibility: Supports MySQL and MariaDB
  • Built-in help: -h command to see all parameters
  • Flexibility: Option to migrate schema-only or complete data

Prerequisites

pip install mysql-connector-python

Getting Started

1. Check help

python migrator.py -h

2. Create virtual environment (recommended)

python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
venv\Scripts\activate     # Windows

3. Install dependencies

pip install mysql-connector-python

4. Create configuration file

Run the command to generate an example file:

python migrator.py --create-config

5. Configure MySQL credentials

Rename mysql_config.ini.example to mysql_config.ini and configure:

[mysql]
host = localhost
port = 3306
user = root
password = your_password
database = database_name

6. Configure indexable fields (optional)

On first run with --analyze-types, indexable_fields.txt will be created:

# Indexable Fields Configuration File
# 
# List of field names that should be optimized for indexes
# One field per line
# Lines starting with # are comments

# Identification fields
id
codigo
cod
code

# Your project-specific fields:
product_code
client_ref
order_num

7. Configure post-migration scripts (optional)

The system automatically creates template files for custom scripts:

  • post_migration.sql - Main script (configurations, optimizations)
  • indexes.sql - Custom indexes
  • views.sql - System views
  • procedures.sql - Stored procedures and functions

Usage

Help and available parameters

# Show all available parameters
python migrator.py -h
python migrator.py --help

Complete migration (schema + data)

python migrator.py my_database.db

Schema-only migration

python migrator.py my_database.db --schema-only

Specific table migration

# Migrate single table
python migrator.py my_database.db --table users

# Migrate multiple tables (comma-separated)
python migrator.py my_database.db --table users,products,orders

# Migrate schema-only for multiple tables
python migrator.py my_database.db --table products,categories --schema-only

Data type optimization

# Analyze data to optimize types (conservative mode)
python migrator.py my_database.db --table users --analyze-types

# Strict mode: exact sizing for controlled/stable data
python migrator.py my_database.db --table users --analyze-types --strict-sizing

# Example analysis output:
# πŸ“Š Processing table: users
#   πŸ“‹ Loaded 25 indexable fields from indexable_fields.txt
#     Analyzing indexable field: email
#       Data: 1000 records, size 12-45, CONSERVATIVE mode, suggested: 67
#     Analyzing indexable field: code
#       Data: 250 records, size 3-3, FIXED mode, suggested: 4  
#   βœ… 1,250 records migrated

Post-migration scripts

# Migration with automatic scripts (default)
python migrator.py my_database.db --table users

# Skip post-migration script execution
python migrator.py my_database.db --table users --skip-post-scripts

# Example script execution:
# πŸ”§ Executing post-migration scripts...
#   πŸ“œ Executing indexes.sql...
#     βœ… 3/3 statements executed
#   βœ… indexes.sql executed successfully
#   πŸ“œ Executing views.sql...
#     βœ… 2/2 statements executed
#   βœ… views.sql executed successfully
# βœ… Scripts executed: indexes.sql, views.sql

Synchronization verification

# Compare records for specific table
python migrator.py my_database.db --table-info users

# Example output:
# πŸ“Š Table information: users
# --------------------------------------------------
# SQLite: 1,250 records
# MySQL:  1,180 records
# ⚠️  SQLite has 70 more records

# Compare all tables
python migrator.py my_database.db --table-compare

# Example output:
# πŸ“Š SQLite ↔ MySQL table comparison
# ================================================================================
# Table                     SQLite       MySQL        Status              
# --------------------------------------------------------------------------------
# ESTABLISHMENTS            65,168,570   65,168,570   βœ… Synchronized
# COMPANIES                 62,085,952   62,080,000   ⚠️  +5,952 in SQLite
# SIMPLE                    42,772,819   N/A          ❌ Does not exist
# --------------------------------------------------------------------------------
# πŸ“‹ Summary: 3 table(s) analyzed
#    βœ… Synchronized: 1
#    ⚠️  Different: 1
#    ❌ Not in MySQL: 1

Database overview

# Show SQLite overview
python migrator.py my_database.db --info-sqlite

# Example output:
# πŸ“Š SQLite general information
# ============================================================
# Table                     Records      Columns
# --------------------------------------------------
# users                     25,340       8
# products                  12,890       12
# orders                    8,750        15
# --------------------------------------------------
# TOTAL                     52,376       3 tables
# πŸ“‹ File: my_database.db (127.45 MB)

# Show MySQL overview
python migrator.py my_database.db --info-mysql

# Example output:
# πŸ“Š MySQL general information
# ============================================================
# Table                     Records      Total MB   Data MB    Index MB
# ----------------------------------------------------------------------
# users                     25,340       45.67      42.12      3.55
# products                  12,890       23.45      20.11      3.34
# orders                    8,750        15.23      13.89      1.34
# ----------------------------------------------------------------------
# TOTAL                     47,136       84.47
# πŸ“‹ Database: my_app | Version: 11.6.2-MariaDB

Force migration (no confirmations)

# Replace existing tables automatically
python migrator.py my_database.db --force
python migrator.py my_database.db -f

# Combine with other options
python migrator.py my_database.db --table users --force

Advanced configurations

# Custom configuration file
python migrator.py my_database.db --config /path/to/config.ini

# Custom batch size
python migrator.py my_database.db --batch-size 5000

# Combining multiple options
python migrator.py my_database.db --schema-only --config prod_config.ini --force

Post-Migration Scripts

How it works

After a successful migration, the system automatically:

  1. Checks for table-specific .sql files based on migrated tables
  2. Analyzes if they contain valid SQL commands (not just comments)
  3. Executes only relevant scripts for the migrated tables
  4. Falls back to general scripts only for complete migrations
  5. Reports execution results

Supported Files

Table-Specific Scripts (Priority)

Pattern Example Executed When
indexes_[table].sql indexes_companies.sql COMPANIES table is migrated
[table]_indexes.sql companies_indexes.sql COMPANIES table is migrated
views_[table].sql views_companies.sql COMPANIES table is migrated
[table]_views.sql companies_views.sql COMPANIES table is migrated
procedures_[table].sql procedures_companies.sql COMPANIES table is migrated
[table]_procedures.sql companies_procedures.sql COMPANIES table is migrated

General Scripts (Fallback)

File Purpose Executed When
post_migration.sql General configurations, optimizations Complete migration (>5 tables)
indexes.sql General indexes Complete migration (>5 tables)
views.sql General views Complete migration (>5 tables)
procedures.sql General procedures and functions Complete migration (>5 tables)

Intelligent Script Selection

Specific Table Migration:

python migrator.py database.db --table COMPANIES --force

Executes: Only indexes_companies.sql (if exists)

Multiple Tables Migration:

python migrator.py database.db --table COMPANIES,ESTABLISHMENTS --force

Executes: indexes_companies.sql + indexes_establishments.sql

Complete Migration:

python migrator.py database.db --force

Executes: General scripts (indexes.sql, views.sql, etc.)

Automatic Template Creation

On first run, if no files are found, the system creates templates with examples:

python migrator.py my_database.db --table COMPANIES

# If no scripts exist, automatically creates:
# πŸ“ Example files created: post_migration.sql, indexes.sql, views.sql, procedures.sql, indexes_companies.sql
# πŸ’‘ For table-specific scripts, use patterns:
#    - indexes_[table].sql or [table]_indexes.sql
#    - views_[table].sql or [table]_views.sql  
#    - procedures_[table].sql or [table]_procedures.sql

Example indexes_companies.sql

-- Table-specific indexes for COMPANIES
-- Executed only when COMPANIES table is migrated

-- Unique index on COD field (correlation key)
CREATE UNIQUE INDEX IF NOT EXISTS idx_companies_cod ON COMPANIES(COD);

-- Indexes for frequent queries
CREATE INDEX IF NOT EXISTS idx_companies_status ON COMPANIES(status);
CREATE INDEX IF NOT EXISTS idx_companies_type ON COMPANIES(company_type);

-- Composite indexes for reports
CREATE INDEX IF NOT EXISTS idx_companies_status_type ON COMPANIES(status, company_type);

Example indexes_establishments.sql

-- Table-specific indexes for ESTABLISHMENTS
-- Executed only when ESTABLISHMENTS table is migrated

-- Unique index on COD field (correlation key)
CREATE UNIQUE INDEX IF NOT EXISTS idx_establishments_cod ON ESTABLISHMENTS(COD);

-- Geographic location indexes (very frequent queries)
CREATE INDEX IF NOT EXISTS idx_establishments_state ON ESTABLISHMENTS(state);
CREATE INDEX IF NOT EXISTS idx_establishments_city ON ESTABLISHMENTS(city_code);
CREATE INDEX IF NOT EXISTS idx_establishments_zip ON ESTABLISHMENTS(zip_code);

-- Correlation with other tables
CREATE INDEX IF NOT EXISTS idx_establishments_company ON ESTABLISHMENTS(company_cod);
CREATE INDEX IF NOT EXISTS idx_establishments_activity ON ESTABLISHMENTS(main_activity_code);

Execution Control

# Execute scripts automatically (default)
python migrator.py my_database.db

# Skip script execution  
python migrator.py my_database.db --skip-post-scripts

# Table-specific execution (only relevant scripts)
python migrator.py my_database.db --table COMPANIES --force
# Output: πŸ”§ Executing post-migration scripts...
#         πŸ“œ Executing indexes_companies.sql...
#         βœ… indexes_companies.sql executed successfully

# Multiple tables (executes scripts for each table)
python migrator.py my_database.db --table COMPANIES,PRODUCTS --force  
# Output: πŸ”§ Executing post-migration scripts...
#         πŸ“œ Executing indexes_companies.sql...
#         πŸ“œ Executing indexes_products.sql...
#         βœ… Scripts executed: indexes_companies.sql, indexes_products.sql

# In case of script error, asks to continue:
# πŸ“œ Executing indexes_companies.sql...
#   ⚠️  Error in statement 2: Table 'companies' doesn't exist
#   πŸ’¬ SQL: CREATE INDEX idx_company_name ON companies(name)...
#   Continue executing indexes_companies.sql? (y/N):

Performance Benefits

For Large Tables (1+ billion records):

# OLD BEHAVIOR: Always executes all scripts
python migrator.py gov_data.db --table SMALL_TABLE --force
# ❌ Would execute indexes for ALL tables (slow!)

# NEW BEHAVIOR: Only relevant scripts  
python migrator.py gov_data.db --table SMALL_TABLE --force
# βœ… Executes only indexes_small_table.sql (fast!)

Real-world Example:

# Migrate only tax classification table (small)
python migrator.py revenue.db --table TAX_CODES --force
# Executes: indexes_tax_codes.sql (< 1 second)
# Skips: indexes for COMPANIES table (would take hours!)

# Migrate companies table specifically  
python migrator.py revenue.db --table COMPANIES --force
# Executes: indexes_companies.sql (optimized for this table only)

# Complete migration
python migrator.py revenue.db --force
# Executes: General scripts for all tables

Parameters

Parameter Description Default
sqlite_db Path to SQLite database (required) -
-h, --help Show help with all parameters -
--config MySQL configuration file mysql_config.ini
--schema-only Migrate schema only, no data False
-f, --force No confirmations, always replace existing tables False
--table Migrate specific table(s) - accepts comma-separated None
--table-info Show comparative information for a specific table None
--table-compare Compare all tables between SQLite and MySQL False
--info-sqlite Show SQLite database general information False
--info-mysql Show MySQL database general information False
--analyze-types Analyze real data to optimize column types False
--strict-sizing Use exact sizes without safety margin (controlled data only) False
--batch-size Batch size for insertion 1000
--create-config Create example configuration file -
--skip-post-scripts Skip post-migration scripts (.sql) execution False

Migration Process

1. Validation

  • Verify SQLite file existence
  • Test connectivity with both databases
  • Validate configuration file

2. Analysis

  • List all SQLite tables
  • Check MySQL conflicts (existing tables)
  • Map SQLite β†’ MySQL data types

3. MySQL Optimization

During data insertion, the system:

  • Disables FOREIGN_KEY_CHECKS
  • Disables UNIQUE_CHECKS
  • Uses manual transactions (AUTOCOMMIT = 0)

4. Migration

  • Create table structure (columns, PKs, indexes)
  • Migrate data in configurable batches
  • Restore default MySQL settings

5. Post-Migration Scripts

  • Check for valid .sql files
  • Execute custom indexes
  • Create views and procedures
  • Apply specific optimizations

Data Type Optimization

How it works

The program can analyze real data in SQLite columns to suggest more efficient MySQL types:

  • No optimization: All TEXT columns remain as TEXT
  • With --analyze-types: Analyzes only "indexable" fields defined in indexable_fields.txt
  • With --strict-sizing: Uses near-exact sizes (ideal for controlled data)

Optimization modes

Mode Recommended use Example
Default Fast migration TEXT β†’ TEXT
Conservative Data that may grow TEXT (3 chars) β†’ VARCHAR(20)
Strict Controlled/stable data TEXT (3 chars) β†’ CHAR(4)

Indexable fields configuration

Edit indexable_fields.txt to define which fields should be analyzed:

# Fields that typically have indexes
id
code
cod
email
ssn
tax_id

# Your specific fields
product_code
customer_ref
establishment_code

Performance impact

Example with "code" field (3 digits, 1M records):

Type Index space SELECT speed RAM usage
TEXT ~45MB 850ms High
VARCHAR(100) ~8MB 120ms Medium
CHAR(4) ~1MB 25ms Low

Gain: up to 35x faster in indexed queries!

Logs and Monitoring

Visual Feedback

During execution, the system displays:

  • Prerequisites validation status
  • Progress per table
  • Processed record counters
  • Post-migration script execution
  • Completion confirmations

Error Logging

Errors are automatically logged only when they occur in:

migration_errors_YYYYMMDD_HHMMSS.log

Smart logging system:

  • βœ… No errors: No log file is created
  • ❌ With errors: Detailed log is saved and reported to user

Implemented Optimizations

Performance

  • Batch insertion: Process records in groups (default: 1000)
  • Check disabling: Remove unnecessary validations during insertion
  • Compression: Reduce MySQL network traffic
  • Transactions: Minimize disk I/O
  • Custom scripts: Specific indexes and optimizations post-migration

Robustness

  • Pre-validation: Avoid failures during process
  • Automatic rollback: Undo changes on error
  • Conflict handling: Option to overwrite existing tables
  • Structure preservation: Maintain PKs, FKs, and indexes
  • Controlled execution: Scripts with individual error handling

Usage Examples

Development migration

# Small database, fast migration
python migrator.py dev_app.db --batch-size 500

Production migration (automated)

# Large database, no confirmations for automation
python migrator.py prod_app.db --batch-size 10000 --config prod_mysql.ini --force

Incremental migration

# 1. Check status of all important tables
python migrator.py app.db --table-info users
python migrator.py app.db --table-info products
python migrator.py app.db --table-info orders

# 2. Migrate only outdated tables (without asking)
python migrator.py app.db --table users --force
python migrator.py app.db --table products --force

# 3. Confirm synchronization
python migrator.py app.db --table-info users

Migration with custom scripts

# 1. Basic migration (creates general templates)
python migrator.py app.db --table users

# 2. Create table-specific script
cat > indexes_users.sql << EOF
-- Table-specific indexes for users table
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_cod ON users(COD);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_status_date ON users(status, created_at);
EOF

# 3. Execute migration with table-specific script
python migrator.py app.db --table users --force

# Expected output:
# πŸ”§ Executing post-migration scripts...
#   πŸ“œ Executing indexes_users.sql...
#     βœ… 3/3 statements executed
#   βœ… indexes_users.sql executed successfully
# βœ… Scripts executed: indexes_users.sql

# 4. Migrate different table (won't execute users scripts)
python migrator.py app.db --table products --force
# Only executes indexes_products.sql (if exists)

Large-scale migration workflow

# 1. Create table-specific scripts for critical tables
cat > indexes_companies.sql << EOF
CREATE UNIQUE INDEX IF NOT EXISTS idx_companies_cod ON COMPANIES(COD);
CREATE INDEX IF NOT EXISTS idx_companies_status ON COMPANIES(status);
EOF

cat > indexes_establishments.sql << EOF  
CREATE UNIQUE INDEX IF NOT EXISTS idx_establishments_cod ON ESTABLISHMENTS(COD);
CREATE INDEX IF NOT EXISTS idx_establishments_state ON ESTABLISHMENTS(state);
CREATE INDEX IF NOT EXISTS idx_establishments_city ON ESTABLISHMENTS(city_code);
EOF

# 2. Migrate small/medium tables with specific optimizations
python migrator.py revenue.db --table TAX_CODES,CITIES,STATES --force
# Fast execution: only relevant scripts

# 3. Migrate large tables individually with their specific indexes
python migrator.py revenue.db --table COMPANIES --force
# Executes: indexes_companies.sql only

python migrator.py revenue.db --table ESTABLISHMENTS --force  
# Executes: indexes_establishments.sql only

# 4. Complete migration for remaining tables
python migrator.py revenue.db --force
# Executes: General scripts for any remaining tables

Analysis and monitoring

# 1. Analyze SQLite database before migration
python migrator.py app.db --info-sqlite

# 2. Check current MySQL state
python migrator.py app.db --info-mysql

# 3. Compare all tables at once
python migrator.py app.db --table-compare

# 4. Check specific tables
python migrator.py app.db --table-info users
python migrator.py app.db --table-info products

# 5. Migrate with type optimization (conservative mode)
python migrator.py app.db --table users,products --analyze-types --force

# 6. Migrate with strict optimization (controlled data)
python migrator.py app.db --table BRANCH_OFFICE --analyze-types --strict-sizing --force

Workflow with table-specific scripts

# 1. First migration (creates general + specific templates)
python migrator.py app.db --table users

# 2. Customize table-specific indexes for performance
cat > indexes_users.sql << EOF
-- Indexes for frequent queries on users table
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_cod ON users(COD);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_status_date ON users(status, created_at);
CREATE INDEX IF NOT EXISTS idx_users_company ON users(company_id, active);
EOF

# 3. Create specific views for users table
cat > views_users.sql << EOF
-- Views specific to users table
CREATE VIEW v_active_users AS
SELECT u.*, 
       COUNT(o.id) as total_orders,
       SUM(o.amount) as total_amount
FROM users u
LEFT JOIN orders o ON u.COD = o.user_cod
WHERE u.active = 1
GROUP BY u.COD;
EOF

# 4. Execute migration with table-specific scripts
python migrator.py app.db --table users --force

# Output:
# πŸ”§ Executing post-migration scripts...
#   πŸ“œ Executing indexes_users.sql...
#     βœ… 3/3 statements executed
#   βœ… indexes_users.sql executed successfully  
#   πŸ“œ Executing views_users.sql...
#     βœ… 1/1 statements executed
#   βœ… views_users.sql executed successfully
# βœ… Scripts executed: indexes_users.sql, views_users.sql

# 5. Migrate different table (independent scripts)
python migrator.py app.db --table products --force
# Only executes product-specific scripts (if they exist)

# 6. Check results
python migrator.py app.db --info-mysql

Time monitoring

# During migration, each table shows elapsed time:
# πŸ“Š Processing table: users
#   πŸ“‹ Loaded 25 indexable fields from indexable_fields.txt
#     Analyzing indexable field: email
#       Data: 50000 records, size 12-45, CONSERVATIVE mode, suggested: 68
#   Creating structure...
#   Migrating 50,000 records...
#   βœ… 50,000 records migrated
#   βœ… Table users completed (15s)
# 
# πŸ”§ Executing post-migration scripts...
#   πŸ“œ Executing indexes.sql...
#     βœ… 3/3 statements executed
#   βœ… indexes.sql executed successfully
# βœ… Scripts executed: indexes.sql
#
# πŸŽ‰ Migration completed successfully!

Automated scripts

# Nightly complete migration with scripts
python migrator.py backup_$(date +%Y%m%d).db --force --batch-size 5000

# Optimized migration for government data
python migrator.py gov_data.db --table ESTABLISHMENTS,COMPANIES --analyze-types --strict-sizing --force

# Daily status report
echo "=== SQLite ===" > report.txt
python migrator.py app.db --info-sqlite >> report.txt
echo "=== MySQL ===" >> report.txt  
python migrator.py app.db --info-mysql >> report.txt
echo "=== Comparison ===" >> report.txt
python migrator.py app.db --table-compare >> report.txt

Troubleshooting

Error: "Configuration file not found"

python migrator.py --create-config
# Edit mysql_config.ini with your credentials

Error: "MySQL connection failed"

  • Check credentials in .ini file
  • Confirm MySQL server is running
  • Test network connectivity

Slow performance

  • Increase --batch-size for large databases
  • Check if compression is enabled
  • Monitor MySQL server resources

Post-migration scripts failing

# View error details in log
cat migration_errors_*.log

# Test script separately
mysql -u user -p database < indexes.sql

# Skip scripts temporarily
python migrator.py app.db --skip-post-scripts

Table replacement confirmation

By default, the system asks before replacing existing tables:

python migrator.py app.db
# ⚠️  Tables already exist in MySQL: users, products
# Continue anyway? (y/N):

Use --force for automation:

python migrator.py app.db --force
# ⚠️  Tables already exist in MySQL: users, products  
# πŸš€ Force mode active: replacing tables automatically

Table not found with --table

python migrator.py app.db --table wrong_name
# ❌ Table(s) not found: wrong_name
# πŸ“‹ Available tables: users, products, orders

Sync verification

python migrator.py app.db --table-info users
# Possible outputs:
# βœ… Tables synchronized
# ⚠️ SQLite has 70 more records
# ❌ Table needs migration

Limitations

  • Foreign Keys: Created only in initial structure, not recreated after optimization
  • Triggers: Not migrated (DBMS-specific)
  • Views: Migrated via custom post-migration scripts
  • Procedures/Functions: Migrated via custom post-migration scripts

Code Structure

migrator.py
β”œβ”€β”€ SQLiteToMySQLMigrator
β”‚   β”œβ”€β”€ __init__()                     # Initial configuration
β”‚   β”œβ”€β”€ validate_prerequisites()        # Pre-migration validation
β”‚   β”œβ”€β”€ connect_databases()            # Establish connections
β”‚   β”œβ”€β”€ get_sqlite_tables()            # List SQLite tables
β”‚   β”œβ”€β”€ parse_table_parameter()        # Process --table parameter
β”‚   β”œβ”€β”€ get_table_schema()             # Extract table structure
β”‚   β”œβ”€β”€ map_sqlite_to_mysql_type()     # Map data types
β”‚   β”œβ”€β”€ load_indexable_fields()        # Load indexable fields
β”‚   β”œβ”€β”€ analyze_text_column()          # Analyze columns for optimization
β”‚   β”œβ”€β”€ create_mysql_table()           # Create table in MySQL
β”‚   β”œβ”€β”€ optimize_mysql_for_insert()    # Performance optimizations
β”‚   β”œβ”€β”€ migrate_table_data()           # Migrate data in batches
β”‚   β”œβ”€β”€ execute_post_migration_scripts() # Execute .sql scripts
β”‚   β”œβ”€β”€ has_valid_sql_content()        # Check valid SQL content
β”‚   β”œβ”€β”€ execute_sql_script()           # Execute individual script
β”‚   β”œβ”€β”€ create_sample_post_migration_files() # Create templates
β”‚   β”œβ”€β”€ show_table_info()              # Compare records between databases
β”‚   β”œβ”€β”€ show_sqlite_info()             # SQLite general information
β”‚   β”œβ”€β”€ show_mysql_info()              # MySQL general information
β”‚   β”œβ”€β”€ compare_all_tables()           # Compare all tables
β”‚   β”œβ”€β”€ format_duration()              # Format execution time
β”‚   └── migrate()                      # Main orchestrator
└── main()                             # CLI interface

Development

To contribute or customize:

  1. Clone the repository
  2. Install dependencies: pip install mysql-connector-python
  3. Run tests with sample databases
  4. Submit PRs with improvements

Exit Codes

The program returns specific codes for different situations, enabling script integration:

Code Situation
0 Success
1 General migration error
2 SQLite file not found
3 MySQL connection error
4 Unexpected error

Script usage examples

Windows Batch:

python migrator.py database.db --table users
if %ERRORLEVEL% EQU 0 (
    echo Migration completed successfully
) else if %ERRORLEVEL% EQU 2 (
    echo File not found
) else if %ERRORLEVEL% EQU 3 (
    echo MySQL connection error
) else (
    echo Migration error
)

Linux/Mac Bash:

python migrator.py database.db --table users
case $? in
    0) echo "Migration completed successfully" ;;
    2) echo "File not found" ;;
    3) echo "MySQL connection error" ;;
    *) echo "Migration error" ;;
esac

PowerShell:

python migrator.py database.db --table users
switch ($LASTEXITCODE) {
    0 { Write-Host "Migration completed successfully" }
    2 { Write-Host "File not found" }
    3 { Write-Host "MySQL connection error" }
    default { Write-Host "Migration error" }
}

Changelog - Version 1.4

New Features

  • Table-specific post-migration scripts: Intelligent system for executing scripts only for migrated tables
  • Performance optimization: Avoid unnecessary script execution on large tables (1+ billion records)
  • Smart script selection: Automatically chooses table-specific vs. general scripts based on migration scope
  • Lazy logging: Log files created only when there are actual errors
  • Enhanced templates: Creation of both general and table-specific example files
  • --skip-post-scripts parameter: Optional control to skip script execution

Table-Specific Script Patterns

  • indexes_[table].sql / [table]_indexes.sql - Table-specific indexes
  • views_[table].sql / [table]_views.sql - Table-specific views
  • procedures_[table].sql / [table]_procedures.sql - Table-specific procedures

General Script Files (Fallback)

  • post_migration.sql - General configurations and optimizations
  • indexes.sql - General indexes for multiple tables
  • views.sql - General views for reports and complex queries
  • procedures.sql - General stored procedures and functions

Script Execution Logic

  • Single table migration β†’ Execute only table-specific scripts
  • Multiple table migration β†’ Execute scripts for each migrated table
  • Complete migration (>5 tables) β†’ Execute general scripts
  • No relevant scripts β†’ Silent execution (no unnecessary messages)

Logging Improvements

  • Smart system: Log created only when necessary
  • Automatic cleanup: Removes empty files on successful executions
  • Clean feedback: No unnecessary messages when there are no errors

Performance Benefits

  • Massive improvement for large databases with selective table migration
  • Eliminates script execution on irrelevant tables
  • Optimizes CI/CD pipelines with frequent single-table updates
  • Reduces migration time from hours to seconds for specific table updates

Real-world Impact

# OLD: Migrating 1 small table executed scripts for ALL tables
python migrator.py huge_db.db --table CODES --force
# ❌ Previously: 30+ minutes (executed scripts for billion-record tables)

# NEW: Only relevant scripts executed  
python migrator.py huge_db.db --table CODES --force
# βœ… Now: < 1 minute (only codes-specific scripts)

Migration Patterns

  1. Development: Table-specific scripts facilitate rapid iteration
  2. Production: Large tables get optimized indexes without affecting others
  3. CI/CD: Selective deployments execute only relevant optimizations
  4. Maintenance: Independent table updates with isolated script execution

Use Cases

  • Government databases: Tax records, company registrations (billion+ records)
  • E-commerce platforms: Product catalogs, user data, transaction logs
  • Analytics systems: Event tracking, user behavior, reporting tables
  • Legacy migrations: Gradual migration of large legacy systems

License

MIT License

Copyright (c) 2025 Vailton Renato

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages