tarunkorat/laravel-migration-squasher

Squash old Laravel migrations into a single schema file

Fund package maintenance!
tarunkorat

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 0

Forks: 0

Open Issues: 1

pkg:composer/tarunkorat/laravel-migration-squasher

v1.0.0 2025-10-30 07:14 UTC

This package is auto-updated.

Last update: 2025-11-30 12:16:10 UTC


README

Latest Version on Packagist Total Downloads License

Squash old Laravel migrations into a single schema file to keep your migrations folder clean and manageable.

Fully compatible with Laravel 8, 9, 10, 11, and 12!

โœจ Features

  • ๐Ÿ—œ๏ธ Squash old migrations into a single schema file
  • ๐Ÿ“… Keep recent migrations separate for easy tracking
  • ๐Ÿ”’ Safe backup before any deletion
  • ๐Ÿš€ Faster migrations - reduce migrate:fresh time by 90%
  • ๐ŸŽฏ Laravel 8-12 compatible - works with all modern Laravel versions
  • ๐Ÿ” Dry run mode - preview changes before applying
  • ๐Ÿ›ก๏ธ Database agnostic - works with MySQL, PostgreSQL, SQLite, SQL Server
  • ๐Ÿ“Š Clean codebase - reduce migration clutter from 100+ to just a few files

๐Ÿ“‹ Requirements

  • PHP 8.0 or higher
  • Laravel 8.x, 9.x, 10.x, 11.x, or 12.x
  • Doctrine DBAL (automatically installed)

๐Ÿ“ฆ Installation

Install the package via composer:

composer require tarunkorat/laravel-migration-squasher

The package will automatically register itself via Laravel's package discovery.

Publish Configuration (Optional)

php artisan vendor:publish --provider="TarunKorat\LaravelMigrationSquasher\MigrationSquasherServiceProvider"

This will create config/migration-squasher.php file.

๐Ÿš€ Usage

Basic Usage

Squash all migrations before a specific date, keeping the 10 most recent:

php artisan migrations:squash

Preview Changes (Dry Run)

See what would be squashed without making any changes:

php artisan migrations:squash --dry-run

Custom Options

# Squash migrations before specific date
php artisan migrations:squash --before=2024-01-01

# Keep specific number of recent migrations
php artisan migrations:squash --keep=15

# Disable backup creation
php artisan migrations:squash --no-backup

# Delete migration records from database
php artisan migrations:squash --delete-records

# Combine multiple options
php artisan migrations:squash --before=2023-06-01 --keep=20 --dry-run

โš™๏ธ Configuration

The config file provides several options:

return [
    // Date before which migrations will be squashed
    'squash_before' => env('MIGRATION_SQUASH_BEFORE', '2024-01-01'),

    // Number of recent migrations to keep separate
    'keep_recent' => (int) env('MIGRATION_SQUASH_KEEP_RECENT', 10),

    // Name of the generated schema dump file
    'schema_file' => '0000_00_00_000000_schema_dump.php',

    // Create backup of squashed migrations
    'backup_migrations' => env('MIGRATION_SQUASH_BACKUP', true),

    // Backup directory path
    'backup_path' => env('MIGRATION_SQUASH_BACKUP_PATH', 'migrations/backup'),

    // Tables to exclude from schema dump
    'excluded_tables' => [
        'migrations',
    ],

    // Delete migration records from database
    'delete_batch_records' => false,
];

๐Ÿ“Š Before & After Example

Before Squashing

database/migrations/
โ”œโ”€โ”€ 2022_01_15_100000_create_users_table.php
โ”œโ”€โ”€ 2022_01_15_100001_create_password_resets_table.php
โ”œโ”€โ”€ 2022_02_20_140000_create_posts_table.php
โ”œโ”€โ”€ 2022_02_20_140001_add_slug_to_posts_table.php
... (96 more old files)
โ”œโ”€โ”€ 2024_10_15_140000_add_thumbnail_to_posts_table.php
โ”œโ”€โ”€ 2024_10_20_150000_create_notifications_table.php
โ”œโ”€โ”€ 2024_10_25_160000_add_verified_at_to_users_table.php

Total: 100 migration files ๐Ÿ˜ฐ

After Squashing

database/migrations/
โ”œโ”€โ”€ 0000_00_00_000000_schema_dump.php          โญ (Entire schema in ONE file)
โ”œโ”€โ”€ 2024_10_15_add_thumbnail_to_posts.php      (Recent - Kept)
โ”œโ”€โ”€ 2024_10_20_create_notifications.php        (Recent - Kept)
โ”œโ”€โ”€ 2024_10_25_add_verified_at_to_users.php    (Recent - Kept)

Total: 4 migration files ๐ŸŽ‰

๐ŸŽฏ When to Use This Package

โœ… Perfect For:

  • Long-running projects (1+ years old with 50+ migrations)
  • Performance optimization (speed up migrate:fresh by 90%)
  • Team onboarding (new developers see clean migration history)
  • CI/CD pipelines (faster test suite execution)
  • Before major releases (clean slate for v2.0)

โŒ Not Recommended For:

  • New projects (< 6 months old with few migrations)
  • Active feature branches (wait until merged to avoid conflicts)
  • Projects that never run migrate:fresh

๐Ÿ”ง How It Works

  1. Analyzes all migrations in your project
  2. Identifies migrations older than specified date
  3. Keeps the most recent N migrations separate
  4. Generates a schema dump from your current database structure
  5. Creates backup of squashed migrations (optional)
  6. Deletes old migration files
  7. Updates migrations table (optional)

Important Notes

  • โœ… Your database stays exactly the same - no data is modified
  • โœ… Only migration files are affected
  • โœ… Always creates backups before deletion (unless disabled)
  • โœ… Safe to run - uses dry-run mode first

๐Ÿงช Testing

composer test

๐Ÿ“ˆ Performance Comparison

Metric Before Squash After Squash Improvement
Migration Files 167 files 11 files 93% reduction
migrate:fresh Time 8 minutes 45 seconds 90% faster
CI/CD Pipeline 2 min migrations 30 sec migrations 75% faster
Codebase Clarity Cluttered Clean Much better

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“ Changelog

Please see CHANGELOG for more information on what has changed recently.

๐Ÿ”’ Security

If you discover any security related issues, please email tarunkorat336@gmail.com instead of using the issue tracker.

๐Ÿ“„ License

The MIT License (MIT). Please see License File for more information.

๐Ÿ’ก Credits

๐Ÿ™ Support

If you find this package helpful, please consider:

  • โญ Starring the repository
  • ๐Ÿ› Reporting bugs and issues
  • ๐Ÿ“– Contributing to documentation
  • ๐Ÿ’ฌ Sharing with other Laravel developers

Made with โค๏ธ for the Laravel community