Intelligent SQL migration squashing and optimization tool for SQLite databases. Transforms complex migration histories with redundant operations into clean, optimized files.
Transform migration chaos like this:
001_baseline.sql β CREATE TABLE users (id, name)
002_add_email.sql β ALTER TABLE users ADD COLUMN email
003_drop_users.sql β DROP TABLE users
004_recreate_users.sql β CREATE TABLE users (id, name, email, phone)
005_add_more_fields.sql β ALTER TABLE users ADD COLUMN status
Into clean, optimized migrations:
001_squashed_schema.sql β CREATE TABLE users (id, name, email, phone, status)
- π Smart Analysis: Detects CREATE/DROP cycles, redundant ALTERs, and optimization opportunities
- ποΈ SQLite Optimized: Handles backup tables, virtual tables (FTS), triggers, and SQLite-specific patterns
- πΈ SQL Dump Baseline: Generate clean baseline migrations from SQL dumps (v0.2.0+)
- β Database Validation: Creates actual test databases to verify schema correctness
- π Advanced Comparison: Element-by-element schema comparison with detailed reporting
- π¨ Rich CLI: Beautiful command-line interface with progress indicators and colorized output
- π‘οΈ Safe Operations: Backup support and validation before applying changes
Real-world example from MCP-Hive project:
- Before: 23 migration files, 316 SQL statements
- After: 1 migration file, 152 SQL statements
- Optimization: 62% reduction in complexity
- Validation: 100% schema accuracy verified
pip install migration-squashgit clone https://github.com/your-username/migration-squash.git
cd migration-squash
# Option 1: Install as global tool (like npm link)
uv tool install --editable .
# Option 2: Development environment
uv sync
uv run migration-squash analyze /path/to/migrations
# Option 3: Editable install in current environment
uv pip install --editable .# 1. Analyze your migrations (safe, read-only)
migration-squash analyze /path/to/migrations
# 2. Generate squashed migrations
migration-squash squash /path/to/migrations --output-dir ./output
# 3. Validate with database comparison
migration-squash compare /path/to/migrations ./output# Preview changes without writing files
migration-squash squash /path/to/migrations --dry-run
# Keep data migrations separate from schema
migration-squash squash /path/to/migrations --keep-data --backup
# Save detailed comparison report
migration-squash compare original/ squashed/ --save-report report.jsonShows optimization opportunities and patterns detected:
- Tables with CREATE/DROP cycles
- Redundant ALTER operations
- SQLite backup table patterns
- Virtual table and trigger detection
Creates clean, consolidated migration files:
- Merges ALTER statements into CREATE tables
- Eliminates redundant operations
- Preserves dependency ordering (tables β virtual tables β triggers)
- Handles SQLite-specific patterns
Create a single baseline migration from your entire migration history:
- Uses SQLite's native
.dumpcommand for accurate schema extraction - Perfect for consolidating 15-50+ migrations into one baseline
- Supports schema-only or schema+data dumps
- Intelligent cleaning and organization of dump output
# Basic usage - schema only
migration-squash dump-baseline /path/to/migrations
# Include data in baseline
migration-squash dump-baseline /path/to/migrations --include-data
# Specify output file
migration-squash dump-baseline /path/to/migrations -o baseline.sql
# Organize by dependency order
migration-squash dump-baseline /path/to/migrations --organizeAdvanced validation using real databases:
- Creates SQLite databases (
original.dbandsquashed.db) in the output directory for inspection - Applies all original migrations vs. single squashed migration
- Performs element-by-element schema comparison
- Reports differences in tables, columns, indexes, triggers, virtual tables
- Databases remain available for manual inspection and debugging
- Generates detailed JSON reports
Simple validation for basic schema comparison (legacy command)
| Option | Description |
|---|---|
--dry-run |
Preview operations without writing files |
--keep-data |
Keep data migrations separate from schema |
--output-dir |
Directory for squashed migration output |
--group-by |
Grouping strategy: 'table', 'feature', 'date' |
--backup |
Create timestamped backup of original migrations |
--save-report |
Save detailed comparison report to JSON file |
--verbose |
Show detailed analysis information |
src/
βββ cli.py # Rich CLI interface with progress bars
βββ analyzer.py # Migration parsing and analysis engine
βββ squasher.py # Smart optimization and merging logic
βββ database_comparator.py # Advanced database schema comparison
βββ validator.py # Basic schema validation
βββ sqlite_patterns.py # SQLite-specific pattern detection
βββ models.py # Data structures and types
Identifies tables that are created, dropped, and recreated, then merges all ALTER operations into the final CREATE statement.
Analyzes ALTER TABLE operations and merges ADD COLUMN statements into CREATE TABLE definitions while preserving constraints and relationships.
- Detects backup table patterns (
*_backup,*_temp,*_old) - Handles virtual tables (FTS) with proper dependency ordering
- Preserves trigger definitions and relationships
Creates actual SQLite databases to verify that squashed migrations produce identical schemas to the original migration sequence.
The tool has been tested on:
- Complex migration histories (20+ files)
- CREATE/DROP/CREATE cycles
- SQLite virtual tables and triggers
- Foreign key relationships
- Mixed schema and data migrations
Built with modern Python practices:
- UV package manager for fast dependency management
- Rich library for beautiful CLI output
- Type hints and dataclasses for maintainability
- Modular architecture for extensibility
This project is licensed under the MIT License - see the LICENSE file for details.