Skip to content

Commit 11a7d5e

Browse files
committed
Handle case of custom column name
1 parent 7a42619 commit 11a7d5e

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/lib/SchemaToDatabase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public static function f7(array $schemasToDrop): array // TODO rename
282282
'drop' => true
283283
]);
284284
$dbModelsToDrop[$key] = $dbModelHere;
285-
// TODO
285+
// TODO remove
286286
// $mm = new MigrationModel($dbModelHere);
287287
// $builder = new MigrationRecordBuilder($this->db->getSchema());
288288
// $mm->addUpCode($builder->dropTable($tableName))

src/lib/generators/MigrationsGenerator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ protected function createBuilder(DbModel $model):BaseMigrationBuilder
175175
protected function sortMigrationsByDeps():array
176176
{
177177
$this->sorted = [];
178-
// ksort($this->migrations);
179178
foreach ($this->migrations as $migration) {
180179
//echo "adding {$migration->tableAlias}\n";
181180
$this->sortByDependencyRecurse($migration);

tests/specs/issue_fix/132_create_migration_for_drop_table/132_create_migration_for_drop_table.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ info:
33
version: 1.0.0
44
title: 132_create_migration_for_drop_table \#132
55

6-
x-deleted-schemas: # don't use x-keep-table, use x-deleted-schemas
6+
x-deleted-schemas:
77
- Pristine
8-
- Fruit # table name evaluated to `itt_fruits`. `itt_` is prefix
9-
# - Mango: the_mango7 # custom table name; `x-table`
8+
- Fruit # Example: table name evaluated to `itt_fruits`. `itt_` is prefix set in DB config
9+
- Mango: the_mango_table_name # custom table name; see `x-table` in README.md
10+
- Animal: the_animal_table_name
1011
- Upk
1112
- Bigpk
1213
- Ubigpk

tests/unit/IssueFixTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function testCreateMigrationForDropTable132()
283283
// $this->deleteTablesForCreateMigrationForDropTable132();
284284
$this->createTablesForCreateMigrationForDropTable132();
285285
$this->runGenerator($testFile);
286-
$this->runActualMigrations('mysql', 6);
286+
$this->runActualMigrations('mysql', 8);
287287
// ... TODO compare files
288288
$this->deleteTablesForCreateMigrationForDropTable132();
289289
$this->deleteTables();
@@ -304,27 +304,47 @@ private function createTablesForCreateMigrationForDropTable132()
304304
'name' => 'string(150)',
305305
])->execute();
306306

307-
307+
// ---
308308
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
309309
'id' => 'pk',
310310
'name' => 'string(150)',
311+
'food_of' => 'int'
311312
])->execute();
312313
Yii::$app->db->createCommand()->createTable('{{%pristines}}', [
313314
'id' => 'pk',
314315
'name' => 'string(151)',
315316
'fruit_id' => 'int', // FK
316317
])->execute();
317318
Yii::$app->db->createCommand()->addForeignKey('name', '{{%pristines}}', 'fruit_id', '{{%fruits}}', 'id')->execute();
319+
320+
// ---
321+
Yii::$app->db->createCommand()->createTable('{{%the_animal_table_name}}', [
322+
'id' => 'pk',
323+
'name' => 'string(150)',
324+
])->execute();
325+
Yii::$app->db->createCommand()->addForeignKey('name2', '{{%fruits}}', 'food_of', '{{%the_animal_table_name}}', 'id')->execute();
326+
Yii::$app->db->createCommand()->createTable('{{%the_mango_table_name}}', [
327+
'id' => 'pk',
328+
'name' => 'string(150)',
329+
'food_of' => 'int'
330+
])->execute();
331+
Yii::$app->db->createCommand()->addForeignKey('animal_fruit_fk', '{{%the_mango_table_name}}', 'food_of', '{{%the_animal_table_name}}', 'id')->execute();
318332
}
319333

320334
private function deleteTablesForCreateMigrationForDropTable132()
321335
{
322336
Yii::$app->db->createCommand()->dropForeignKey('name', '{{%pristines}}')->execute();
323337
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute();
338+
Yii::$app->db->createCommand()->dropForeignKey('name2', '{{%fruits}}')->execute();
324339
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
340+
325341
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%upks}}')->execute();
326342
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%bigpks}}')->execute();
327343
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%ubigpks}}')->execute();
344+
345+
Yii::$app->db->createCommand()->dropForeignKey('animal_fruit_fk', '{{%the_mango_table_name}}')->execute();
346+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_mango_table_name}}')->execute();
347+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_animal_table_name}}')->execute();
328348
}
329349

330350
public function test162BugDollarrefWithXFaker()

0 commit comments

Comments
 (0)