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

7.6k views Asked by At

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?

4

There are 4 answers

0
ceejayoz On

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.

It's not enormously ideal, but after running them you can adjust the batch values on each migration in the database table to be separate numbers. php artisan migrate:rollback takes the MAX() batch value and rolls all of its migrations back.

1
Rob Derks On

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.

0
Joey Rich On

I don't know of a way to migrate forward one at a time. But you can roll migrations back one at a time like this:

php artisan migrate:rollback --step=1
7
Colin On

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.