Skip to main content

Squashing Migrations

As your application grows, you may accumulate hundreds of migration files. Squashing compresses all migrations into a single SQL schema file, speeding up test database creation and simplifying your migrations directory.

Creating a Schema Dump

dart run ormed schema:dump

This exports your current database schema to a SQL file (default: database/schema.sql).

Pruning Migration Files

After creating a dump, optionally delete all migration files:

dart run ormed schema:dump --prune

This:

  1. Creates the schema dump
  2. Deletes all migration files
  3. Future migrations apply on top of the schema dump
caution

Only use --prune after ensuring:

  • The schema dump was successfully created
  • All team members have pulled the latest changes
  • Your CI/CD pipeline is updated

How It Works

When running migrations:

  1. If ledger is empty (fresh database):

    • Load schema from dump file (if exists)
    • Apply any migrations created after the dump
  2. If ledger has entries (existing database):

    • Normal migration flow
    • Schema dump is ignored

Database-Specific Dumps

For multiple connections, create separate dumps:

# Dump default database
dart run ormed schema:dump

# Dump testing database
dart run ormed schema:dump --database testing

Custom Output Path

dart run ormed schema:dump --path database/schema/production.sql

Configuration

migrations:
path: lib/src/database/migrations
registry: lib/src/database/migrations/registry.dart
ledger_table: _orm_migrations
schema_dump: database/schema.sql # Schema dump location

Best Practices

  • Commit schema files to version control
  • Create dumps periodically (e.g., before major releases)
  • Test first in dev/staging before pruning in production
  • Document when dumps were created and which migration they represent

Database Support

DatabaseMethod
SQLiteNative dump/restore
PostgreSQLpg_dump
MySQLmysqldump

The CLI automatically uses the appropriate database-specific tools.