Skip to content

Commit d13479f

Browse files
committed
Do not generate ENUM types on postgres if x-db-type is specified for the enum column
fixes cebe#173
1 parent 70a449f commit d13479f

File tree

18 files changed

+48
-4
lines changed

18 files changed

+48
-4
lines changed

src/lib/ColumnToCode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function isJson():bool
226226

227227
public function isEnum():bool
228228
{
229-
return !empty($this->column->enumValues);
229+
return !empty($this->column->enumValues) && empty($this->column->xDbType);
230230
}
231231

232232
public function isDecimal()

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public function newColStr(string $tableAlias, \cebe\yii2openapi\db\ColumnSchema
479479

480480
public static function isEnum(\yii\db\ColumnSchema $columnSchema): bool
481481
{
482-
if (!empty($columnSchema->enumValues) && is_array($columnSchema->enumValues)) {
482+
if (!empty($columnSchema->enumValues) && is_array($columnSchema->enumValues) && empty($columnSchema->xDbType)) {
483483
return true;
484484
}
485485
return false;

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ protected function createEnumMigrations():void
147147
$tableAlias = $this->model->getTableAlias();
148148
$enums = $this->model->getEnumAttributes();
149149
foreach ($enums as $attr) {
150+
if (!empty($attr->xDbType)) {
151+
// do not generate enum types when custom x-db-type is used
152+
continue;
153+
}
150154
$this->migration
151155
->addUpCode($this->recordBuilder->createEnum($tableAlias, $attr->columnName, $attr->enumValues), true)
152156
->addDownCode($this->recordBuilder->dropEnum($tableAlias, $attr->columnName), true);

tests/migrations/m100000_000000_pgsql.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public function safeUp()
103103
'json3' => $this->json()->defaultValue(Json::encode(['foo' => 'bar', 'bar' => 'baz'])),
104104
'json4' => "json DEFAULT '" . new Expression(Json::encode(['ffo' => 'bar'])) . "'",
105105
'status' => '"'.$enumTypeName.'"',
106+
'status_x' => 'varchar(10)',
106107
'search' => 'tsvector'
107108
]);
108109
$columns = [

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function up()
1010
$this->createTable('{{%newcolumns}}', [
1111
'id' => $this->primaryKey(),
1212
'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'',
13+
0 => 'new_column_x varchar(10) NOT NULL DEFAULT \'ONE\'',
1314
]);
1415
}
1516

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function up()
1010
$this->createTable('{{%newcolumns}}', [
1111
'id' => $this->primaryKey(),
1212
'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'',
13+
0 => 'new_column_x varchar(10) NOT NULL DEFAULT \'ONE\'',
1314
]);
1415
}
1516

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function safeUp()
1111
$this->createTable('{{%newcolumns}}', [
1212
'id' => $this->primaryKey(),
1313
'new_column' => '"enum_itt_newcolumns_new_column" NOT NULL DEFAULT \'ONE\'',
14+
0 => '"new_column_x" varchar(10) NOT NULL DEFAULT \'ONE\'',
1415
]);
1516
}
1617

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function up()
1010
$this->createTable('{{%newcolumns}}', [
1111
'id' => $this->primaryKey(),
1212
'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'',
13+
0 => 'new_column_x varchar(10) NOT NULL DEFAULT \'ONE\'',
1314
]);
1415
}
1516

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function up()
1010
$this->createTable('{{%newcolumns}}', [
1111
'id' => $this->primaryKey(),
1212
'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'',
13+
0 => 'new_column_x varchar(10) NOT NULL DEFAULT \'ONE\'',
1314
]);
1415
}
1516

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ components:
5959
default:
6060
ONE
6161
nullable: false
62+
new_column_x:
63+
type: string
64+
enum:
65+
- ONE
66+
- TWO
67+
- THREE
68+
default:
69+
ONE
70+
x-db-type: varchar(10)
71+
nullable: false
6272

6373
Editcolumn:
6474
type: object

0 commit comments

Comments
 (0)