Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Laravel's artisan migrate one step at a time?

I already read Running one specific laravel 4 migration (single file) but this doesn't give me the answer.

I want to know whether there is a way to run the command so that it just executes the next, and just this one migration.

I have got 10 files in my Migrate-Folder. 7 of them are migrated already. Now I found that while I created the 3 new ones and run the command, they are all executed.

The problem is that in the database 'select * from migrations' they show up in one batch and not in separate ones. This means that if I just want to rollback one step, we are back to step 7 and not 9 - what I want.

This is confusing sometimes as I want to rollback one step at a time and not rollback all the steps of one batch.

I know I could move the files in another folder and just leave one to run migrate. Then move the next one and migrate again but this is very inconvenient - what happens if by accident i move and migrate step 10 before step 9.

Anybody knows an answer to this?

like image 718
hogan Avatar asked Jun 12 '15 19:06

hogan


People also ask

How do I run a particular migration in Laravel?

To run the specific migration in Laravel, you need to use --path option with the php artisan migrate command. Let's take a simple example, we have '2019_12_04_131405_create_payments_table. php' migration in the database/migrations directory and we would like to run this migration.


2 Answers

In laravel 5.4 you can: php artisan migrate --step

When you execute the command like this you can roll-back every migration individually afterwards by using the default "php artisan migrate:rollback" without specifying how many steps to rollback.

like image 98
Rob Derks Avatar answered Oct 09 '22 05:10

Rob Derks


A bit of a hack, but you could run artisan migrate to run all the migrations, then the following SQL commands to make it look like the migrations were run one at a time:

SET @a = 0;  
UPDATE migrations SET batch = @a:=@a+1;

That will change the batch column to 1, 2, 3, 4 .. etc. Add a WHERE batch>=... condition on there (and update the initial value of @a) to only affect certain migrations.

Then you can artisan migrate:rollback as much as is required.

like image 35
Colin Avatar answered Oct 09 '22 04:10

Colin