Skip to content

Commit 8250c09

Browse files
committed
Fix errors in SOHELAHMED7#10
1 parent e2fc2e9 commit 8250c09

23 files changed

+54
-20
lines changed

src/lib/ColumnToCode.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function getAlterExpression(bool $addUsingExpression = false):string
170170
if ($this->isEnum() && ApiGenerator::isPostgres()) {
171171
$rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
172172
$enumTypeName = 'enum_'.$rawTableName.'_'.$this->column->name;
173-
return "'" . sprintf($enumTypeName.' USING "%1$s"::"'.$enumTypeName.'"', $this->column->name) . "'";
173+
return "'" . sprintf('"'.$enumTypeName.'" USING "%1$s"::"'.$enumTypeName.'"', $this->column->name) . "'";
174174
}
175175
if ($this->column->dbType === 'tsvector') {
176176
return "'" . $this->rawParts['type'] . "'";
@@ -379,7 +379,7 @@ private function resolveEnumType():void
379379
{
380380
if (ApiGenerator::isPostgres()) {
381381
$rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
382-
$this->rawParts['type'] = 'enum_'.$rawTableName.'_' . $this->column->name;
382+
$this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
383383
return;
384384
}
385385
$this->rawParts['type'] = 'enum(' . self::mysqlEnumToString($this->column->enumValues) . ')';

src/lib/migrations/BaseMigrationBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch
416416
return '"tmp_enum_'.$columnName.'_"';
417417
};
418418
$rawTableName = $this->db->schema->getRawTableName($tableAlias);
419-
$innerEnumTypeName = "enum_{$tmpTableName}_$columnSchema->name";
419+
$innerEnumTypeName = "\"enum_{$tmpTableName}_{$columnSchema->name}\"";
420420

421421
Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tmpTableName)->execute();
422422

tests/migrations/m100000_000000_pgsql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function safeUp()
102102
'json2' => $this->json()->null()->defaultValue(null),
103103
'json3' => $this->json()->defaultValue(Json::encode(['foo' => 'bar', 'bar' => 'baz'])),
104104
'json4' => "json DEFAULT '" . new Expression(Json::encode(['ffo' => 'bar'])) . "'",
105-
'status' => $enumTypeName,
105+
'status' => '"'.$enumTypeName.'"',
106106
'search' => 'tsvector'
107107
]);
108108
$columns = [

tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function safeUp()
99
{
1010
$this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey());
1111
$this->execute('CREATE TYPE "enum_itt_v2_posts_lang" AS ENUM(\'ru\', \'eng\')');
12-
$this->addColumn('{{%v2_posts}}', 'lang', 'enum_itt_v2_posts_lang NULL DEFAULT \'ru\'');
12+
$this->addColumn('{{%v2_posts}}', 'lang', '"enum_itt_v2_posts_lang" NULL DEFAULT \'ru\'');
1313
$this->dropColumn('{{%v2_posts}}', 'uid');
1414
$this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT");
1515
$this->alterColumn('{{%v2_posts}}', 'category_id', 'bigint NOT NULL USING "category_id"::bigint');

tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function safeUp()
1111
$this->createTable('{{%v2_tags}}', [
1212
'id' => $this->bigPrimaryKey(),
1313
0 => '"name" varchar(100) NOT NULL',
14-
'lang' => 'enum_itt_v2_tags_lang NOT NULL',
14+
'lang' => '"enum_itt_v2_tags_lang" NOT NULL',
1515
]);
1616
$this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true);
1717
}

tests/specs/blog_v2/migrations_pgsql_db/m200000_000004_change_table_v2_users.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function safeUp()
1212
$this->dropColumn('{{%v2_users}}', 'username');
1313
$this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT");
1414
$this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN "email" SET DATA TYPE varchar(255)')->execute();
15-
$this->alterColumn('{{%v2_users}}', 'role', 'enum_itt_v2_users_role USING "role"::"enum_itt_v2_users_role"');
15+
$this->alterColumn('{{%v2_users}}', 'role', '"enum_itt_v2_users_role" USING "role"::"enum_itt_v2_users_role"');
1616
$this->alterColumn('{{%v2_users}}', 'role', "DROP DEFAULT");
1717
$this->dropIndex('v2_users_username_key', '{{%v2_users}}');
1818
$this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true);

tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration
77
{
88
public function up()
99
{
10+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'');
1011
$this->alterColumn('{{%editcolumns}}', 'connection', 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'');
1112
$this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null()->defaultValue(null));
1213
}
@@ -15,5 +16,6 @@ public function down()
1516
{
1617
$this->alterColumn('{{%editcolumns}}', 'device', 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'');
1718
$this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null()->defaultValue(null));
19+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', $this->string(255)->null()->defaultValue(null));
1820
}
1921
}

tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration
77
{
88
public function up()
99
{
10+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'');
1011
$this->alterColumn('{{%editcolumns}}', 'connection', 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'');
1112
$this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null());
1213
}
@@ -15,5 +16,6 @@ public function down()
1516
{
1617
$this->alterColumn('{{%editcolumns}}', 'device', 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'');
1718
$this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null()->defaultValue(null));
19+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', $this->string(255)->null()->defaultValue(null));
1820
}
1921
}

tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration
88
public function safeUp()
99
{
1010
$this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')');
11-
$this->alterColumn('{{%editcolumns}}', 'connection', 'enum_itt_editcolumns_connection USING "connection"::"enum_itt_editcolumns_connection"');
11+
$this->execute('CREATE TYPE "enum_itt_editcolumns_camelCaseCol" AS ENUM(\'ONE\', \'TWO\', \'THREE\')');
12+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', '"enum_itt_editcolumns_camelCaseCol" USING "camelCaseCol"::"enum_itt_editcolumns_camelCaseCol"');
13+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "SET NOT NULL");
14+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "SET DEFAULT 'TWO'");
15+
$this->alterColumn('{{%editcolumns}}', 'connection', '"enum_itt_editcolumns_connection" USING "connection"::"enum_itt_editcolumns_connection"');
1216
$this->alterColumn('{{%editcolumns}}', 'connection', "SET NOT NULL");
1317
$this->alterColumn('{{%editcolumns}}', 'connection', "SET DEFAULT 'WIRED'");
1418
$this->alterColumn('{{%editcolumns}}', 'device', 'text NULL USING "device"::text');
@@ -20,8 +24,12 @@ public function safeUp()
2024
public function safeDown()
2125
{
2226
$this->execute('CREATE TYPE "enum_itt_editcolumns_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')');
23-
$this->alterColumn('{{%editcolumns}}', 'device', 'enum_itt_editcolumns_device USING "device"::"enum_itt_editcolumns_device"');
27+
$this->alterColumn('{{%editcolumns}}', 'device', '"enum_itt_editcolumns_device" USING "device"::"enum_itt_editcolumns_device"');
2428
$this->alterColumn('{{%editcolumns}}', 'connection', 'varchar(255) NULL USING "connection"::varchar');
29+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', 'varchar(255) NULL USING "camelCaseCol"::varchar');
30+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "DROP NOT NULL");
31+
$this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "DROP DEFAULT");
32+
$this->execute('DROP TYPE "enum_itt_editcolumns_camelCaseCol"');
2533
$this->alterColumn('{{%editcolumns}}', 'connection', "DROP NOT NULL");
2634
$this->alterColumn('{{%editcolumns}}', 'connection', "DROP DEFAULT");
2735
$this->execute('DROP TYPE "enum_itt_editcolumns_connection"');

tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function safeUp()
1010
$this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')');
1111
$this->createTable('{{%newcolumns}}', [
1212
'id' => $this->primaryKey(),
13-
'new_column' => 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\'',
13+
'new_column' => '"enum_itt_newcolumns_new_column" NOT NULL DEFAULT \'ONE\'',
1414
]);
1515
}
1616

tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function safeUp()
1010
$this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')');
1111
$this->createTable('{{%pristines}}', [
1212
'id' => $this->primaryKey(),
13-
'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'',
13+
'device' => '"enum_itt_pristines_device" NOT NULL DEFAULT \'TV\'',
1414
]);
1515
}
1616

tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function up()
1111
'id' => $this->primaryKey(),
1212
'device' => $this->text()->null()->defaultValue(null),
1313
'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'',
14+
'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'',
1415
]);
1516
}
1617

tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function up()
1111
'id' => $this->primaryKey(),
1212
'device' => $this->text()->null(),
1313
'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'',
14+
'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'',
1415
]);
1516
}
1617

tests/specs/enum/fresh/mysql/enum.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ components:
7878
default:
7979
WIRED
8080
nullable: false
81+
camelCaseCol:
82+
type: string
83+
enum:
84+
- ONE
85+
- TWO
86+
- THREE
87+
default:
88+
TWO
89+
nullable: false
8190

8291
# Enumvaluechange:
8392
# type: object

tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration
77
{
88
public function safeUp()
99
{
10+
$this->execute('CREATE TYPE "enum_itt_editcolumns_camelCaseCol" AS ENUM(\'ONE\', \'TWO\', \'THREE\')');
1011
$this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')');
1112
$this->createTable('{{%editcolumns}}', [
1213
'id' => $this->primaryKey(),
1314
'device' => $this->text()->null()->defaultValue(null),
14-
'connection' => 'enum_itt_editcolumns_connection NOT NULL DEFAULT \'WIRED\'',
15+
'connection' => '"enum_itt_editcolumns_connection" NOT NULL DEFAULT \'WIRED\'',
16+
'camelCaseCol' => '"enum_itt_editcolumns_camelCaseCol" NOT NULL DEFAULT \'TWO\'',
1517
]);
1618
}
1719

1820
public function safeDown()
1921
{
2022
$this->dropTable('{{%editcolumns}}');
2123
$this->execute('DROP TYPE "enum_itt_editcolumns_connection"');
24+
$this->execute('DROP TYPE "enum_itt_editcolumns_camelCaseCol"');
2225
}
2326
}

tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function safeUp()
1010
$this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')');
1111
$this->createTable('{{%newcolumns}}', [
1212
'id' => $this->primaryKey(),
13-
'new_column' => 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\'',
13+
'new_column' => '"enum_itt_newcolumns_new_column" NOT NULL DEFAULT \'ONE\'',
1414
]);
1515
}
1616

tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function safeUp()
1010
$this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')');
1111
$this->createTable('{{%pristines}}', [
1212
'id' => $this->primaryKey(),
13-
'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'',
13+
'device' => '"enum_itt_pristines_device" NOT NULL DEFAULT \'TV\'',
1414
]);
1515
}
1616

tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function up()
1111
'id' => $this->primaryKey(),
1212
'device' => $this->text()->null()->defaultValue(null),
1313
'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'',
14+
'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'',
1415
]);
1516
}
1617

tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function up()
1111
'id' => $this->primaryKey(),
1212
'device' => $this->text()->null(),
1313
'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'',
14+
'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'',
1415
]);
1516
}
1617

tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration
77
{
88
public function safeUp()
99
{
10+
$this->execute('CREATE TYPE "enum_itt_editcolumns_camelCaseCol" AS ENUM(\'ONE\', \'TWO\', \'THREE\')');
1011
$this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')');
1112
$this->createTable('{{%editcolumns}}', [
1213
'id' => $this->primaryKey(),
1314
'device' => $this->text()->null()->defaultValue(null),
14-
'connection' => 'enum_itt_editcolumns_connection NOT NULL DEFAULT \'WIRED\'',
15+
'connection' => '"enum_itt_editcolumns_connection" NOT NULL DEFAULT \'WIRED\'',
16+
'camelCaseCol' => '"enum_itt_editcolumns_camelCaseCol" NOT NULL DEFAULT \'TWO\'',
1517
]);
1618
}
1719

1820
public function safeDown()
1921
{
2022
$this->dropTable('{{%editcolumns}}');
2123
$this->execute('DROP TYPE "enum_itt_editcolumns_connection"');
24+
$this->execute('DROP TYPE "enum_itt_editcolumns_camelCaseCol"');
2225
}
2326
}

tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ class m200000_000001_change_table_newcolumns extends \yii\db\Migration
88
public function safeUp()
99
{
1010
$this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')');
11-
$this->addColumn('{{%newcolumns}}', 'new_column', 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\'');
11+
$this->addColumn('{{%newcolumns}}', 'new_column', '"enum_itt_newcolumns_new_column" NOT NULL DEFAULT \'ONE\'');
1212
$this->dropColumn('{{%newcolumns}}', 'delete_col');
1313
$this->execute('DROP TYPE "enum_itt_newcolumns_delete_col"');
1414
}
1515

1616
public function safeDown()
1717
{
1818
$this->execute('CREATE TYPE "enum_itt_newcolumns_delete_col" AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')');
19-
$this->addColumn('{{%newcolumns}}', 'delete_col', 'enum_itt_newcolumns_delete_col NULL DEFAULT NULL');
19+
$this->addColumn('{{%newcolumns}}', 'delete_col', '"enum_itt_newcolumns_delete_col" NULL DEFAULT NULL');
2020
$this->dropColumn('{{%newcolumns}}', 'new_column');
2121
$this->execute('DROP TYPE "enum_itt_newcolumns_new_column"');
2222
}

tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function safeUp()
1010
$this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')');
1111
$this->createTable('{{%pristines}}', [
1212
'id' => $this->primaryKey(),
13-
'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'',
13+
'device' => '"enum_itt_pristines_device" NOT NULL DEFAULT \'TV\'',
1414
]);
1515
}
1616

tests/unit/EnumTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ private function deleteTables()
205205
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_pristines_device CASCADE')->execute();
206206
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_editcolumns_device CASCADE')->execute();
207207
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_editcolumns_connection CASCADE')->execute();
208+
Yii::$app->db->createCommand('DROP TYPE IF EXISTS "enum_itt_editcolumns_camelCaseCol" CASCADE')->execute();
208209
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_newcolumns_new_column CASCADE')->execute();
209210
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_newcolumns_delete_col CASCADE')->execute();
210211
}
@@ -222,14 +223,16 @@ private function createTableForEditEnumToString() // and vice versa
222223
Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [
223224
'id' => 'pk',
224225
'device' => 'enum_itt_editcolumns_device NOT NULL DEFAULT \'TV\'',
225-
'connection' => 'string'
226+
'connection' => 'string',
227+
'camelCaseCol' => 'string',
226228
])->execute();
227229
return;
228230
}
229231
Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [
230232
'id' => 'pk',
231233
'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'',
232-
'connection' => 'string'
234+
'connection' => 'string',
235+
'camelCaseCol' => 'string',
233236
])->execute();
234237
}
235238

0 commit comments

Comments
 (0)