1

I've inserted a column with name im_useless to my table earlier which I do not need anymore.

This is my schema (filename: 2017_02_27_120313_units.php):

public function up()
{
    Schema::create('units', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->string('im_useless');
        $table->timestamps();
    });
}

Now I try to remove it, so I used this code inside the down() function:

public function down()
{
    Schema::dropColumn('im_useless');
}

New Schema:

public function up()
{
    Schema::create('units', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->timestamps();
    });
}

Now I have to rollback and then migrate again. I try to rollback only that specific migration file, by executing php artisan help migrate:rollback I found out that there is a --path option.

So I tried to rollback that specific migration like this:

php artisan migrate:rollback --path=2017_02_27_120313_units.php

But I get Nothing to rollback

How can I drop that specific column without having to rollback any other migrations?


UPDATE:

I think I have to change the path like this:

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

...since my php shell was opened in the project root folder?

However I still get Nothing to rollback

I also tried php artisan migrate --path=2017_02_27_120313_units.php and php artisan migrate --path=database/migrate/2017_02_27_120313_units.php

...and get Nothing to migrate


UPDATE 2

I think I have messed up my migrations table, because I removed the code inside the down() function and the table was never deleted. https://stackoverflow.com/a/26077506/4684797

Community
  • 1
  • 1
Black
  • 18,150
  • 39
  • 158
  • 271
  • Using migrations is much more complicated than just doing the operations directly in the database e.g. with phpmyadmin... thats what I learned today – Black Feb 27 '17 at 14:40

4 Answers4

2

The rollback function is meant to give you the possibility to revert to the version you had right before you migrated, in case something goes wrong when you deploy. If you want to drop a specific column that you don't need anymore, you should treat that as a new migration and drop the column in the up() method.

LorenzSchaef
  • 1,523
  • 10
  • 16
  • Does not work. If I try to do a new migration like this `php artisan make:migrate units --create=units --table=units` then I get `InvalidArgumentException, A Units migration already exists` – Black Feb 27 '17 at 13:31
  • `php artisan make:migration dropColumnFromTable` (replace `column` and `table` with applicable names), and in the `public function up()` method, do `Schema::table("table", function( ... ){ Schema::dropColumn("column"); });`. The `down` function could be set to add `column` back to `table`, but could also (and likely should) be left blank. – Tim Lewis Feb 27 '17 at 15:13
0

If your laravel version is >= 5.3 you could simply add the migrations path like packages does.

https://laravel.com/docs/5.3/packages#migrations

Just put this code in your AppServiceProvider boot method:

$this->loadMigrationsFrom(__DIR__.'/path/to/migrations');

Also if you want to include subfolders recursively you can try with "/path/to/migrations/**/*" but I'm not sure if this will work in older laravel versions.

0

--path param is like a filter. It will work on the latest migrations that rollback will act. You can use path reference starting from datatabase/migrations. For example:

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

It will work if in your database, you have something like 2022_10_03_193316_create_something.php on migration column plus greatest batch value.

Tested on Laravel 6

-1

Run composer dump-autoload and try again

AhmedAlawady
  • 77
  • 1
  • 3
  • ok, I think you must add dropColumn inside Schema::table('units', function (Blueprint $table) { Schema::dropColumn('im_useless');} – AhmedAlawady Feb 27 '17 at 13:56
  • Does not work either, same error `InvalidArgumentException, A Units migration already exists` – Black Feb 27 '17 at 14:01