Skip to content

Commit 6de7b05

Browse files
committed
fix: refactor migration table creation for consistency and improve test database teardown process.
1 parent f3b8b32 commit 6de7b05

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

migrations/m250707_103609_tree.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@ class m250707_103609_tree extends Migration
88
{
99
public function safeUp()
1010
{
11-
$primaryKey = $this->db->driverName === 'oci'
12-
? 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'
13-
: $this->primaryKey()->notNull();
14-
15-
$nameColumn = $this->db->driverName === 'oci'
16-
? $this->string()->notNull()
17-
: $this->text()->notNull();
18-
19-
$this->createTable('{{%tree}}', [
20-
'id' => $primaryKey,
21-
'name' => $nameColumn,
22-
'lft' => $this->integer()->notNull(),
23-
'rgt' => $this->integer()->notNull(),
24-
'depth' => $this->integer()->notNull(),
25-
]);
11+
$rawPrimaryKey = 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY';
12+
13+
$this->createTable(
14+
'{{%tree}}',
15+
[
16+
'id' => $this->db->driverName !== 'oci' ? $this->primaryKey()->notNull() : $rawPrimaryKey,
17+
'name' => $this->db->driverName === 'oci' ? $this->string()->notNull() : $this->text()->notNull(),
18+
'lft' => $this->integer()->notNull(),
19+
'rgt' => $this->integer()->notNull(),
20+
'depth' => $this->integer()->notNull(),
21+
],
22+
);
2623

2724
$this->createIndex('idx_tree_lft', '{{%tree}}', 'lft');
2825
$this->createIndex('idx_tree_rgt', '{{%tree}}', 'rgt');

migrations/m250707_104009_multiple_tree.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ class m250707_104009_multiple_tree extends Migration
88
{
99
public function safeUp()
1010
{
11-
$primaryKey = $this->db->driverName === 'oci'
12-
? 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'
13-
: $this->primaryKey();
14-
15-
$nameColumn = $this->db->driverName === 'oci'
16-
? $this->string(4000)->notNull()
17-
: $this->text()->notNull();
18-
19-
$this->createTable('{{%multiple_tree}}', [
20-
'id' => $primaryKey,
21-
'tree' => $this->integer()->null(),
22-
'name' => $nameColumn,
23-
'lft' => $this->integer()->notNull(),
24-
'rgt' => $this->integer()->notNull(),
25-
'depth' => $this->integer()->notNull(),
26-
]);
11+
$rawPrimaryKey = 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY';
12+
13+
$this->createTable(
14+
'{{%multiple_tree}}',
15+
[
16+
'id' => $this->db->driverName !== 'oci' ? $this->primaryKey()->notNull() : $rawPrimaryKey,
17+
'tree' => $this->integer()->null(),
18+
'name' => $this->db->driverName === 'oci' ? $this->string()->notNull() : $this->text()->notNull(),
19+
'lft' => $this->integer()->notNull(),
20+
'rgt' => $this->integer()->notNull(),
21+
'depth' => $this->integer()->notNull(),
22+
],
23+
);
2724

2825
$this->createIndex('idx_multiple_tree_tree', '{{%multiple_tree}}', 'tree');
2926
$this->createIndex('idx_multiple_tree_lft', '{{%multiple_tree}}', 'lft');

tests/TestCase.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,21 @@ protected function buildFlatXMLDataSet(array $dataSet): string
170170
protected function createDatabase(): void
171171
{
172172
$command = $this->getDb()->createCommand();
173+
$dropTables = [
174+
'migration',
175+
'multiple_tree',
176+
'tree',
177+
];
178+
179+
try {
180+
$this->runMigrate('down', ['all']);
181+
} catch (RuntimeException) {
182+
}
173183

174-
$this->runMigrate('down', ['all']);
175-
176-
if ($this->getDb()->getTableSchema('migration', true) !== null) {
177-
$command->dropTable('migration')->execute();
184+
foreach ($dropTables as $table) {
185+
if ($this->getDb()->getTableSchema($table, true) !== null) {
186+
$command->dropTable($table)->execute();
187+
}
178188
}
179189

180190
$this->runMigrate('up');
@@ -395,7 +405,11 @@ protected function runMigrate(string $action, array $params = []): mixed
395405

396406
$result = $migrate->run($action, $params);
397407

398-
ob_get_clean();
408+
$capture = ob_get_clean();
409+
410+
if (is_int($result) && $result !== 0) {
411+
throw new RuntimeException("Migration '{$action}' failed with code {$result}.\nOutput: {$capture}");
412+
}
399413

400414
return $result;
401415
}

0 commit comments

Comments
 (0)