Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Migrate command ignores the --database option #17546

Closed
kfirba opened this issue Jan 25, 2017 · 7 comments
Closed

[5.4] Migrate command ignores the --database option #17546

kfirba opened this issue Jan 25, 2017 · 7 comments

Comments

@kfirba
Copy link

kfirba commented Jan 25, 2017

  • Laravel Version: 5.4.2
  • PHP Version: 7.0.12
  • Database Driver & Version: 10.1.19-MariaDB

Description:

When we run a command in the the php artisan migrate namespace, such as the php artisan migrate:refresh command with the --database flag, it doesn't run the migrations against the appropriate database.

Assuming we have these two connections defined in config/database.php:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'mysql-testing' => [
            'driver' => 'mysql',
            'host' => env('DB_TESTING_HOST', 'localhost'),
            'port' => env('DB_TESTING_PORT', '3306'),
            'database' => env('DB_TESTING_DATABASE', 'forge'),
            'username' => env('DB_TESTING_USERNAME', 'forge'),
            'password' => env('DB_TESTING_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

And these environment variables defined in the .env file:

DB_HOST="127.0.0.1"
DB_PORT=3306
DB_DATABASE="example"
DB_USERNAME="root"
DB_PASSWORD=""

DB_TESTING_CONNECTION="mysql-testing"
DB_TESTING_HOST="127.0.0.1"
DB_TESTING_PORT=3306
DB_TESTING_DATABASE="example_testing"
DB_TESTING_USERNAME="root"
DB_TESTING_PASSWORD=""

When we try to migrate the testing database:

php artisan migrate --database="mysql-testing"

The migration is trying to migrate the original database - the mysql connection instead of mysql-testing connection.

I tried to track down the bug, but couldn't find much. If we visit the Illuminate\Database\Migrations\Migrator class at the runMigration method:

protected function runMigration($migration, $method)
    {
        $connection = $this->resolveConnection(
            $migration->getConnection()
        );

        $callback = function () use ($migration, $method) {
            $migration->{$method}();
        };

        $this->getSchemaGrammar($connection)->supportsSchemaTransactions()
                    ? $connection->transaction($callback)
                    : $callback();
    }

I noticed that it resolves the right $connection but it just never uses it. The migration itself relies on the connection property defined on the abstract class Illuminate\Database\Migrations\Migration which all of the migrations inherit from. This connection is set to null by default.

I tried reverting my app version to 5.3 and run the commands above and it works as expected.

Steps To Reproduce:

Upgrade a Laravel 5.3 app to 5.4 and try the steps above.

@JeffreyWay
Copy link
Contributor

I noticed this issue earlier tonight, too.

@Crinsane
Copy link
Contributor

At our company we also noticed this problem. It can easily be reproduced. Setup a clean installation of Laravel. Set the default connection to something like 'sqlite' and have a secondary connection 'mysql'. Next run artisan migrate --database="mysql" and you'll see that the migrations table is created in mysql, but the other tables are not.

@tomschlick
Copy link
Contributor

Yup ran into this myself while upgrading a multi tenant app with "dynamic connections" to 5.4

@tomschlick
Copy link
Contributor

Could be related to #17548 -- both seem to deal with some sort of connection caching that isn't being changed over when using another database.

@Oldenborg
Copy link

I also have this issue as of today.

@djaiss
Copy link

djaiss commented Jan 26, 2017

I have the exact same problem.

@themsaid
Copy link
Member

fix submitted to #17574

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants