Home / Laravel / Detect and Resolve Laravel Schema Drift with MigrAlign

Detect and Resolve Laravel Schema Drift with MigrAlign

MigrAlign is a Laravel package that finds the differences between your migration files and your actual database, then applies the changes with safeguards based on how risky each change is. It targets schema drift — the gap that opens up when teams edit tables directly, skip a migration, or run divergent environments.

How It Works

MigrAlign reads the intent from your migration files by scanning Schema::create() and Schema::table() calls, then introspects the live schema from information_schema on MySQL or MariaDB. The diff between the two becomes a set of proposed changes. You can preview everything before anything runs:

php artisan migralign:sync --dry-run

Risk Classification

Rather than applying every change the same way, MigrAlign sorts each change into one of three categories:

  • Safe: adding nullable columns, widening column sizes, and metadata-only changes.
  • Risky: shrinking a type, switching a column from nullable to not-null, or contracting an enum.
  • Destructive: dropping columns or tables.

Safe changes are applied automatically when auto_apply_safe is enabled. Risky and destructive changes prompt for confirmation before MigrAlign touches the database, so a forgotten drop doesn’t run unattended.

Targeted Syncing

You can scope a sync to a single table or migration instead of the whole schema, which is useful when you only need to reconcile one area:

# Sync a specific table
php artisan migralign:sync --table=users
 
# Sync changes from a specific migration
php artisan migralign:sync --migration=2024_01_01
 
# Apply all changes without prompts
php artisan migralign:sync --force

After a run, MigrAlign reports each change as applied, skipped, pending, or errored.

Installation and Configuration

Install the package via Composer and publish the config file:

composer require migralign/laravel-migralign
php artisan vendor:publish --tag=migralign-config

The published config controls which migrations path is read, which tables to ignore, and whether safe changes apply automatically:

return [
'migrations_path' => database_path('migrations'),
'ignored_tables' => ['migrations', 'sessions', 'jobs', 'failed_jobs'],
'auto_apply_safe' => true,
'connection' => null,
];

MigrAlign requires PHP 8.2+ (8.3+ on Laravel 13) and supports Laravel 11, 12, and 13. It works with MySQL and MariaDB only, and migrations with highly dynamic logic may still need manual review.

You can learn more and view the source on GitHub.

Source: https://laravel-news.com

Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *