Skip to content

[5.8] Bug: Schema is aware of the connection that was default when it was first initialized, while it should be aware of the current connection #27686

@stancl

Description

@stancl
  • Laravel Version: 5.8.0
  • PHP Version: 7.2
  • Database Driver & Version: Doesn't matter

Description:

Introduced in #25512.

This results in Schema using the connection that the DB manager uses when Schema is first called.

// change DB connection from default to foo
Schema::getConnection()->getName(); // => foo
// change the DB connection back
Schema::getConnection()->getName(); // => foo

Schema remembers the connection that was used when it was first initialized.

In 5.7 Schema used the current DB connection, which I believe is more correct.

My package's tests are broken because of this.

https://github.com/stancl/tenancy/blob/e1def355f96686556edd125975e76d1018d30d24/tests/CommandsTest.php#L30-L37

On line 34 Schema is first initialized and remembers that the sqlite connection is used and on line 36, when the DB manager uses the tenant connection, Schema still uses the sqlite connection.

Steps To Reproduce:

You can try to turn my pseudocode into code or you can play with my package:

cd /tmp
git clone https://github.com/stancl/tenancy/
cd tenancy
git checkout f9a819215f7350bc3aec869cf7da687cd53bfcbc
composer install
phpunit --filter migrate_command_works_with_tenants_option

and look at the migrate_command_works_with_tenants_option method in tests/CommandsTest.php

@taylorotwell said:

Hmm, I'm scared to change this in case I forgot some reason I did it this way. 👀

I think that this is the reason. This facade's accessor should be obtained on demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions