Framework: [5.3] Rolling Back Migrations Created With --path Parameter

Created on 26 Aug 2016  路  7Comments  路  Source: laravel/framework

Running into a problem in Laravel 5.3 that I don't think I've had in past versions (5.0,5.1...not sure about 5.2). I've done a bit of searching and I see other folks get 'Class not Found' errors when using "--path" to create migrations, but this seems a bit different.

First I create a migration in a folder...all good.

php artisan make:migration create_demo_table --path=database/migrations/demo
Created Migration: 2016_08_26_195240_create_demo_table

Next, I run the migration...all good.

php artisan migrate --path=database/migrations/demo
Migrated: 2016_08_26_195240_create_demo_table

Now, I try to rollback.

php artisan migrate:rollback -vvv

...and I get...

[ErrorException]                                      
Undefined index: 2016_08_26_195240_create_demo_table

Exception trace:
 () at /var/www/html/app/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:207
 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at /var/www/html/app/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:207
 Illuminate\Database\Migrations\Migrator->rollback() at /var/www/html/app/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/RollbackCommand.php:61
 Illuminate\Database\Console\Migrations\RollbackCommand->fire() at n/a:n/a
 call_user_func_array() at /var/www/html/app/bootstrap/cache/compiled.php:1269
 Illuminate\Container\Container->call() at /var/www/html/app/vendor/laravel/framework/src/Illuminate/Console/Command.php:169
 Illuminate\Console\Command->execute() at /var/www/html/app/vendor/symfony/console/Command/Command.php:256
 Symfony\Component\Console\Command\Command->run() at /var/www/html/app/vendor/laravel/framework/src/Illuminate/Console/Command.php:155
 Illuminate\Console\Command->run() at /var/www/html/app/vendor/symfony/console/Application.php:818
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/app/vendor/symfony/console/Application.php:186
 Symfony\Component\Console\Application->doRun() at /var/www/html/app/vendor/symfony/console/Application.php:117
 Symfony\Component\Console\Application->run() at /var/www/html/app/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:122
 Illuminate\Foundation\Console\Kernel->handle() at /var/www/html/app/artisan:3

Since the "--path" option doesn't exist on migrate:rollback, not sure what to try.

Not a huge deal for me (since I don't rollback that often), but just curious if any body else running into this.

Most helpful comment

This has existed for a long time. The solution is to add a --path param to the codebase for rollback but for some reason it hasn't been done yet.

All 7 comments

This has existed for a long time. The solution is to add a --path param to the codebase for rollback but for some reason it hasn't been done yet.

All you have todo in L5.3 now is define the migration path in your custom apps service provider boot() method like so #$this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); which is documented here https://laravel.com/docs/5.3/packages#migrations but @taylorotwell did not add to the Upgrade Guide...which would be helpful for others. With this in place, no --path on rollback is required. This change happened because a rollback or reset now gets a list of migration $files first. In L5.2 it used migrations from the migrations table instead.

The --path field is still required if you use multiple databases. You can't set a connection when using loadMigrationsFrom() :(

All of my "apps" migrations never touch main laravel db...so all my apps have their own dbs. And it works well for me. Not sure what you have going on. Let me show you how it works for me.

Say I have a package called "Warehouse", it does NOT use the main laravel database, uses its own "warehouse" database. All migrations are in the Warehouse/Database/Migrations folder, no where near laravel.

In WarehouseServiceProvider.php boot() method I have
$this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');

Then I write my own little console helper to migrate, seed, rollback, reset...but its all just laravels artisan commands with the proper --flags.

    /**
     * Run the database migrations
     */
    protected function dbMigrate()
    {
        $this->call('migrate', [
            '--database' => $this->connection['name'],
            '--path' => "$this->relativePath/Database/Migrations/",
            '--force' => $force = $this->input->getOption('force')
        ]);
    }

    /**
     * Rollback the last database migration
     */
    protected function dbRollback()
    {
        $this->call('migrate:rollback', [
            '--database' => $this->connection['name'],
            '--force' => $force = $this->input->getOption('force')
        ]);
    }

    /**
     * Rolls all of the currently applied migrations back
     */
    protected function dbReset()
    {
        $this->call('migrate:reset', [
            '--database' => $this->connection['name'],
            '--force' => $force = $this->input->getOption('force')
        ]);
    }

And everything works great. So in L5.3 rollback and reset were broken for me ONLY becuase I didn't have the new $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); in the provider.

In laravel 5.2, laravels rollback() and reset() internal methods would simply look at the migrations table defined in the passed --database...so it worked from the database table NOT your migrations folder. Now in L5.3, it gets a list of $files...and it knows WHERE to find those files because of the $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); So L5.2 used migrations table as its queue, L5.3 uses the files in the folder as its queue for rollback. Notice you still pass the --database param for rollback and reset so it uses your APPS database, not laravels.

Thanks for letting me know why it happened. I'll work around it.

had same problem. on L 5.4 passed path parameter to migrate:rollback and worked.

php artisan migrate:rollback --path=/database/migrations/my-folder

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerbylav picture kerbylav  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

ghost picture ghost  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments