Uh oh!
There was an error while loading. Please reload this page.
fix: migrations not using custom DB connection of migration runner - #8221
Conversation
paulbalandan
commented
Nov 15, 2023
I found this bug while working on |
kenjis
commented
Nov 15, 2023
@paulbalandan The code is indeed something wrong, but I don't understand the significance of passing DB connection to MigrationRunner. |
paulbalandan
commented
Nov 15, 2023
The use case is this: In order to infer the property types of Entities, I thought of using the database tables and the fields. So, I must run migrations in order to get the tables. However, I don't want to mess with the migrations of the end users so I thought of running the migrations in a separate connection. So, regardless of the default connection of the user, I run my own migrations using |
Uh oh!
There was an error while loading. Please reload this page.
Okay, so you want to change the DB connection for testing. I sent PR #8222 to make it clear. |
a7a6777 to
fbdcf93Comparekenjis
commented
Nov 16, 2023
What should be the group for the added test? |
kenjis
commented
Nov 16, 2023
It appears that |
fbdcf93 to
dac028fCompareUh oh!
There was an error while loading. Please reload this page.
kenjis
commented
Nov 17, 2023
Frankly, Migration is complex and difficult to maintain consistency. When temporarily changing a DB connection for testing purposes, it seems better to regard that the DB connection as the default group because it is easier to manipulate. However, it seems a bit odd that the DB group name in the migration history is changed to the default group even if you specify the name of the DB group, e.g., Maybe the following patch is needed. --- a/system/Database/MigrationRunner.php+++ b/system/Database/MigrationRunner.php@@ -141,10 +141,17 @@ class MigrationRunner
// Default name space is the app namespace
$this->namespace = APP_NAMESPACE;
- // get default database group- $config = config(Database::class);- $this->group = $config->defaultGroup;- unset($config);+ if (is_string($db)) {+ $this->group = $db;+ } else {+ // We use `defaultGroup` even if $db is ConnectionInterface or config+ // array. Because this is for testing purposes, and it is easier to+ // understand and operate as if the migrations were run on the+ // default group.+ $config = config(Database::class);+ $this->group = $config->defaultGroup;+ unset($config);+ }
// If no db connection passed in, use
// default database group.
@@ -841,7 +848,7 @@ class MigrationRunner
/** @var Migration $instance */
$instance = new $class(Database::forge($this->db));
- $group = $instance->getDBGroup() ?? config(Database::class)->defaultGroup;+ $group = $instance->getDBGroup() ?? $this->group;
if (ENVIRONMENT !== 'testing' && $group === 'tests' && $this->groupFilter !== 'tests') {
// @codeCoverageIgnoreStart
@@ -857,8 +864,6 @@ class MigrationRunner
return true;
}
- $this->setGroup($group);-
if (! is_callable([$instance, $direction])) {
$message = sprintf(lang('Migrations.missingMethod'), $direction);
|
c581a48 to
6a078a4CompareMGatner
commented
Nov 23, 2023
This is complicated. I'm tracking but let me know if you need me more. Thanks for handling. |
6a078a4 to
fa68681CompareUh oh!
There was an error while loading. Please reload this page.
fa68681 to
9e56bbcCompare9e56bbc to
42fe160Compare
Description
Closes#8060
When using custom array connection for migration runner, the migrations ran do not use the custom connection of the runner but instead uses the default shared connection.
Checklist: