diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index f059c9f4..831f6f3d 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -209,7 +209,13 @@ function (string $unknownColumn) { ->addDownCode($builder->addPrimaryKey($tableName, $this->model->junctionCols)); } } + + if (!$relation) { + $this->buildIndexChanges(); + } + $this->buildColumnsDrop($columnsForDrop); + foreach ($columnsForChange as $commonColumn) { $current = $this->tableSchema->columns[$commonColumn]; /** @var \cebe\yii2openapi\db\ColumnSchema|\yii\db\ColumnSchema $desired */ @@ -224,9 +230,7 @@ function (string $unknownColumn) { } $this->buildColumnChanges($current, $desired, $changedAttributes); } - if (!$relation) { - $this->buildIndexChanges(); - } + if ($relation) { $this->buildRelationsForJunction($relation); } else { diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index 3c02fa95..fd3eebb4 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -4,6 +4,7 @@ use cebe\yii2openapi\generator\ApiGenerator; use Yii; +use yii\db\IndexConstraint; use yii\di\Container; use yii\db\mysql\Schema as MySqlSchema; use yii\db\pgsql\Schema as PgSqlSchema; @@ -167,8 +168,8 @@ protected function runUpMigrations(string $db = 'mysql', int $number = 2): void exec('cd tests; php -dxdebug.mode=develop ./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($upExitCode, 0); $this->assertSame($upOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' applied.'); // 1 migration was applied. // 2 migrations were applied. @@ -192,4 +193,18 @@ protected function dropFkIfExists(string $table, string $fk): void Yii::$app->db->createCommand()->dropForeignKey($fk, $table)->execute(); } } + + protected function indexExists(string $indexName): bool + { + $indices = Yii::$app->db->schema->schemaIndexes; + foreach ($indices as $subIndices) { + foreach ($subIndices as $index) { + /** @var IndexConstraint $index */ + if ($index->name === $indexName) { + return true; + } + } + } + return false; + } } diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php index 5c43dabf..5f50d0d7 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php @@ -9,20 +9,20 @@ public function up() { $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\' AFTER slug'); + $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)); - $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); } public function down() { - $this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->integer(11)->null()->defaultValue(null)); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()->defaultValue(0)); $this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer(11)->notNull()); $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger(20)->notNull()->first()); + $this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); } diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000003_change_table_v2_categories.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000003_change_table_v2_categories.php index e608ed52..a91537a9 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000003_change_table_v2_categories.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000003_change_table_v2_categories.php @@ -8,18 +8,18 @@ class m200000_000003_change_table_v2_categories extends \yii\db\Migration public function up() { $this->addColumn('{{%v2_categories}}', 'cover', $this->text()->notNull()->after('title')); - $this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull()); - $this->alterColumn('{{%v2_categories}}', 'active', $this->tinyInteger(1)->notNull()); $this->dropIndex('v2_categories_title_key', '{{%v2_categories}}'); $this->createIndex('v2_categories_title_index', '{{%v2_categories}}', 'title', false); + $this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull()); + $this->alterColumn('{{%v2_categories}}', 'active', $this->tinyInteger(1)->notNull()); } public function down() { - $this->dropIndex('v2_categories_title_index', '{{%v2_categories}}'); - $this->createIndex('v2_categories_title_key', '{{%v2_categories}}', 'title', true); $this->alterColumn('{{%v2_categories}}', 'active', $this->tinyInteger(1)->notNull()->defaultValue(0)); $this->alterColumn('{{%v2_categories}}', 'title', $this->string(255)->notNull()); + $this->dropIndex('v2_categories_title_index', '{{%v2_categories}}'); + $this->createIndex('v2_categories_title_key', '{{%v2_categories}}', 'title', true); $this->dropColumn('{{%v2_categories}}', 'cover'); } } diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000004_change_table_v2_users.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000004_change_table_v2_users.php index 1231f6db..0fc265b4 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000004_change_table_v2_users.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000004_change_table_v2_users.php @@ -8,24 +8,24 @@ class m200000_000004_change_table_v2_users extends \yii\db\Migration public function up() { $this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull()->after('id')); + $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); + $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); + $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', ["role", "flags"], 'hash'); $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'email', $this->string(255)->notNull()); $this->alterColumn('{{%v2_users}}', 'role', 'enum("admin", "editor", "reader") NULL DEFAULT NULL'); $this->alterColumn('{{%v2_users}}', 'created_at', $this->timestamp()->null()->defaultValue(null)); - $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); - $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); - $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', ["role", "flags"], 'hash'); } public function down() { - $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}}', 'created_at', $this->timestamp()->null()->defaultExpression("current_timestamp()")); $this->alterColumn('{{%v2_users}}', 'role', $this->string(20)->null()->defaultValue('reader')); $this->alterColumn('{{%v2_users}}', 'email', $this->string(200)->notNull()); $this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull()->after('id')); + $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->dropColumn('{{%v2_users}}', 'login'); } } diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php index be309829..eea03cca 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php @@ -9,20 +9,20 @@ public function up() { $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\' AFTER slug'); + $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)); - $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); } public function down() { - $this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->integer()->null()->defaultValue(null)); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()->defaultValue(0)); $this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer()->notNull()); $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull()->first()); + $this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); } diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000003_change_table_v2_categories.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000003_change_table_v2_categories.php index e608ed52..a91537a9 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000003_change_table_v2_categories.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000003_change_table_v2_categories.php @@ -8,18 +8,18 @@ class m200000_000003_change_table_v2_categories extends \yii\db\Migration public function up() { $this->addColumn('{{%v2_categories}}', 'cover', $this->text()->notNull()->after('title')); - $this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull()); - $this->alterColumn('{{%v2_categories}}', 'active', $this->tinyInteger(1)->notNull()); $this->dropIndex('v2_categories_title_key', '{{%v2_categories}}'); $this->createIndex('v2_categories_title_index', '{{%v2_categories}}', 'title', false); + $this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull()); + $this->alterColumn('{{%v2_categories}}', 'active', $this->tinyInteger(1)->notNull()); } public function down() { - $this->dropIndex('v2_categories_title_index', '{{%v2_categories}}'); - $this->createIndex('v2_categories_title_key', '{{%v2_categories}}', 'title', true); $this->alterColumn('{{%v2_categories}}', 'active', $this->tinyInteger(1)->notNull()->defaultValue(0)); $this->alterColumn('{{%v2_categories}}', 'title', $this->string(255)->notNull()); + $this->dropIndex('v2_categories_title_index', '{{%v2_categories}}'); + $this->createIndex('v2_categories_title_key', '{{%v2_categories}}', 'title', true); $this->dropColumn('{{%v2_categories}}', 'cover'); } } diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000004_change_table_v2_users.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000004_change_table_v2_users.php index 412d6ade..061ed5a7 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000004_change_table_v2_users.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000004_change_table_v2_users.php @@ -8,24 +8,24 @@ class m200000_000004_change_table_v2_users extends \yii\db\Migration public function up() { $this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull()->after('id')); + $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); + $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); + $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', ["role", "flags"], 'hash'); $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'email', $this->string(255)->notNull()); $this->alterColumn('{{%v2_users}}', 'role', 'enum("admin", "editor", "reader") NULL DEFAULT NULL'); $this->alterColumn('{{%v2_users}}', 'created_at', $this->timestamp()->null()->defaultValue(null)); - $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); - $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); - $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', ["role", "flags"], 'hash'); } public function down() { - $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}}', 'created_at', $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP")); $this->alterColumn('{{%v2_users}}', 'role', $this->string(20)->null()->defaultValue('reader')); $this->alterColumn('{{%v2_users}}', 'email', $this->string(200)->notNull()); $this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull()->after('id')); + $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->dropColumn('{{%v2_users}}', 'login'); } } 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 5188983b..9444aef9 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 @@ -10,19 +10,19 @@ public function safeUp() $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); $this->execute('CREATE TYPE "enum_itt_v2_posts_lang" AS ENUM(\'ru\', \'eng\')'); $this->addColumn('{{%v2_posts}}', 'lang', '"enum_itt_v2_posts_lang" NULL DEFAULT \'ru\''); + $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'category_id', 'int8 NOT NULL USING "category_id"::int8'); $this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT"); $this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int8 NULL USING "created_by_id"::int8'); - $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', '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->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); $this->execute('DROP TYPE "enum_itt_v2_posts_lang"'); 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 5f934936..d3958265 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 @@ -8,17 +8,17 @@ class m200000_000003_change_table_v2_categories extends \yii\db\Migration public function safeUp() { $this->addColumn('{{%v2_categories}}', 'cover', $this->text()->notNull()); - $this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull()); - $this->alterColumn('{{%v2_categories}}', 'active', "DROP DEFAULT"); $this->dropIndex('v2_categories_title_key', '{{%v2_categories}}'); $this->createIndex('v2_categories_title_index', '{{%v2_categories}}', 'title', false); + $this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull()); + $this->alterColumn('{{%v2_categories}}', 'active', "DROP DEFAULT"); } public function safeDown() { + $this->alterColumn('{{%v2_categories}}', 'title', $this->string(255)->notNull()); $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->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 a57ea8df..e8847b2d 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 @@ -9,24 +9,24 @@ public function safeUp() { $this->execute('CREATE TYPE "enum_itt_v2_users_role" AS ENUM(\'admin\', \'editor\', \'reader\')'); $this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull()); + $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); + $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); + $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', ["role", "flags"], 'hash'); $this->dropColumn('{{%v2_users}}', 'username'); $this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN "email" SET DATA TYPE varchar(255)')->execute(); $this->alterColumn('{{%v2_users}}', 'role', '"enum_itt_v2_users_role" USING "role"::"enum_itt_v2_users_role"'); $this->alterColumn('{{%v2_users}}', 'role', "DROP DEFAULT"); $this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT"); - $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); - $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); - $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', ["role", "flags"], 'hash'); } 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', '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->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->dropColumn('{{%v2_users}}', 'login'); $this->alterColumn('{{%v2_users}}', 'role', "SET DEFAULT 'reader'"); $this->execute('DROP TYPE "enum_itt_v2_users_role"'); diff --git a/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.php b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.php new file mode 100644 index 00000000..2dc7823c --- /dev/null +++ b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => false, +]; diff --git a/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.yaml b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.yaml new file mode 100644 index 00000000..3e1cc80c --- /dev/null +++ b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.yaml @@ -0,0 +1,38 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: 3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Address: + type: object + description: desc + x-indexes: + - 'unique:shortName,postCode' + required: + - id + properties: + id: + type: integer + readOnly: true + + name: + type: string + maxLength: 64 + + shortName: + type: string + maxLength: 64 + + postCode: + type: string + maxLength: 64 diff --git a/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/mysql/migrations_mysql_db/m200000_000000_change_table_addresses.php b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/mysql/migrations_mysql_db/m200000_000000_change_table_addresses.php new file mode 100644 index 00000000..d86cdcc9 --- /dev/null +++ b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/mysql/migrations_mysql_db/m200000_000000_change_table_addresses.php @@ -0,0 +1,23 @@ +addColumn('{{%addresses}}', 'postCode', $this->string(64)->null()->defaultValue(null)); + $this->dropIndex('addresses_shortName_postalCode_key', '{{%addresses}}'); + $this->createIndex('addresses_shortName_postCode_key', '{{%addresses}}', ["shortName", "postCode"], true); + $this->dropColumn('{{%addresses}}', 'postalCode'); + } + + public function down() + { + $this->addColumn('{{%addresses}}', 'postalCode', $this->string(64)->null()->defaultValue(null)); + $this->dropIndex('addresses_shortName_postCode_key', '{{%addresses}}'); + $this->createIndex('addresses_shortName_postalCode_key', '{{%addresses}}', ["shortName", "postalCode"], true); + $this->dropColumn('{{%addresses}}', 'postCode'); + } +} diff --git a/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/mysql/models/Address.php b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/mysql/models/Address.php new file mode 100644 index 00000000..abfd7d59 --- /dev/null +++ b/tests/specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/mysql/models/Address.php @@ -0,0 +1,10 @@ + [['name', 'shortName', 'postCode'], 'trim'], + 'shortName_postCode_unique' => [['shortName', 'postCode'], 'unique', 'targetAttribute' => [ + 'shortName', + 'postCode', + ]], + 'name_string' => [['name'], 'string', 'max' => 64], + 'shortName_string' => [['shortName'], 'string', 'max' => 64], + 'postCode_string' => [['postCode'], 'string', 'max' => 64], + ]; + } +} diff --git a/tests/specs/postgres_custom/migrations_pgsql_db/m200000_000000_change_table_v3_pgcustom.php b/tests/specs/postgres_custom/migrations_pgsql_db/m200000_000000_change_table_v3_pgcustom.php index 37cb7765..1356f456 100644 --- a/tests/specs/postgres_custom/migrations_pgsql_db/m200000_000000_change_table_v3_pgcustom.php +++ b/tests/specs/postgres_custom/migrations_pgsql_db/m200000_000000_change_table_v3_pgcustom.php @@ -7,6 +7,7 @@ class m200000_000000_change_table_v3_pgcustom extends \yii\db\Migration { public function safeUp() { + $this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'to_tsvector(\'english\', search::text)', 'gin'); $this->alterColumn('{{%v3_pgcustom}}', 'json1', "SET NOT NULL"); $this->alterColumn('{{%v3_pgcustom}}', 'json1', "SET DEFAULT '[]'"); $this->alterColumn('{{%v3_pgcustom}}', 'json2', "SET NOT NULL"); @@ -17,7 +18,6 @@ public function safeUp() $this->alterColumn('{{%v3_pgcustom}}', 'json4', "SET DEFAULT '{\"foo\":\"bar\",\"bar\":\"baz\"}'"); $this->alterColumn('{{%v3_pgcustom}}', 'status', "SET DEFAULT 'draft'"); $this->alterColumn('{{%v3_pgcustom}}', 'status_x', "SET DEFAULT 'draft'"); - $this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'to_tsvector(\'english\', search::text)', 'gin'); } public function safeDown() diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 9739f3ec..91a17357 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -544,6 +544,43 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim() $this->checkFiles($actualFiles, $expectedFiles); } + // https://github.com/php-openapi/yii2-openapi/issues/3 + public function test3BugAddRemovePropertyAndAtTheSameTimeChangeItAtXIndexes() + { + $this->dropTestTableFor3BugAddRemovePropertyAndAtTheSameTimeChangeItAtXIndexes(); + $this->createTestTableFor3BugAddRemovePropertyAndAtTheSameTimeChangeItAtXIndexes(); + $testFile = Yii::getAlias("@specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/index.php"); + $this->runGenerator($testFile); + $this->runActualMigrations('mysql', 1); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/3_bug_add_remove_property_and_at_the_same_time_change_it_at_x_indexes/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->dropTestTableFor3BugAddRemovePropertyAndAtTheSameTimeChangeItAtXIndexes(); + } + + private function createTestTableFor3BugAddRemovePropertyAndAtTheSameTimeChangeItAtXIndexes() + { + Yii::$app->db->createCommand()->createTable('{{%addresses}}', [ + 'id' => 'pk', + 'name' => 'varchar(64)', + 'shortName' => 'varchar(64)', + 'postalCode' => 'varchar(64)', + ])->execute(); + Yii::$app->db->createCommand()->createIndex('addresses_shortName_postalCode_key', '{{%addresses}}', ["shortName", "postalCode"], true)->execute(); + } + + private function dropTestTableFor3BugAddRemovePropertyAndAtTheSameTimeChangeItAtXIndexes() + { + if ($this->indexExists('addresses_shortName_postalCode_key')) { + Yii::$app->db->createCommand()->dropIndex('addresses_shortName_postalCode_key', '{{%addresses}}')->execute(); + } + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%addresses}}')->execute(); + } + // https://github.com/php-openapi/yii2-openapi/issues/29 public function test29ExtensionFkColumnNameCauseErrorInCaseOfColumnNameWithoutUnderscore() {