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

4

11

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?

Handspring answered 12/6, 2015 at 19:45 Comment(4)
There's some info here: #19102697 The short version is that migrations weren't built for that purpose.Deanery
That's the exact same link from my question.Handspring
I apologise. I read that too clumsily.Deanery
Sometimes the Laravel framework just doesn't think things all the way through. The DEFAULT behaviour should be to migrate only one file at a time. If you want to migrate more, only THEN should you have to specify the number of steps to carry forward. Laravel is great in so many ways, but it sometimes has a tendency to treat databases as an afterthought.Lesbos
D
17

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.

Dunning answered 27/5, 2017 at 11:43 Comment(1)
This is somehow helpful, but I doubt whether it actually answers the question. Running the command migrates all the migrations, but one at a time rather than as a batch. But the question seemed to ask how to run just one migration file and just stop there (leaving the others unmigrated)Castera
J
2

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
Joijoice answered 14/10, 2021 at 19:12 Comment(0)
O
0

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.

Opener answered 24/8, 2015 at 21:14 Comment(7)
too much hassle, since copying one file after another into the folder is faster i go with this one.Handspring
Well, how much of a hassle it is ... that's your call. Running two SQL commands seems faster than manually copying files. In any case, I would humble suggest that the SQL is a "better" way of doing it, in terms of actually solving the problem, (and certainly doesn't deserve a downvote).Opener
Wasn't my downvote. I rather wanted to know if there is a command option. Don't worry more, the question is answered.Handspring
With laravel 5.3 , you will be able to do this: php artisan migrate:rollback --step=2 (set how many steps will be rolled back)Furlong
@Arda: Yup. I think I made the initial PR to add --step to the core. :)Opener
@Opener Ah, great, didn't know about that, thanks man! It'll be a life saver!Furlong
You have to be careful with a solution like this. I agree that the principle can work (if the developer has access to the database). However, the specific SQL suggested doesn't take account of the fact that previous migrations might already exist in the table (which we may not want to override). And worse, without an ORDER BY clause, you may end up re-ordering stuff in the incorrect order than what is desired!! Be sure you know what you're doing if you do this. You're probably better off using a db GUI tool such as phpMyAdmin, and manually keying in the batch numbers you want.Lesbos
G
-1

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.

Gesualdo answered 12/6, 2015 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.