Hi everyone,
A while ago I shared Laravel Safe Migrations, a package that checks your pending migrations for things that can break production before you run migrate. Thanks for all the feedback last time. Several suggestions from that thread are now in the package.
How it works
It dry-runs your pending migrations in Laravel's pretend mode, collects the SQL they would execute, and checks it against a set of rules. Because it reads the SQL and not your PHP code, it also catches DB::statement(), DB::unprepared() and query builder calls.
php artisan safe-migrations:check ERROR [drop-column] Dropping column [users.legacy] destroys its data, and app servers still running the old code will fail on queries that use it. → Remove every use of the column from the code and deploy that first, then drop the column in a follow-up migration. What it catches
Dropping or renaming tables and columns that the old code still uses during the deploy NOT NULL columns without a default on tables that already have rows Column type changes that rewrite the whole table PostgreSQL: blocking CREATE INDEX, foreign keys without NOT VALID, and SET NOT NULL UPDATE / DELETE without WHERE, and TRUNCATE Tables created earlier in the same batch are ignored, since they have no data yet New since last time
concurrent-in-transaction rule: flags CREATE INDEX CONCURRENTLY in a migration that Laravel wraps in a transaction, which PostgreSQL rejects. The fix is public $withinTransaction = false;. (Suggested in the last thread, thanks!) Side effects are faked during the check: HTTP calls, mail, notifications, queued jobs and dispatch() inside up() no longer actually happen while the migration is analysed. Migrations with their own $connection are now checked against the right database. SQL comments can't hide a statement from the check anymore. Three ways to use it
In CI: safe-migrations:check fails the pull request if a migration is unsafe. In deploys: php artisan migrate:safe --force checks first, then migrates. As a guard: set SAFE_MIGRATIONS_GUARD=true and plain migrate refuses unsafe migrations in production. When a risky change is intentional, mark the migration so the reason stays in code review:
return new #[AllowUnsafe('drop-column', reason: 'unused since v2.3')] class extends Migration { ... }; Honest limitations
Pretend mode returns no data, so a migration that branches on Schema::hasColumn() is checked along the "empty" path. It can't know your table sizes. A warning on a 200-row table may not matter. File and cache writes inside up() still run during the check. Supports Laravel 12/13 on MySQL/MariaDB, PostgreSQL and SQLite. SQL Server isn't supported. Install
composer require hamza-wakrim/laravel-safe-migrations GitHub: https://github.com/Hamza-Wakrim/laravel-safe-migrations
I'd love feedback, especially false positives or problems it missed on your real migrations, and rule ideas from your zero-downtime deploys. MySQL lock detection (instant vs. table-copy changes) is next on my list.