diff --git a/Makefile b/Makefile index 9f6547a4..44086f91 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,6 @@ up: docker-compose up -d echo "Waiting for mariadb to start up..." docker-compose exec -T mysql timeout 60s sh -c "while ! (mysql -udbuser -pdbpass -h maria --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1) - echo "Create another test DB for PgSQL ..." # created because of enum in PgSQL are different than MySQL cli: docker-compose exec php bash diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 2eb47ba1..271e86ee 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -166,7 +166,10 @@ public function getAlterExpression(bool $addUsingExpression = false):string return "'" . $this->rawParts['type'] . "'"; } if ($addUsingExpression && ApiGenerator::isPostgres()) { - return "'" . $this->rawParts['type'] . " ".$this->rawParts['nullable'] + return "'" . $this->rawParts['type'] . + ($this->alterByXDbType ? + '' : + " ".$this->rawParts['nullable']) .' USING "'.$this->column->name.'"::'.$this->typeWithoutSize($this->rawParts['type'])."'"; } diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index b0f8475f..3fdd0d32 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -400,20 +400,15 @@ protected function unPrefixTableName(string $tableName):string return str_replace($this->db->tablePrefix, '', $tableName); } - protected function isNeedUsingExpression(string $fromType, string $toType):bool + protected function isNeedUsingExpression(string $fromDbType, string $toDbType):bool { - $strings = ['string', 'text', 'char']; - if (in_array($fromType, $strings) && in_array($toType, $strings)) { + if ($fromDbType === $toDbType) { return false; } - $ints = ['smallint', 'integer', 'bigint', 'float', 'decimal']; - if (in_array($fromType, $ints) && in_array($toType, $ints)) { - return false; - } - $dates = ['date', 'timestamp']; - return !(in_array($fromType, $dates) && in_array($toType, $dates)); + return true; } + // temporary save new/changed/desired column to temporary table. If saved we can fetch it from DB and then it can be used to compare with current column public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema { $tableName = 'tmp_table_'; diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index f9627cf6..d1a314bf 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -67,8 +67,8 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir , 'dbType', 'phpType' , 'precision', 'scale', 'unsigned' ], $changed))) { - $addUsing = $this->isNeedUsingExpression($current->type, $desired->type); - $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired)); + $addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType); + $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired, $addUsing)); $this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing)); } if (in_array('allowNull', $changed, true)) { diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index bb77194c..9f2b8dd9 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -104,4 +104,26 @@ protected function checkFiles(array $actual, array $expected) $this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath"); } } + + protected function runActualMigrations(string $db = 'mysql', int $number = 2): void + { + // up + exec('cd tests; ./yii migrate-'.$db.' --interactive=0', $upOutput, $upExitCode); + $last = count($upOutput) - 1; + $lastThird = count($upOutput) - 3; + $this->assertSame($upExitCode, 0); + $this->assertSame($upOutput[$last], 'Migrated up successfully.'); + $this->assertSame($upOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' applied.'); + // 1 migration was applied. + // 2 migrations were applied. + + // down + exec('cd tests; ./yii migrate-'.$db.'/down --interactive=0 '.$number, $downOutput, $downExitCode); + $last = count($downOutput) - 1; + $lastThird = count($downOutput) - 3; + $this->assertSame($downExitCode, 0); + $this->assertSame($downOutput[$last], 'Migrated down successfully.'); + $this->assertSame($downOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' reverted.'); + + } } diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php index 22a0029b..20d47cbd 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php @@ -12,16 +12,16 @@ public function safeUp() $this->addColumn('{{%v2_posts}}', 'lang', 'enum_lang NULL DEFAULT \'ru\''); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT"); - $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); - $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()); + $this->alterColumn('{{%v2_posts}}', 'category_id', 'bigint NOT NULL USING "category_id"::bigint'); + $this->alterColumn('{{%v2_posts}}', 'created_by_id', 'bigint NULL USING "created_by_id"::bigint'); $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); } public function safeDown() { $this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); - $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->integer()->null()); - $this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer()->notNull()); + $this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int4 NULL USING "created_by_id"::int4'); + $this->alterColumn('{{%v2_posts}}', 'category_id', 'int4 NOT NULL USING "category_id"::int4'); $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull()); $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000003_change_table_v2_categories.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000003_change_table_v2_categories.php index 6e30984f..79990cfd 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000003_change_table_v2_categories.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000003_change_table_v2_categories.php @@ -9,7 +9,7 @@ public function safeUp() { $this->addColumn('{{%v2_categories}}', 'cover', $this->text()->notNull()); $this->alterColumn('{{%v2_categories}}', 'active', "DROP DEFAULT"); - $this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull()); + $this->alterColumn('{{%v2_categories}}', 'title', 'string(100) NOT NULL USING "title"::string'); $this->dropIndex('v2_categories_title_key', '{{%v2_categories}}'); $this->createIndex('v2_categories_title_index', '{{%v2_categories}}', 'title', false); } @@ -18,7 +18,7 @@ public function safeDown() { $this->dropIndex('v2_categories_title_index', '{{%v2_categories}}'); $this->createIndex('v2_categories_title_key', '{{%v2_categories}}', 'title', true); - $this->alterColumn('{{%v2_categories}}', 'title', $this->string(255)->notNull()); + $this->alterColumn('{{%v2_categories}}', 'title', 'varchar(255) NOT NULL USING "title"::varchar'); $this->dropColumn('{{%v2_categories}}', 'cover'); $this->alterColumn('{{%v2_categories}}', 'active', "SET DEFAULT 'f'"); } diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000004_change_table_v2_users.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000004_change_table_v2_users.php index 0b58811c..4651987c 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000004_change_table_v2_users.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000004_change_table_v2_users.php @@ -24,7 +24,7 @@ public function safeDown() $this->dropIndex('v2_users_role_flags_hash_index', '{{%v2_users}}'); $this->dropIndex('v2_users_login_key', '{{%v2_users}}'); $this->createIndex('v2_users_username_key', '{{%v2_users}}', 'username', true); - $this->alterColumn('{{%v2_users}}', 'role', $this->string(20)->null()); + $this->alterColumn('{{%v2_users}}', 'role', 'varchar(20) NULL USING "role"::varchar'); $this->alterColumn('{{%v2_users}}', 'email', $this->string(200)->notNull()); $this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull()); $this->dropColumn('{{%v2_users}}', 'login'); diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000005_change_table_v2_comments.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000005_change_table_v2_comments.php index 3d24ee30..782080d7 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000005_change_table_v2_comments.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000005_change_table_v2_comments.php @@ -11,10 +11,10 @@ public function safeUp() $this->dropForeignKey('fk_v2_comments_post_id_v2_posts_uid', '{{%v2_comments}}'); $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)); $this->dropColumn('{{%v2_comments}}', 'author_id'); - $this->alterColumn('{{%v2_comments}}', 'created_at', $this->timestamp()->notNull()); - $this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull()); + $this->alterColumn('{{%v2_comments}}', 'created_at', 'datetime NOT NULL USING "created_at"::datetime'); + $this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text'); $this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT"); - $this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null()); + $this->alterColumn('{{%v2_comments}}', 'meta_data', 'string(300) NULL USING "meta_data"::string'); $this->alterColumn('{{%v2_comments}}', 'meta_data', "DROP NOT NULL"); $this->alterColumn('{{%v2_comments}}', 'meta_data', "SET DEFAULT ''"); $this->addForeignKey('fk_v2_comments_post_id_v2_posts_id', '{{%v2_comments}}', 'post_id', '{{%v2_posts}}', 'id'); diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php index 5069d918..52454746 100644 --- a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php @@ -11,7 +11,7 @@ public function safeUp() $this->alterColumn('{{%editcolumns}}', 'connection', 'enum_connection USING "connection"::enum_connection'); $this->alterColumn('{{%editcolumns}}', 'connection', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'connection', "SET DEFAULT 'WIRED'"); - $this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null()); + $this->alterColumn('{{%editcolumns}}', 'device', 'text NULL USING "device"::text'); $this->alterColumn('{{%editcolumns}}', 'device', "DROP NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'device', "DROP DEFAULT"); $this->execute('DROP TYPE enum_device'); @@ -21,7 +21,7 @@ public function safeDown() { $this->execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->alterColumn('{{%editcolumns}}', 'device', 'enum_device USING "device"::enum_device'); - $this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null()); + $this->alterColumn('{{%editcolumns}}', 'connection', 'varchar(255) NULL USING "connection"::varchar'); $this->alterColumn('{{%editcolumns}}', 'connection', "DROP NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'connection', "DROP DEFAULT"); $this->execute('DROP TYPE enum_connection'); diff --git a/tests/specs/issue_fix/float_issue/float_issue.php b/tests/specs/issue_fix/float_issue/float_issue.php new file mode 100644 index 00000000..c3be4693 --- /dev/null +++ b/tests/specs/issue_fix/float_issue/float_issue.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/float_issue/float_issue.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => false, +]; diff --git a/tests/specs/issue_fix/float_issue/float_issue.yaml b/tests/specs/issue_fix/float_issue/float_issue.yaml new file mode 100644 index 00000000..cc02ef67 --- /dev/null +++ b/tests/specs/issue_fix/float_issue/float_issue.yaml @@ -0,0 +1,28 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: float_issue +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Fruit: + type: object + description: Test model for float_issue + required: + - id + - name + properties: + id: + type: integer + vat_percent: + type: number + format: float + default: 0 diff --git a/tests/specs/issue_fix/no_syntax_error_107/mysql/app/migrations_mysql_db/m200000_000000_change_table_fruits.php b/tests/specs/issue_fix/no_syntax_error_107/mysql/app/migrations_mysql_db/m200000_000000_change_table_fruits.php new file mode 100644 index 00000000..2905aec3 --- /dev/null +++ b/tests/specs/issue_fix/no_syntax_error_107/mysql/app/migrations_mysql_db/m200000_000000_change_table_fruits.php @@ -0,0 +1,19 @@ +db->createCommand('ALTER TABLE {{%fruits}} ADD COLUMN test_emails json NOT NULL')->execute(); + $this->alterColumn('{{%fruits}}', 'name', $this->text()->notNull()); + } + + public function down() + { + $this->alterColumn('{{%fruits}}', 'name', $this->string(255)->null()->defaultValue(null)); + $this->dropColumn('{{%fruits}}', 'test_emails'); + } +} diff --git a/tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php b/tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php new file mode 100644 index 00000000..0056078e --- /dev/null +++ b/tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.yaml', + 'generateUrls' => false, + 'generateModels' => false, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => false, +]; diff --git a/tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.yaml b/tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.yaml new file mode 100644 index 00000000..cf06addd --- /dev/null +++ b/tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.yaml @@ -0,0 +1,41 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Fix https://github.com/cebe/yii2-openapi/issues/107 Migrations are generated with syntax error and wrong data type in MySQL +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Fruit: + type: object + description: A table to fix \#107 + required: + - id + - name + properties: + id: + type: integer + name: + # type: array + # x-db-type: JSON + # nullable: false + # default: '{}' + + type: array + x-db-type: text + nullable: false + # default: '{}' + + + test_emails: + type: array + x-db-type: JSON + nullable: false + default: '[]' diff --git a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_editcolumns.php b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_editcolumns.php index f753b3fe..1c34a89a 100644 --- a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_editcolumns.php +++ b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_editcolumns.php @@ -11,29 +11,29 @@ public function safeUp() $this->db->createCommand('ALTER TABLE {{%editcolumns}} ADD COLUMN "json_col_def_n" json NOT NULL DEFAULT \'[]\'')->execute(); $this->db->createCommand('ALTER TABLE {{%editcolumns}} ADD COLUMN "json_col_def_n_2" json NOT NULL DEFAULT \'[]\'')->execute(); $this->db->createCommand('ALTER TABLE {{%editcolumns}} ADD COLUMN "text_col_array" text[] NULL DEFAULT NULL')->execute(); - $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN "dec_col" SET DATA TYPE decimal(12,2)')->execute(); + $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN "dec_col" SET DATA TYPE decimal(12,2) USING "dec_col"::decimal(12,2)')->execute(); $this->alterColumn('{{%editcolumns}}', 'dec_col', "SET DEFAULT 3.14"); - $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN "json_col" SET DATA TYPE text')->execute(); + $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN "json_col" SET DATA TYPE text USING "json_col"::text')->execute(); $this->alterColumn('{{%editcolumns}}', 'json_col', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'json_col', "SET DEFAULT 'fox jumps over dog'"); $this->alterColumn('{{%editcolumns}}', 'json_col_2', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'json_col_2', "SET DEFAULT '[]'"); $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN "name" SET DATA TYPE varchar(254)')->execute(); $this->alterColumn('{{%editcolumns}}', 'name', "SET DEFAULT 'Horse-2'"); - $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN "numeric_col" SET DATA TYPE double precision')->execute(); + $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN "numeric_col" SET DATA TYPE double precision USING "numeric_col"::double precision')->execute(); $this->alterColumn('{{%editcolumns}}', 'str_col_def', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'str_col_def', "DROP DEFAULT"); - $this->alterColumn('{{%editcolumns}}', 'string_col', $this->text()->null()); + $this->alterColumn('{{%editcolumns}}', 'string_col', 'text NULL USING "string_col"::text'); $this->alterColumn('{{%editcolumns}}', 'string_col', "DROP NOT NULL"); } public function safeDown() { - $this->alterColumn('{{%editcolumns}}', 'string_col', $this->string(255)->notNull()); + $this->alterColumn('{{%editcolumns}}', 'string_col', 'varchar(255) NOT NULL USING "string_col"::varchar'); $this->alterColumn('{{%editcolumns}}', 'numeric_col', 'int4 NULL USING "numeric_col"::int4'); $this->alterColumn('{{%editcolumns}}', 'name', $this->string(255)->notNull()); $this->alterColumn('{{%editcolumns}}', 'json_col', 'jsonb NULL USING "json_col"::jsonb'); - $this->alterColumn('{{%editcolumns}}', 'dec_col', $this->decimal()->null()); + $this->alterColumn('{{%editcolumns}}', 'dec_col', 'numeric NULL USING "dec_col"::numeric'); $this->dropColumn('{{%editcolumns}}', 'text_col_array'); $this->dropColumn('{{%editcolumns}}', 'json_col_def_n_2'); $this->dropColumn('{{%editcolumns}}', 'json_col_def_n'); diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 247ee344..82546392 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -30,6 +30,7 @@ public function testFresh() 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 3); $this->changeDbToMariadb(); $this->deleteTables(); @@ -43,6 +44,7 @@ public function testFresh() 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('maria', 3); $this->changeDbToPgsql(); $this->deleteTables(); @@ -56,6 +58,7 @@ public function testFresh() 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 3); } public function testAddNewColumn() // and drop enum column @@ -73,6 +76,7 @@ public function testAddNewColumn() // and drop enum column 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 3); // Mariadb $this->changeDbToMariadb(); @@ -88,6 +92,7 @@ public function testAddNewColumn() // and drop enum column 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('maria', 3); // Pgsql $this->changeDbToPgsql(); @@ -103,6 +108,7 @@ public function testAddNewColumn() // and drop enum column 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 3); } public function testChangeToAndFromEnum() // edit enum to string and vice versa @@ -119,6 +125,7 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 3); // Mariadb $this->changeDbToMariadb(); @@ -134,6 +141,7 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('maria', 3); $this->changeDbToPgsql(); @@ -149,6 +157,7 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 3); } // TODO ENH enum change is more work than just changing the eunm values. And for PgSQL it is even more diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php new file mode 100644 index 00000000..4e0cb966 --- /dev/null +++ b/tests/unit/IssueFixTest.php @@ -0,0 +1,71 @@ +deleteTablesForNoSyntaxError107(); + $this->createTableForNoSyntaxError107(); + $this->runGenerator($testFile, 'mysql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 1); + } + + private function deleteTablesForNoSyntaxError107() + { + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute(); + } + + private function createTableForNoSyntaxError107() + { + Yii::$app->db->createCommand()->createTable('{{%fruits}}', [ + 'id' => 'pk', + 'name' => 'varchar(255)', + ])->execute(); + } + + public function testFloatIssue() + { + // test no migrations are generaeted + $this->changeDbToPgsql(); + $this->deleteTablesForFloatIssue(); + $this->createTableForFloatIssue(); + $testFile = Yii::getAlias("@specs/issue_fix/float_issue/float_issue.php"); + $this->runGenerator($testFile, 'pgsql'); + $this->expectException(\yii\base\InvalidArgumentException::class); + FileHelper::findDirectories(Yii::getAlias('@app').'/migration'); + FileHelper::findDirectories(Yii::getAlias('@app').'/migrations'); + FileHelper::findDirectories(Yii::getAlias('@app').'/migrations_mysql_db'); + FileHelper::findDirectories(Yii::getAlias('@app').'/migrations_maria_db'); + FileHelper::findDirectories(Yii::getAlias('@app').'/migrations_pgsql_db'); + } + + private function deleteTablesForFloatIssue() + { + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute(); + } + + private function createTableForFloatIssue() + { + Yii::$app->db->createCommand()->createTable('{{%fruits}}', [ + 'id' => 'pk', + 'vat_percent' => 'float default 0', + ])->execute(); + } +} diff --git a/tests/unit/XDbTypeTest.php b/tests/unit/XDbTypeTest.php index 7f492c22..7be7198e 100644 --- a/tests/unit/XDbTypeTest.php +++ b/tests/unit/XDbTypeTest.php @@ -29,6 +29,7 @@ public function testXDbTypeFresh() 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 4); // same yaml file is used for MySQL and MariaDB ---------------------- $this->changeDbToMariadb(); @@ -43,6 +44,7 @@ public function testXDbTypeFresh() 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('maria', 4); // PgSQL ------------------------------------------------ $this->changeDbToPgsql(); @@ -57,6 +59,7 @@ public function testXDbTypeFresh() 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 4); } public function testXDbTypeSecondaryWithNewColumn() // v2 @@ -74,6 +77,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 4); // same yaml file is used for MySQL and MariaDB ---------------------- $this->changeDbToMariadb(); @@ -90,6 +94,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('maria', 4); // PgSQL ------------------------------------------------ $this->changeDbToPgsql(); @@ -106,6 +111,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 4); } public function testXDbTypeSecondaryWithEditColumn() // v3 @@ -123,6 +129,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 4); // same yaml file is used for MySQL and MariaDB ---------------------- $this->changeDbToMariadb(); @@ -139,6 +146,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('maria', 4); // PgSQL ------------------------------------------------ $this->changeDbToPgsql(); @@ -155,6 +163,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3 'recursive' => true, ]); $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 4); } private function deleteTables()