diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index c03979df..8ba6a0fa 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -258,6 +258,11 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $ return null; } } + if ($forAlter && $forDrop) { + if (!array_key_exists($prevColName, $this->newColumns)) { + return null; + } + } return self::POS_AFTER . ' ' . $prevColName; } return null; diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 947cadea..f1d2410c 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -430,7 +430,6 @@ private function for58($schema, $expected, $columns = [ $actual = file_get_contents(Yii::getAlias('@app') . '/migrations_' . $dbStr . '_db/m200000_000000_change_table_fruits.php'); $this->assertSame($expected, $actual); $this->runActualMigrations($dbStr, 1); - $deleteTable(); } FileHelper::unlink($tmpConfigFile); @@ -769,7 +768,7 @@ public function down() PHP; - $this->for58($schema, $expected, $columns/*, ['Mysql']*/); + $this->for58($schema, $expected, $columns); } public function test58DeleteFirst4Col() @@ -832,7 +831,7 @@ public function down() PHP; - $this->for58($schema, $expected, $columns/*, ['Mysql']*/); + $this->for58($schema, $expected, $columns); } // ------------ Add @@ -1219,7 +1218,7 @@ public function down() PHP; - $this->for58($schema, $expected, $columns/*, ['Mysql']*/); + $this->for58($schema, $expected, $columns); } // ----------- Miscellaneous @@ -1290,7 +1289,7 @@ public function down() PHP; - $this->for58($schema, $expected, $columns/*, ['Mysql']*/); + $this->for58($schema, $expected, $columns); } public function test58Add1Del1ColAtSamePosition() @@ -1357,7 +1356,141 @@ public function down() $this->for58($schema, $expected, $columns); } - // TODO add tests cases: - // add 1 and del 1 col at different position - // add 2 and del 1 col at different positions + public function test58Add3Del2ColAtDiffPos() + { + $columns = [ + 'id' => 'pk', + 'name' => 'bool null', + 'description' => 'bool null', + 'colour' => 'bool null', + 'size' => 'bool null', + ]; + + $schema = <<addColumn('{{%fruits}}', 'col_6', $this->boolean()->null()->defaultValue(null)->after('id')); + $this->addColumn('{{%fruits}}', 'col_7', $this->boolean()->null()->defaultValue(null)->after('name')); + $this->addColumn('{{%fruits}}', 'col_8', $this->boolean()->null()->defaultValue(null)->after('col_7')); + $this->dropColumn('{{%fruits}}', 'colour'); + $this->dropColumn('{{%fruits}}', 'description'); + } + + public function down() + { + $this->addColumn('{{%fruits}}', 'description', $this->tinyInteger(1)->null()->defaultValue(null)->after('name')); + $this->addColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null)->after('description')); + $this->dropColumn('{{%fruits}}', 'col_8'); + $this->dropColumn('{{%fruits}}', 'col_7'); + $this->dropColumn('{{%fruits}}', 'col_6'); + } +} + +PHP; + + $this->for58($schema, $expected, $columns); + } + + // This test fails. See description of https://github.com/php-openapi/yii2-openapi/pull/59 +// public function test58Add3Del2Move3ColAtDiffPos() +// { +// $columns = [ +// 'id' => 'pk', +// 'name' => 'bool null', +// 'description' => 'bool null', +// 'colour' => 'bool null', +// 'size' => 'bool null', +// 'col_6' => 'bool null', +// ]; +// +// $schema = <<for58($schema, $expected, $columns); +// } }