August 20th, 2021  |  Victor Paredes

Rolling Back Database Migrations in Laravel

One of the most annoying issues to encounter is code that works great on staging and dev but breaks on Production. No matter how hard we try to create the perfect devOps environment, there will always be slight differences. This is the screen that I faced earlier this morning:

This was just after pushing some code—which worked well in my dev environment—that included several database migrations, so I knew that it had to do with this. Sure enough, there was an SQL error:

Illuminate\Database\QueryException

SQLSTATE[42000]: Syntax error or access violation: 1101 BLOB, TEXT, GEOMETRY or JSON column 'content' can't have a default value

Databases have always seemed delicate to me, and because of this they feel dangerous to meddle with. Databases don't have to be scary, especially in Laravel. Why? Because there is a handy artisan feature that helps correct recent mistakes and, hopefully, will inspire some peace of mind when working with your database.

This tool is called php artisan migrate:rollback, which will undo the effects of the previous migration. The rollback feature can go back as far as the beginning of time; simply keep typing the command, or you can rollback your database a specific number of migrations:

php artisan migrate:rollback —step=3    // rollback 3 migrations 

After rolling back your migrations to the desired point, simply delete any latter migrations, update the now-current migration file to fix your mistake, and then use artisan to migrate again.

 php artisan migrate 

More information about Laravel's rollback feature and its supported commands can be found in the official docs.

Meet the Author
 

Victor Paredes is a web and print designer. When not hard at work, you can find him tinkering around with code, music, artwork, and archaic poetic structures. He is based in Portland, Oregon.
 
Close