5

I need to rollback a specific table as I forgot to mention foreign key. and I dont want my data loss from all tables.

My framework is Laravel 5.4

Thanks in Advance Everyone

Mansoor Ahmad Khan
  • 124
  • 1
  • 1
  • 9

4 Answers4

8

You can do

php artisan migrate:rollback --path=/database/migrations/your_file.php

Hajopa
  • 91
  • 2
  • 3
7

I do it this way:

  1. Deleting a table manually;
  2. Going to "migrations" table and deleting corresponding row to your migration;
  3. php artisan migrate;
  4. done

For example, if you modified migration called "Settings", then you delete this table from DB and migrations table, then rerun artisan command.

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
3

Migrations are meant to be applied and rolled back in a specific order. Therefore there is no "proper" way of reapplying an arbitrary migration.

In any case there are at least two options:

  1. "Fail forward" - create a subsequent migration that creates a necessary FK and apply it. This is the only proper way if you are already in production.

  2. If you're just in early stages of development and don't won't to bloat the migrations directory you can

    • dump the tables so that you preserve the data
    • rollback up to this particular migration
    • fix and test the migration
    • migrate
    • load the data from dumps
peterm
  • 91,357
  • 15
  • 148
  • 157
  • +1 . Yes it is development phase, i am working on my semester project. i am new to laravel , i got your point number 2, will you please further explain point number 1. – Mansoor Ahmad Khan May 21 '17 at 18:51
  • Just create a new migration that adds a missing FK and migrate your database. – peterm May 21 '17 at 19:19
1

Unfortunately there isn't an option to rollback a migration per table basis. You can only rollback the latest migration or last few migrations using the step parameter.

But there is a hacky way to do it in case you really need to. You can set the batch value in the migrations table to a higher number than the most recent migration for only the migrations you want to rollback. With this when you call php artisan migrate:rollback, only that particular migration files with batch value altered would rollback.

Sandeesh
  • 11,486
  • 3
  • 31
  • 42
  • how to set batch number through laravel commands or should i go manually ? if the batch number is set. what command should I run on cmdprpmt/terminal thanks in advance – Mansoor Ahmad Khan May 21 '17 at 18:53
  • You need to set the batch number manually in the db, set it a number higher than the current highest. Once done you can run the normal rollback command to rollback those migrations `php artisan migrate:rollback`. You can also verify the migrations that would be affected after changing the batch value by running `php artisan migrate:rollback --pretend` – Sandeesh May 21 '17 at 18:59