-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
fix: migrations not using custom DB connection of migration runner #8221
fix: migrations not using custom DB connection of migration runner #8221
Conversation
I found this bug while working on |
@paulbalandan The code is indeed something wrong, but I don't understand the significance of passing DB connection to MigrationRunner. |
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 |
Okay, so you want to change the DB connection for testing. I sent PR #8222 to make it clear. |
a7a6777
to
fbdcf93
Compare
What should be the group for the added test?
|
It appears that |
fbdcf93
to
dac028f
Compare
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
6a078a4
Compare
This is complicated. I'm tracking but let me know if you need me more. Thanks for handling. |
6a078a4
to
fa68681
Compare
fa68681
to
9e56bbc
Compare
9e56bbc
to
42fe160
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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: