Skip to content

Commit 2420424

Browse files
authored
fix: migrations not using custom DB connection of migration runner (#8221)
* Add failing test * Add fix * Fix setting of group * Change priority order of migration's db group
1 parent 8f2c065 commit 2420424

4 files changed

Lines changed: 40 additions & 25 deletions

File tree

phpstan-baseline.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,11 +1146,6 @@
11461146
'count' => 1,
11471147
'path' => __DIR__ . '/system/Database/Migration.php',
11481148
];
1149-
$ignoreErrors[] = [
1150-
'message' => '#^Property CodeIgniter\\\\Database\\\\Migration\\:\\:\\$DBGroup \\(string\\) on left side of \\?\\? is not nullable\\.$#',
1151-
'count' => 1,
1152-
'path' => __DIR__ . '/system/Database/Migration.php',
1153-
];
11541149
$ignoreErrors[] = [
11551150
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
11561151
'count' => 8,

system/Database/Migration.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class Migration
2121
/**
2222
* The name of the database group to use.
2323
*
24-
* @var string
24+
* @var string|null
2525
*/
2626
protected $DBGroup;
2727

@@ -39,12 +39,15 @@ abstract class Migration
3939
*/
4040
protected $forge;
4141

42-
/**
43-
* Constructor.
44-
*/
4542
public function __construct(?Forge $forge = null)
4643
{
47-
$this->forge = $forge ?? Database::forge($this->DBGroup ?? config(Database::class)->defaultGroup);
44+
if (isset($this->DBGroup)) {
45+
$this->forge = Database::forge($this->DBGroup);
46+
} elseif ($forge !== null) {
47+
$this->forge = $forge;
48+
} else {
49+
$this->forge = Database::forge(config(Database::class)->defaultGroup);
50+
}
4851

4952
$this->db = $this->forge->getConnection();
5053
}

system/Database/MigrationRunner.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,12 @@ public function __construct(MigrationsConfig $config, $db = null)
135135
$this->enabled = $config->enabled ?? false;
136136
$this->table = $config->table ?? 'migrations';
137137

138-
// Default name space is the app namespace
139138
$this->namespace = APP_NAMESPACE;
140139

141-
// get default database group
142-
$config = config(Database::class);
143-
$this->group = $config->defaultGroup;
144-
unset($config);
140+
// Even if a DB connection is passed, since it is a test,
141+
// it is assumed to use the default group name
142+
$this->group = is_string($db) ? $db : config(Database::class)->defaultGroup;
145143

146-
// If no db connection passed in, use
147-
// default database group.
148144
$this->db = db_connect($db);
149145
}
150146

@@ -836,8 +832,9 @@ protected function migrate($direction, $migration): bool
836832
throw new RuntimeException($message);
837833
}
838834

839-
$instance = new $class();
840-
$group = $instance->getDBGroup() ?? config(Database::class)->defaultGroup;
835+
/** @var Migration $instance */
836+
$instance = new $class(Database::forge($this->db));
837+
$group = $instance->getDBGroup() ?? $this->group;
841838

842839
if (ENVIRONMENT !== 'testing' && $group === 'tests' && $this->groupFilter !== 'tests') {
843840
// @codeCoverageIgnoreStart
@@ -853,8 +850,6 @@ protected function migrate($direction, $migration): bool
853850
return true;
854851
}
855852

856-
$this->setGroup($group);
857-
858853
if (! is_callable([$instance, $direction])) {
859854
$message = sprintf(lang('Migrations.missingMethod'), $direction);
860855

tests/system/Database/Migrations/MigrationRunnerTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace CodeIgniter\Database\Migrations;
1313

1414
use CodeIgniter\Database\BaseConnection;
15-
use CodeIgniter\Database\Config;
1615
use CodeIgniter\Database\MigrationRunner;
1716
use CodeIgniter\Events\Events;
1817
use CodeIgniter\Exceptions\ConfigException;
@@ -454,11 +453,34 @@ public function testGetBatchVersions(): void
454453
$this->assertSame('2018-01-24-102302', $runner->getBatchEnd(1));
455454
}
456455

457-
protected function resetTables(): void
456+
public function testMigrationUsesSameConnectionAsMigrationRunner(): void
458457
{
459-
$forge = Config::forge();
458+
$config = ['database' => WRITEPATH . 'runner.sqlite', 'DBDriver' => 'SQLite3', 'DBDebug' => true];
460459

461-
foreach (db_connect()->listTables() as $table) {
460+
$database = Database::connect($config, false);
461+
$this->resetTables($database);
462+
463+
$runner = new MigrationRunner(config(Migrations::class), $database);
464+
$runner->clearCliMessages();
465+
$runner->clearHistory();
466+
$runner->setNamespace('Tests\Support\MigrationTestMigrations');
467+
$runner->latest();
468+
469+
$tables = $database->listTables();
470+
$this->assertCount(2, $tables);
471+
$this->assertSame('migrations', $tables[0]);
472+
$this->assertSame('foo', $tables[1]);
473+
}
474+
475+
protected function resetTables($db = null): void
476+
{
477+
$forge = Database::forge($db);
478+
479+
/** @var BaseConnection $conn */
480+
$conn = $forge->getConnection();
481+
$conn->resetDataCache();
482+
483+
foreach (db_connect($db)->listTables() as $table) {
462484
$table = str_replace('db_', '', $table);
463485
$forge->dropTable($table, true);
464486
}

0 commit comments

Comments
 (0)