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 →
Laravel Truss: Live Interactive Database ER Diagrams image

Laravel Truss: Live Interactive Database ER Diagrams

Read article
Laravel artisan dev: Run Server, Queue, Logs, and Vite image

Laravel artisan dev: Run Server, Queue, Logs, and Vite

Read article
Validate and Convert HEIC Images in Laravel image

Validate and Convert HEIC Images in Laravel

Read article
Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues image

Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues

Read article
Reject Unexpected Array Keys with Laravel Validation image

Reject Unexpected Array Keys with Laravel Validation

Read article
Major performance improvements & security patches for Filament v4.12 and v5.7! image

Major performance improvements & security patches for Filament v4.12 and v5.7!

Read article