News

Detect and Resolve Laravel Schema Drift with MigrAlign

Published
Detect and Resolve Laravel Schema Drift with MigrAlign image

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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article