Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

$connection property ignored in migrations #38777

Closed
JohnnyWalkerDigital opened this issue Sep 12, 2021 · 2 comments
Closed

$connection property ignored in migrations #38777

JohnnyWalkerDigital opened this issue Sep 12, 2021 · 2 comments

Comments

@JohnnyWalkerDigital
Copy link
Contributor

  • Laravel Version: 8.60.0
  • PHP Version: 7.4.11
  • Database Driver & Version: MySQL 8.0.25

Description:

The documentation states that you can create a migration connection by using the $connection property. I have done this with 70 migrations in my project and can confirm that the default connection was used for all migrations.

Steps To Reproduce:

  1. Create a second database
  2. Create a new migration that you wish to use with the second database
  3. Add protected $connection = 'second_database'; to the top of your migration.
  4. Run migration.

The migration should use the connection specified in the $connection property, but it will ignore it and use the default connection.

@derekmd
Copy link
Contributor

derekmd commented Sep 12, 2021

Commit that changed the behavior starting in laravel/framework 7.0.0: f4bd05f

Pull request #35842 was rejected earlier this year but I think only because it lacked a bug report description and the addition of tests to cover documented behavior.

Either that pull request needs to be properly re-submitted to the 8.x branch or the docs should be updated to suggest the answer given in #31060: call Schema::connection('secondary_database')->create(...).

@lk77
Copy link

lk77 commented Sep 13, 2021

I've tested it and it work :

class TestMigration extends Migration
{
    protected $connection = 'does_not_exist';
    [...]
}
Migrating: 2021_09_13_090345_test_migration

   InvalidArgumentException 

  Database connection [does_not_exist] not configured.

  at vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:152
    148▕         // If the configuration doesn't exist, we'll throw an exception and bail.
    149▕         $connections = $this->app['config']['database.connections'];
    150▕ 
    151▕         if (is_null($config = Arr::get($connections, $name))) {
  ➜ 152▕             throw new InvalidArgumentException("Database connection [{$name}] not configured.");
    153▕         }
    154▕ 
    155▕         return (new ConfigurationUrlParser)
    156▕                     ->parseConfiguration($config);

      +23 vendor frames 
  24  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

@laravel laravel locked and limited conversation to collaborators Sep 13, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants