Skip to content

Resolve: Bug: add/remove property and at the same time change it at x-indexes: #3 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -224,9 +230,7 @@ function (string $unknownColumn) {
}
$this->buildColumnChanges($current, $desired, $changedAttributes);
}
if (!$relation) {
$this->buildIndexChanges();
}

if ($relation) {
$this->buildRelationsForJunction($relation);
} else {
Expand Down
17 changes: 16 additions & 1 deletion tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@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,
];
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Table for Address
*/
class m200000_000000_change_table_addresses extends \yii\db\Migration
{
public function up()
{
$this->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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Address extends \app\models\base\Address
{


}

Loading
Loading