We ran migrate:fresh against production by mistake: what happened and the rule we made

From our own projects · Published 29 November 2026

This is an incident write-up from one of our own Laravel applications. We ran php artisan migrate:fresh --database=pgsql --force inside the production container while verifying a patch. The command was meant for a throwaway test database. It dropped and recreated the production schema. We restored from a backup taken earlier the same day. This page covers why it happened, what we lost, and the rules that would have stopped it. We leave out customer and campaign details.

What went wrong

The patch was tested against PostgreSQL. The PHPUnit runs were isolated: phpunit.xml.dist overrides the database connection for tests, so a test run never touches the real database. We assumed the same protection covered an artisan command we ran by hand to prepare a test database.

It does not. The <env> overrides in phpunit.xml.dist apply only when PHPUnit runs. php artisan reads the real environment of the container it runs in. Inside the production container, that environment points at the production database. migrate:fresh drops every table and runs all migrations again, and --force skips the confirmation Laravel asks for in production.

So the command did exactly what it was told, on the wrong database.

The timeline, in outline

  • A full pg_dump backup had been taken earlier that morning, as routine pre-work.
  • The campaign scheduler kept sending in the background while the work went on.
  • migrate:fresh ran against the production connection.
  • We restored from the morning dump, checked that the tables and row counts matched what we expected, and restarted the queue workers so that no half-finished send chain could continue against the missing rows.
  • We took a fresh dump right after the restore.

The restore itself was quick, because the backup was recent and the format was a standard custom-format dump.

What we lost

Everything written between the backup and the wipe. That was a few hours of records: campaign configuration created in that window, message records, send outcomes and engagement events. Some of the emails had already been delivered, so the recipients got them, but our database no longer knew about them. A recipient list that existed only in the database for that window could not be recovered, because the logs held SMTP message IDs and not addresses.

Nothing outside that window was affected. The backup was the reason that was true.

The rule we wrote down

Destructive schema testing happens only in dedicated throwaway containers with their own environment files. Never run artisan, and especially never migrate:fresh or an explicit --database flag, against the production container’s environment.

Guardrails that would have stopped it

We wrote the rule above after the incident. The items below are what we recommend adding to any Laravel app that has a production database. We have not verified that every item is in place in our own app, so check yours.

  1. Block destructive commands in production in code. Laravel provides DB::prohibitDestructiveCommands(). Call it in a service provider with $this->app->isProduction(). It makes migrate:fresh, migrate:refresh, migrate:reset and db:wipe refuse to run in production, even with --force.

    // app/Providers/AppServiceProvider.php
    use Illuminate\Support\Facades\DB;
    
    public function boot(): void
    {
        DB::prohibitDestructiveCommands($this->app->isProduction());
    }
    

    This only helps if the container reports APP_ENV=production. If your production container has another value, the guard is off.

  2. Do not keep a shell with production credentials open for testing. Run test databases in their own containers with their own .env, started for the test and removed after.

  3. Use a separate database user for the app with no DROP rights on the schema if your setup allows it, and run migrations with a different user.

  4. Take a backup right before risky work and know how to restore it. Practise the restore. Ours worked because the dump was recent and the procedure was known.

  5. Stop the scheduler and queue workers before a restore. Otherwise jobs run against a half-restored database. We restarted the queue workers after the restore so that no orphaned job kept running.

  6. Print the target before you act. php artisan db:show shows the connection the command would use. Look at it before any destructive command.

Checking your own setup

php artisan about --only=environment
php artisan db:show

If the output names your production database from a place you thought was a test shell, stop and find out why before you run anything else.

Our network