9

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?

Community
  • 1
  • 1
hogan
  • 1,434
  • 1
  • 15
  • 32
  • 1
    There's some info here: http://stackoverflow.com/questions/19102197/running-one-specific-laravel-4-migration-single-file The short version is that migrations weren't built for that purpose. – Joel Hinz Jun 12 '15 at 19:50
  • That's the exact same link from my question. – hogan Jun 12 '15 at 19:57
  • I apologise. I read that too clumsily. – Joel Hinz Jun 12 '15 at 19:57
  • 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. – cartbeforehorse Apr 28 '23 at 09:15

4 Answers4

16

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.

Rob Derks
  • 196
  • 1
  • 7
  • 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) – gthuo Apr 16 '18 at 05:06
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
Joey Rich
  • 391
  • 3
  • 7
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.

Colin
  • 2,109
  • 1
  • 20
  • 22
  • 1
    too much hassle, since copying one file after another into the folder is faster i go with this one. – hogan Aug 24 '15 at 23:32
  • 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). – Colin Aug 27 '15 at 16:19
  • 1
    Wasn't my downvote. I rather wanted to know if there is a command option. Don't worry more, the question is answered. – hogan Aug 28 '15 at 22:59
  • 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) – Arda Jun 07 '16 at 08:38
  • 1
    @Arda: Yup. I think I made the initial PR to add `--step` to the core. :) – Colin Jun 08 '16 at 14:18
  • 1
    @Colin Ah, great, didn't know about that, thanks man! It'll be a life saver! – Arda Jun 08 '16 at 16:17
  • 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. – cartbeforehorse Apr 28 '23 at 08:49
-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.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368