From 08e30abb006d2279963c91ffd88ee11de27b5fbd Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 15:32:18 +0530 Subject: [PATCH 01/62] Test --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 597fbc33..f09f58cf 100644 --- a/README.md +++ b/README.md @@ -268,3 +268,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From ec097d7de1b733fd7ec9098d74da8fb33bf5fb26 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 15:35:09 +0530 Subject: [PATCH 02/62] Test 2 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 94b53f46..eb457b6b 100644 --- a/README.md +++ b/README.md @@ -406,3 +406,5 @@ https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + +- From 03ca98bc8760780ba28f6cdb686df53873299cb0 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 15:37:59 +0530 Subject: [PATCH 03/62] test 3 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eb457b6b..3653aadd 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Development of this library is sponsored by [cebe.:cloud: "Your Professional Dep - +- From 984c7b3621265889cf6c80df87d4b433a160b947 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 16:19:57 +0530 Subject: [PATCH 04/62] Test --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3653aadd..8b0e60ec 100644 --- a/README.md +++ b/README.md @@ -409,3 +409,4 @@ Development of this library is sponsored by [cebe.:cloud: "Your Professional Dep - - +- From 96257f6dd31d9d8411039bd07f1d975ff075c8c8 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 16:38:00 +0530 Subject: [PATCH 05/62] Cleanup --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 8b0e60ec..1626e149 100644 --- a/README.md +++ b/README.md @@ -405,8 +405,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - - -- -- -- From d3ecd74ff8297d69addea8224863e36e8b73f07a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 16:40:03 +0530 Subject: [PATCH 06/62] test --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1626e149..78cb4ed8 100644 --- a/README.md +++ b/README.md @@ -405,3 +405,5 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + +4 From c809394d45ea2ccfe161863feb104ab9f1cf2358 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 16:42:45 +0530 Subject: [PATCH 07/62] Fix issue in setting default expression --- README.md | 2 -- src/lib/ColumnToCode.php | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 78cb4ed8..1626e149 100644 --- a/README.md +++ b/README.md @@ -405,5 +405,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - -4 diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 7a0188e2..3a79d636 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -421,15 +421,17 @@ private function resolveDefaultValue():void break; default: $isExpression = StringHelper::startsWith($value, 'CURRENT') + || StringHelper::startsWith($value, 'current') || StringHelper::startsWith($value, 'LOCAL') || substr($value, -1, 1) === ')'; if ($isExpression) { $this->fluentParts['default'] = 'defaultExpression("' . self::escapeQuotes((string)$value) . '")'; + $this->rawParts['default'] = $value; } else { $this->fluentParts['default'] = $expectInteger ? 'defaultValue(' . $value . ')' : 'defaultValue("' . self::escapeQuotes((string)$value) . '")'; + $this->rawParts['default'] = $expectInteger ? $value : self::wrapQuotes($value); } - $this->rawParts['default'] = $expectInteger ? $value : self::wrapQuotes($value); if (ApiGenerator::isMysql() && $this->isEnum()) { $this->rawParts['default'] = self::escapeQuotes($this->rawParts['default']); } From 3d4d71c6cee2dddba8ace4496b38ef07f0fe8475 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 21 Dec 2022 17:44:30 +0530 Subject: [PATCH 08/62] Fix issue: decimal considered as string instead of double/float --- README.md | 1 + src/lib/openapi/PropertySchema.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1626e149..94b53f46 100644 --- a/README.md +++ b/README.md @@ -405,3 +405,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index 4137f99a..9e6b638f 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -530,7 +530,7 @@ public static function findMoreDetailOf(string $xDbType): array } /** - * This method is copied from protected method `getColumnPhpType()` of \yii\db\Schema class + * This method is copied + enhanced from protected method `getColumnPhpType()` of \yii\db\Schema class * Extracts the PHP type from abstract DB type. * @param \yii\db\ColumnSchema $column the column schema information * @return string PHP type name @@ -546,6 +546,7 @@ public static function getColumnPhpType(ColumnSchema $column): string YiiDbSchema::TYPE_BOOLEAN => 'boolean', YiiDbSchema::TYPE_FLOAT => 'double', YiiDbSchema::TYPE_DOUBLE => 'double', + YiiDbSchema::TYPE_DECIMAL => 'double', # (enhanced) YiiDbSchema::TYPE_BINARY => 'resource', YiiDbSchema::TYPE_JSON => 'array', ]; From a9c6ec2cd1658261e693eaf0d22cb3c55cd4c80e Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 22 Dec 2022 16:32:39 +0530 Subject: [PATCH 09/62] Fix enum issue for MySQL, MariaDB and partially for PgSQL #111 --- src/lib/ColumnToCode.php | 8 ++++--- tests/specs/enum/enum.php | 13 ++++++++++++ tests/specs/enum/enum.yaml | 33 +++++++++++++++++++++++++++++ tests/unit/EnumTest.php | 43 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 tests/specs/enum/enum.php create mode 100644 tests/specs/enum/enum.yaml create mode 100644 tests/unit/EnumTest.php diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 3a79d636..377333e7 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -270,7 +270,9 @@ public static function enumToString(array $enum):string public static function mysqlEnumToString(array $enum):string { - return implode(', ', array_map('self::wrapQuotes', $enum)); + return implode(', ', array_map(function($aEnumValue) { + return self::wrapQuotes($aEnumValue, '"'); + }, $enum)); } private function defaultValueJson(array $value):string @@ -329,7 +331,7 @@ private function resolve():void $this->rawParts['type'] = $this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize); } - + $this->isBuiltinType = $this->raw ? false : $this->getIsBuiltinType($type, $dbType); $this->resolveDefaultValue(); @@ -346,7 +348,7 @@ private function getIsBuiltinType($type, $dbType) return false; } - if ($this->isEnum() && ApiGenerator::isMariaDb()) { + if ($this->isEnum()) { return false; } if ($this->fromDb === true) { diff --git a/tests/specs/enum/enum.php b/tests/specs/enum/enum.php new file mode 100644 index 00000000..30e7ae15 --- /dev/null +++ b/tests/specs/enum/enum.php @@ -0,0 +1,13 @@ + '@specs/enum/enum.yaml', + 'generateUrls' => false, + 'generateModels' => false, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => false, +]; diff --git a/tests/specs/enum/enum.yaml b/tests/specs/enum/enum.yaml new file mode 100644 index 00000000..6986ad5c --- /dev/null +++ b/tests/specs/enum/enum.yaml @@ -0,0 +1,33 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Enum test +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Pristine: + type: object + description: Enum for migration code generation + required: + - id + properties: + id: + type: integer + device: + type: string + maxLength: 8 + enum: + - MOBILE + - TV + - COMPUTER + default: + TV + nullable: false diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php new file mode 100644 index 00000000..494a6de8 --- /dev/null +++ b/tests/unit/EnumTest.php @@ -0,0 +1,43 @@ +deleteTables(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'mysql'); + + $this->changeDbToMariadb(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'maria'); + + $this->changeDbToPgsql(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'pgsql'); + + + // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + // 'recursive' => true, + // 'except' => ['migrations_maria_db', 'migrations_pgsql_db'] + // ]); + // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/fresh/mysql/app"), [ + // 'recursive' => true, + // ]); + // $this->compareFiles($actualFiles, $expectedFiles); + } +} From 89f98c202e2d2baaad0c20529dbda385e62c02fc Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 23 Dec 2022 17:34:46 +0530 Subject: [PATCH 10/62] WIP --- src/lib/items/MigrationModel.php | 43 +++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/lib/items/MigrationModel.php b/src/lib/items/MigrationModel.php index a8ba6611..b1a74d01 100644 --- a/src/lib/items/MigrationModel.php +++ b/src/lib/items/MigrationModel.php @@ -8,7 +8,8 @@ namespace cebe\yii2openapi\lib\items; use yii\base\BaseObject; -use yii\helpers\Inflector; +use yii\helpers\{Inflector, VarDumper}; +use cebe\yii2openapi\generator\ApiGenerator; use function array_push; use function array_unshift; use function implode; @@ -72,14 +73,50 @@ public function __construct(DbModel $model, bool $isFresh = true, ManyToManyRela } } + /** + * @param ?array $codes + * @param string $direction ENUM allowed value can be one of "UP", "DOWN" + */ + public static function moveEnumStatementForPgsql(?array $codes, string $direction) + { + // for up migration move enum statemenet to botton + // for down migration move enum statemenet to top + + if (!ApiGenerator::isPostgres() || empty($codes)) { + return $codes; + } + + // $enumKey = null; + // foreach ($codes as $key => $code) { + // if (strpos($code, ' TYPE enum_') !== false) { + // $enumKey = $key; + // break; + // } + // } + + // if ($enumKey !== null) { + // if ($direction === 'UP') { + // array_push($codes, $codes[$enumKey]); + // unset($codes[$enumKey]); + // } elseif ($direction === 'DOWN') { + // $code = $codes[$enumKey]; + // unset($codes[$enumKey]); + // array_unshift($codes, $code); + // } else { + // throw new \Exception('Unknown direction found.'); + // } + // } + return $codes; + } + public function getUpCodeString():string { - return !empty($this->upCodes) ? implode(PHP_EOL, $this->upCodes) : ''; + return !empty($this->upCodes) ? implode(PHP_EOL, static::moveEnumStatementForPgsql($this->upCodes, 'UP')) : ''; } public function getDownCodeString():string { - return !empty($this->downCodes) ? implode(PHP_EOL, $this->downCodes) : ''; + return !empty($this->downCodes) ? implode(PHP_EOL, static::moveEnumStatementForPgsql($this->downCodes, 'DOWN')) : ''; } public function notEmpty():bool From b047f7dcebacd07f399dd7bd45b9f659829a0bc3 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 23 Dec 2022 17:35:06 +0530 Subject: [PATCH 11/62] undo WIP --- src/lib/items/MigrationModel.php | 43 +++----------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) diff --git a/src/lib/items/MigrationModel.php b/src/lib/items/MigrationModel.php index b1a74d01..a8ba6611 100644 --- a/src/lib/items/MigrationModel.php +++ b/src/lib/items/MigrationModel.php @@ -8,8 +8,7 @@ namespace cebe\yii2openapi\lib\items; use yii\base\BaseObject; -use yii\helpers\{Inflector, VarDumper}; -use cebe\yii2openapi\generator\ApiGenerator; +use yii\helpers\Inflector; use function array_push; use function array_unshift; use function implode; @@ -73,50 +72,14 @@ public function __construct(DbModel $model, bool $isFresh = true, ManyToManyRela } } - /** - * @param ?array $codes - * @param string $direction ENUM allowed value can be one of "UP", "DOWN" - */ - public static function moveEnumStatementForPgsql(?array $codes, string $direction) - { - // for up migration move enum statemenet to botton - // for down migration move enum statemenet to top - - if (!ApiGenerator::isPostgres() || empty($codes)) { - return $codes; - } - - // $enumKey = null; - // foreach ($codes as $key => $code) { - // if (strpos($code, ' TYPE enum_') !== false) { - // $enumKey = $key; - // break; - // } - // } - - // if ($enumKey !== null) { - // if ($direction === 'UP') { - // array_push($codes, $codes[$enumKey]); - // unset($codes[$enumKey]); - // } elseif ($direction === 'DOWN') { - // $code = $codes[$enumKey]; - // unset($codes[$enumKey]); - // array_unshift($codes, $code); - // } else { - // throw new \Exception('Unknown direction found.'); - // } - // } - return $codes; - } - public function getUpCodeString():string { - return !empty($this->upCodes) ? implode(PHP_EOL, static::moveEnumStatementForPgsql($this->upCodes, 'UP')) : ''; + return !empty($this->upCodes) ? implode(PHP_EOL, $this->upCodes) : ''; } public function getDownCodeString():string { - return !empty($this->downCodes) ? implode(PHP_EOL, static::moveEnumStatementForPgsql($this->downCodes, 'DOWN')) : ''; + return !empty($this->downCodes) ? implode(PHP_EOL, $this->downCodes) : ''; } public function notEmpty():bool From 6cea7f292db7213efcaadcc060b1025a6e6f389e Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 23 Dec 2022 17:36:04 +0530 Subject: [PATCH 12/62] Fix issue: CREATE/DROP enum in PGSQL up/down migration not in correct order --- .../migrations/PostgresMigrationBuilder.php | 24 ++++++++------- tests/specs/enum/enum.yaml | 19 ++++++++++++ tests/unit/EnumTest.php | 29 ++++++++++++++++++- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index edd9ec4d..e1bf88cb 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -24,7 +24,7 @@ protected function buildColumnsCreation(array $columns):void $tableName = $this->model->getTableAlias(); if ($column->dbType === 'enum') { $this->migration->addUpCode($this->recordBuilder->createEnum($column->name, $column->enumValues)) - ->addDownCode($this->recordBuilder->dropEnum($column->name)); + ->addDownCode($this->recordBuilder->dropEnum($column->name), true); } $this->migration->addUpCode($this->recordBuilder->addColumn($tableName, $column)) ->addDownCode($this->recordBuilder->dropColumn($tableName, $column->name)); @@ -42,8 +42,8 @@ protected function buildColumnsDrop(array $columns):void $this->migration->addDownCode($this->recordBuilder->addDbColumn($tableName, $column)) ->addUpCode($this->recordBuilder->dropColumn($tableName, $column->name)); if ($column->dbType === 'enum') { - $this->migration->addDownCode($this->recordBuilder->createEnum($column->name, $column->enumValues)) - ->addUpCode($this->recordBuilder->dropEnum($column->name), true); + $this->migration->addDownCode($this->recordBuilder->createEnum($column->name, $column->enumValues), true) + ->addUpCode($this->recordBuilder->dropEnum($column->name)); } } } @@ -62,12 +62,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir // This action require several steps and can't be applied during single transaction return; } - if ($isChangeToEnum) { - $this->migration->addUpCode($this->recordBuilder->createEnum($desired->name, $desired->enumValues), true); - } - if ($isChangeFromEnum) { - $this->migration->addUpCode($this->recordBuilder->dropEnum($current->name)); - } + if (!empty(array_intersect(['type', 'size'], $changed))) { $addUsing = $this->isNeedUsingExpression($desired->type, $current->type); $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired)); @@ -93,9 +88,16 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir $this->migration->addUpCode($upCode)->addDownCode($downCode, true); } } + if ($isChangeToEnum) { + $this->migration->addUpCode($this->recordBuilder->createEnum($desired->name, $desired->enumValues), true); + } + if ($isChangeFromEnum) { + $this->migration->addUpCode($this->recordBuilder->dropEnum($current->name)); + } + if ($isChangeFromEnum) { $this->migration - ->addDownCode($this->recordBuilder->createEnum($current->name, $current->enumValues), true); + ->addDownCode($this->recordBuilder->createEnum($current->name, $current->enumValues)); } if ($isChangeToEnum) { $this->migration->addDownCode($this->recordBuilder->dropEnum($current->name), true); @@ -132,7 +134,7 @@ protected function createEnumMigrations():void foreach ($enums as $attr) { $this->migration ->addUpCode($this->recordBuilder->createEnum($attr->columnName, $attr->enumValues), true) - ->addDownCode($this->recordBuilder->dropEnum($attr->columnName)); + ->addDownCode($this->recordBuilder->dropEnum($attr->columnName), true); } } diff --git a/tests/specs/enum/enum.yaml b/tests/specs/enum/enum.yaml index 6986ad5c..8c9c8592 100644 --- a/tests/specs/enum/enum.yaml +++ b/tests/specs/enum/enum.yaml @@ -31,3 +31,22 @@ components: default: TV nullable: false + + Editcolumn: + type: object + description: Table with edit enum columns for migration code generation + required: + - id + properties: + id: + type: integer + device: + type: string + # maxLength: 8 + # enum: + # - MOBILE + # - TV + # - COMPUTER + # default: + # TV + # nullable: false diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 494a6de8..1409f559 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -15,7 +15,7 @@ class EnumTest extends DbTestCase { - public function testXDbTypeFresh() + public function testEnumFresh() { // default DB is Mysql ------------------------------------------------ // $this->deleteTables(); @@ -40,4 +40,31 @@ public function testXDbTypeFresh() // ]); // $this->compareFiles($actualFiles, $expectedFiles); } + + public function testEnumToString() + { + $this->changeDbToPgsql(); + $this->deleteTables(); + $this->createTableForEditEnumToString(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'pgsql'); + } + + private function deleteTables() + { + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_device CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute(); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%newcolumns}}')->execute(); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%editcolumns}}')->execute(); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%alldbdatatypes}}')->execute(); + } + + private function createTableForEditEnumToString() + { + Yii::$app->db->createCommand('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')')->execute(); + Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ + 'id' => 'pk', + 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + ])->execute(); + } } From 6a9a6807993535140628b950670f02ba58fd6a18 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 23 Dec 2022 17:45:34 +0530 Subject: [PATCH 13/62] Fix style --- src/lib/ColumnToCode.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 377333e7..c963aba2 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -270,7 +270,7 @@ public static function enumToString(array $enum):string public static function mysqlEnumToString(array $enum):string { - return implode(', ', array_map(function($aEnumValue) { + return implode(', ', array_map(function ($aEnumValue) { return self::wrapQuotes($aEnumValue, '"'); }, $enum)); } From c60403fa4b1d50a0babb512a537623fc2c95363d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 23 Dec 2022 18:48:55 +0530 Subject: [PATCH 14/62] Fix quote issue in migration down code for MySQL and Maria + add more tests --- src/lib/ColumnToCode.php | 6 +++--- tests/unit/EnumTest.php | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index c963aba2..bc896385 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -148,8 +148,8 @@ public function getCode(bool $quoted = false):string } $code = $this->rawParts['type'] . ' ' . $this->rawParts['nullable'] . $default; - if (ApiGenerator::isMysql() && $this->isEnum()) { - return $quoted ? '"' . str_replace("\'", "'", $code) . '"' : $code; + if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb()) && $this->isEnum()) { + return $quoted ? "'" . $code . "'" : $code; } if (ApiGenerator::isPostgres() && $this->alterByXDbType) { return $quoted ? "'" . $this->rawParts['type'] . "'" : $this->rawParts['type']; @@ -434,7 +434,7 @@ private function resolveDefaultValue():void ? 'defaultValue(' . $value . ')' : 'defaultValue("' . self::escapeQuotes((string)$value) . '")'; $this->rawParts['default'] = $expectInteger ? $value : self::wrapQuotes($value); } - if (ApiGenerator::isMysql() && $this->isEnum()) { + if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb()) && $this->isEnum()) { $this->rawParts['default'] = self::escapeQuotes($this->rawParts['default']); } } diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 1409f559..6e8e5ca5 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -43,6 +43,19 @@ public function testEnumFresh() public function testEnumToString() { + $this->deleteTables(); + $this->createTableForEditEnumToString(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'mysql'); + + + $this->changeDbToMariadb(); + $this->deleteTables(); + $this->createTableForEditEnumToString(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'maria'); + + $this->changeDbToPgsql(); $this->deleteTables(); $this->createTableForEditEnumToString(); @@ -50,9 +63,23 @@ public function testEnumToString() $this->runGenerator($testFile, 'pgsql'); } + // public function testStringToEnum() + // { // TODO + // } + + // public function testChangeEnumValues() + // { + // // TODO + // // add a value to list + // // fix a typo in a enum value present in existing list + // // remove a value from list + // } + private function deleteTables() { - Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_device CASCADE')->execute(); + if (ApiGenerator::isPostgres()) { + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_device CASCADE')->execute(); + } Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute(); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%newcolumns}}')->execute(); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%editcolumns}}')->execute(); @@ -61,10 +88,17 @@ private function deleteTables() private function createTableForEditEnumToString() { - Yii::$app->db->createCommand('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')')->execute(); + if (ApiGenerator::isPostgres()) { + Yii::$app->db->createCommand('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')')->execute(); + Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ + 'id' => 'pk', + 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + ])->execute(); + return; + } Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ 'id' => 'pk', - 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', ])->execute(); } } From fcc53a20e1d8ef53ffd50efad922610b28a5d20b Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 23 Dec 2022 20:57:09 +0530 Subject: [PATCH 15/62] Fix multiple issue + add more tests for eunm in Pgsql + other DB --- src/lib/ColumnToCode.php | 2 +- src/lib/migrations/BaseMigrationBuilder.php | 20 +++++++++++++++++++ src/lib/migrations/MysqlMigrationBuilder.php | 12 +++++++++++ .../migrations/PostgresMigrationBuilder.php | 12 ++++++++++- tests/specs/enum/enum.yaml | 16 +++++++-------- tests/unit/EnumTest.php | 8 +++++++- 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index bc896385..2eb47ba1 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -160,7 +160,7 @@ public function getCode(bool $quoted = false):string public function getAlterExpression(bool $addUsingExpression = false):string { if ($this->isEnum() && ApiGenerator::isPostgres()) { - return "'" . sprintf('enum_%1$s USING %1$s::enum_%1$s', $this->column->name) . "'"; + return "'" . sprintf('enum_%1$s USING "%1$s"::enum_%1$s', $this->column->name) . "'"; } if ($this->column->dbType === 'tsvector') { return "'" . $this->rawParts['type'] . "'"; diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 82becfb9..c930c56c 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -7,6 +7,7 @@ namespace cebe\yii2openapi\lib\migrations; +use cebe\yii2openapi\generator\ApiGenerator; use cebe\yii2openapi\lib\ColumnToCode; use cebe\yii2openapi\lib\items\DbModel; use cebe\yii2openapi\lib\items\ManyToManyRelation; @@ -203,6 +204,11 @@ function (string $unknownColumn) { // do not adjust existing primary keys continue; } + // TODO this section is not concretely correct + // type can be one of: char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime, + // timestamp, time, date, binary, and money. + // `enum` is not a part of it + // also real DB type required enum values in MySQL and new `TYPE` in PgSQL if (!empty($current->enumValues)) { $current->type = 'enum'; $current->dbType = 'enum'; @@ -211,6 +217,7 @@ function (string $unknownColumn) { $desired->type = 'enum'; $desired->dbType = 'enum'; } + // section end $changedAttributes = $this->compareColumns($current, $desired); if (empty($changedAttributes)) { continue; @@ -432,12 +439,25 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): $column = [$columnSchema->name => $this->newColStr($columnSchema)]; } + // create enum if relevant + if (ApiGenerator::isPostgres() && !empty($columnSchema->enumValues)) { + $allEnumValues = $columnSchema->enumValues; + $allEnumValues = array_map(function ($aValue) {return "'$aValue'";}, $allEnumValues); + Yii::$app->db->createCommand( + 'CREATE TYPE enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')' + )->execute(); + } + Yii::$app->db->createCommand()->createTable($tableName, $column)->execute(); $table = Yii::$app->db->getTableSchema($tableName); Yii::$app->db->createCommand()->dropTable($tableName)->execute(); + if (ApiGenerator::isPostgres() && !empty($columnSchema->enumValues)) {// drop enum + Yii::$app->db->createCommand('DROP TYPE enum_'.$columnSchema->name)->execute(); + } + return $table->columns[$columnSchema->name]; } diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 9cb17c79..1de0ea44 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -119,6 +119,12 @@ public function modifyCurrent(ColumnSchema $current): void if ($current->phpType === 'integer' && $current->defaultValue !== null) { $current->defaultValue = (int)$current->defaultValue; } + + // TODO this is not concretely correct + if (!empty($current->enumValues)) { + $current->type = 'enum'; + $current->dbType = 'enum'; + } } public function modifyDesired(ColumnSchema $desired): void @@ -132,6 +138,12 @@ public function modifyDesired(ColumnSchema $desired): void $desired->precision = $decimalAttributes['precision']; $desired->scale = $decimalAttributes['scale']; } + + // TODO this is not concretely correct + if (!empty($desired->enumValues)) { + $desired->type = 'enum'; + $desired->dbType = 'enum'; + } } public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index e1bf88cb..1076d22f 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -63,7 +63,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir return; } - if (!empty(array_intersect(['type', 'size'], $changed))) { + if (!empty(array_intersect(['type', 'size'/*, 'dbType'*/], $changed))) { $addUsing = $this->isNeedUsingExpression($desired->type, $current->type); $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired)); $this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing)); @@ -207,6 +207,11 @@ public function modifyCurrent(ColumnSchema $current): void if ($current->phpType === 'integer' && $current->defaultValue !== null) { $current->defaultValue = (int)$current->defaultValue; } + // TODO this is not concretely correct + if (!empty($current->enumValues)) { + $current->type = 'enum'; + $current->dbType = 'enum'; + } } public function modifyDesired(ColumnSchema $desired): void @@ -219,6 +224,11 @@ public function modifyDesired(ColumnSchema $desired): void $desired->precision = $decimalAttributes['precision']; $desired->scale = $decimalAttributes['scale']; } + // TODO this is not concretely correct + if (!empty($desired->enumValues)) { + $desired->type = 'enum'; + $desired->dbType = 'enum'; + } } public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void diff --git a/tests/specs/enum/enum.yaml b/tests/specs/enum/enum.yaml index 8c9c8592..df860f3e 100644 --- a/tests/specs/enum/enum.yaml +++ b/tests/specs/enum/enum.yaml @@ -42,11 +42,11 @@ components: type: integer device: type: string - # maxLength: 8 - # enum: - # - MOBILE - # - TV - # - COMPUTER - # default: - # TV - # nullable: false + connection: + type: string + enum: + - WIRED + - WIRELESS + default: + WIRED + nullable: false diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 6e8e5ca5..440c7de4 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -64,7 +64,11 @@ public function testEnumToString() } // public function testStringToEnum() - // { // TODO + // { + // $this->deleteTables(); + // $this->createTableForEditEnumToString(); + // $testFile = Yii::getAlias("@specs/enum/enum.php"); + // $this->runGenerator($testFile, 'mysql'); // } // public function testChangeEnumValues() @@ -93,12 +97,14 @@ private function createTableForEditEnumToString() Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ 'id' => 'pk', 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + 'connection' => 'string' ])->execute(); return; } Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ 'id' => 'pk', 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', + 'connection' => 'string' ])->execute(); } } From 130c69a0608cf1a085bf5c60bf1d8107d161e280 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 24 Dec 2022 13:14:40 +0530 Subject: [PATCH 16/62] Fix bug in column change detection in Pgsql --- src/lib/migrations/MysqlMigrationBuilder.php | 4 ++-- src/lib/migrations/PostgresMigrationBuilder.php | 9 ++++++--- tests/specs/postgres_custom.yaml | 8 ++++---- .../m200000_000001_change_table_editcolumns.php | 2 ++ tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml | 2 +- tests/unit/XDbTypeTest.php | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 1de0ea44..c74f4ce4 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -120,7 +120,7 @@ public function modifyCurrent(ColumnSchema $current): void $current->defaultValue = (int)$current->defaultValue; } - // TODO this is not concretely correct + // TODO this is not concretely correct, reason is in BaseMigrationBuilder if (!empty($current->enumValues)) { $current->type = 'enum'; $current->dbType = 'enum'; @@ -139,7 +139,7 @@ public function modifyDesired(ColumnSchema $desired): void $desired->scale = $decimalAttributes['scale']; } - // TODO this is not concretely correct + // TODO this is not concretely correct, reason is in BaseMigrationBuilder if (!empty($desired->enumValues)) { $desired->type = 'enum'; $desired->dbType = 'enum'; diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index 1076d22f..b736a958 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -63,7 +63,10 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir return; } - if (!empty(array_intersect(['type', 'size'/*, 'dbType'*/], $changed))) { + if (!empty(array_intersect(['type', 'size' + , 'dbType', 'phpType' + , 'precision', 'scale', 'unsigned' + ], $changed))) { $addUsing = $this->isNeedUsingExpression($desired->type, $current->type); $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired)); $this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing)); @@ -207,7 +210,7 @@ public function modifyCurrent(ColumnSchema $current): void if ($current->phpType === 'integer' && $current->defaultValue !== null) { $current->defaultValue = (int)$current->defaultValue; } - // TODO this is not concretely correct + // TODO this is not concretely correct, reason is in BaseMigrationBuilder if (!empty($current->enumValues)) { $current->type = 'enum'; $current->dbType = 'enum'; @@ -224,7 +227,7 @@ public function modifyDesired(ColumnSchema $desired): void $desired->precision = $decimalAttributes['precision']; $desired->scale = $decimalAttributes['scale']; } - // TODO this is not concretely correct + // TODO this is not concretely correct, reason is in BaseMigrationBuilder if (!empty($desired->enumValues)) { $desired->type = 'enum'; $desired->dbType = 'enum'; diff --git a/tests/specs/postgres_custom.yaml b/tests/specs/postgres_custom.yaml index fce6664c..68c67ae1 100644 --- a/tests/specs/postgres_custom.yaml +++ b/tests/specs/postgres_custom.yaml @@ -48,21 +48,21 @@ components: default: 0 json1: type: array - x-db-type: json + x-db-type: jsonb default: [] json2: type: array - x-db-type: json + x-db-type: jsonb default: '{}' json3: type: array - x-db-type: json + x-db-type: jsonb default: - foo: foobar - xxx: yyy json4: type: array - x-db-type: json + x-db-type: jsonb default: '{"foo": "bar", "bar": "baz"}' # status: # type: string 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 d412271c..79e81cd7 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,6 +11,7 @@ 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->alterColumn('{{%editcolumns}}', 'dec_col', "SET DEFAULT 3.14"); $this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN json_col SET DATA TYPE text')->execute(); $this->alterColumn('{{%editcolumns}}', 'json_col', "SET NOT NULL"); @@ -32,6 +33,7 @@ public function safeDown() $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->dropColumn('{{%editcolumns}}', 'text_col_array'); $this->dropColumn('{{%editcolumns}}', 'json_col_def_n_2'); $this->dropColumn('{{%editcolumns}}', 'json_col_def_n'); diff --git a/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml b/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml index b472ecc3..692a9831 100644 --- a/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml +++ b/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml @@ -505,7 +505,7 @@ components: default: fox jumps over dog json_col_2: type: string - x-db-type: json + x-db-type: jsonb nullable: false default: [] numeric_col: diff --git a/tests/unit/XDbTypeTest.php b/tests/unit/XDbTypeTest.php index 223a782b..28385841 100644 --- a/tests/unit/XDbTypeTest.php +++ b/tests/unit/XDbTypeTest.php @@ -197,7 +197,7 @@ private function createTableForEditColumns() 'string_col' => 'string not null', 'dec_col' => 'decimal(12, 4)', 'str_col_def' => 'string default \'hi there\'', - 'json_col' => 'json', + 'json_col' => 'json', // json is jsonb in Pgsql via Yii Pgsql Schema 'json_col_2' => 'json', 'numeric_col' => 'integer', ])->execute(); From 1b0bc2ef25c6ba15ee1ffe4854cd078335c83294 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 24 Dec 2022 14:22:52 +0530 Subject: [PATCH 17/62] Fix failing tests --- .../m200000_000001_create_table_editcolumns.php | 2 +- .../m200000_000001_create_table_editcolumns.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php index 93111403..cf34df76 100644 --- a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php +++ b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php @@ -16,7 +16,7 @@ public function safeUp() 2 => 'dec_col decimal(12,2) NULL DEFAULT 3.14', 3 => 'str_col_def varchar NOT NULL', 4 => 'json_col text NOT NULL DEFAULT \'fox jumps over dog\'', - 5 => 'json_col_2 json NOT NULL DEFAULT \'[]\'', + 5 => 'json_col_2 jsonb NOT NULL DEFAULT \'[]\'', 6 => 'numeric_col double precision NULL DEFAULT NULL', 7 => 'json_col_def_n json NOT NULL DEFAULT \'[]\'', 8 => 'json_col_def_n_2 json NOT NULL DEFAULT \'[]\'', diff --git a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php index 93111403..cf34df76 100644 --- a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php +++ b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php @@ -16,7 +16,7 @@ public function safeUp() 2 => 'dec_col decimal(12,2) NULL DEFAULT 3.14', 3 => 'str_col_def varchar NOT NULL', 4 => 'json_col text NOT NULL DEFAULT \'fox jumps over dog\'', - 5 => 'json_col_2 json NOT NULL DEFAULT \'[]\'', + 5 => 'json_col_2 jsonb NOT NULL DEFAULT \'[]\'', 6 => 'numeric_col double precision NULL DEFAULT NULL', 7 => 'json_col_def_n json NOT NULL DEFAULT \'[]\'', 8 => 'json_col_def_n_2 json NOT NULL DEFAULT \'[]\'', From a037827df3a544010a791dc34dc63f8792363255 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 24 Dec 2022 18:40:48 +0530 Subject: [PATCH 18/62] Fix enum related issues + add more tests for it for all 3 DBs --- src/lib/migrations/BaseMigrationBuilder.php | 40 +++++++++++-------- src/lib/migrations/MysqlMigrationBuilder.php | 16 +------- .../migrations/PostgresMigrationBuilder.php | 22 +++------- tests/specs/enum/enum.yaml | 28 +++++++++++++ tests/unit/EnumTest.php | 40 +++++++++++++++++++ 5 files changed, 100 insertions(+), 46 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index c930c56c..d091b63a 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -204,20 +204,6 @@ function (string $unknownColumn) { // do not adjust existing primary keys continue; } - // TODO this section is not concretely correct - // type can be one of: char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime, - // timestamp, time, date, binary, and money. - // `enum` is not a part of it - // also real DB type required enum values in MySQL and new `TYPE` in PgSQL - if (!empty($current->enumValues)) { - $current->type = 'enum'; - $current->dbType = 'enum'; - } - if (!empty($desired->enumValues)) { - $desired->type = 'enum'; - $desired->dbType = 'enum'; - } - // section end $changedAttributes = $this->compareColumns($current, $desired); if (empty($changedAttributes)) { continue; @@ -440,9 +426,11 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): } // create enum if relevant - if (ApiGenerator::isPostgres() && !empty($columnSchema->enumValues)) { + if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { $allEnumValues = $columnSchema->enumValues; - $allEnumValues = array_map(function ($aValue) {return "'$aValue'";}, $allEnumValues); + $allEnumValues = array_map(function ($aValue) { + return "'$aValue'"; + }, $allEnumValues); Yii::$app->db->createCommand( 'CREATE TYPE enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')' )->execute(); @@ -466,4 +454,24 @@ public function newColStr(\cebe\yii2openapi\db\ColumnSchema $columnSchema): stri $ctc = new ColumnToCode(\Yii::$app->db->schema, $columnSchema, false, false, true); return ColumnToCode::undoEscapeQuotes($ctc->getCode()); } + + public static function isEnum(\yii\db\ColumnSchema $columnSchema): bool + { + if (!empty($columnSchema->enumValues) && is_array($columnSchema->enumValues)) { + return true; + } + return false; + } + + public static function isEnumValuesChanged( + \yii\db\ColumnSchema $current, + \yii\db\ColumnSchema $desired + ): bool + { + if (static::isEnum($current) && static::isEnum($desired) && + $current->enumValues !== $desired->enumValues) { + return true; + } + return false; + } } diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index c74f4ce4..c9f13c32 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -29,8 +29,8 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir foreach ($changed as $attr) { $newColumn->$attr = $desired->$attr; } - if (!empty($newColumn->enumValues)) { - $newColumn->dbType = 'enum'; + if (static::isEnum($newColumn)) { + $newColumn->dbType = 'enum'; // TODO this is concretely not correct } $this->migration->addUpCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $newColumn)) ->addDownCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $current)); @@ -119,12 +119,6 @@ public function modifyCurrent(ColumnSchema $current): void if ($current->phpType === 'integer' && $current->defaultValue !== null) { $current->defaultValue = (int)$current->defaultValue; } - - // TODO this is not concretely correct, reason is in BaseMigrationBuilder - if (!empty($current->enumValues)) { - $current->type = 'enum'; - $current->dbType = 'enum'; - } } public function modifyDesired(ColumnSchema $desired): void @@ -138,12 +132,6 @@ public function modifyDesired(ColumnSchema $desired): void $desired->precision = $decimalAttributes['precision']; $desired->scale = $decimalAttributes['scale']; } - - // TODO this is not concretely correct, reason is in BaseMigrationBuilder - if (!empty($desired->enumValues)) { - $desired->type = 'enum'; - $desired->dbType = 'enum'; - } } public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index b736a958..add24713 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -22,7 +22,7 @@ protected function buildColumnsCreation(array $columns):void { foreach ($columns as $column) { $tableName = $this->model->getTableAlias(); - if ($column->dbType === 'enum') { + if (static::isEnum($column)) { $this->migration->addUpCode($this->recordBuilder->createEnum($column->name, $column->enumValues)) ->addDownCode($this->recordBuilder->dropEnum($column->name), true); } @@ -41,8 +41,8 @@ protected function buildColumnsDrop(array $columns):void $tableName = $this->model->getTableAlias(); $this->migration->addDownCode($this->recordBuilder->addDbColumn($tableName, $column)) ->addUpCode($this->recordBuilder->dropColumn($tableName, $column->name)); - if ($column->dbType === 'enum') { - $this->migration->addDownCode($this->recordBuilder->createEnum($column->name, $column->enumValues), true) + if (static::isEnum($column)) { + $this->migration->addDownCode($this->recordBuilder->createEnum($column->name, $column->enumValues)) ->addUpCode($this->recordBuilder->dropEnum($column->name)); } } @@ -54,9 +54,9 @@ protected function buildColumnsDrop(array $columns):void protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desired, array $changed):void { $tableName = $this->model->getTableAlias(); - $isChangeToEnum = $current->type !== $desired->type && !empty($desired->enumValues); - $isChangeFromEnum = $current->type !== $desired->type && !empty($current->enumValues); - $isChangedEnum = $current->type === $desired->type && !empty($current->enumValues); + $isChangeToEnum = !static::isEnum($current) && static::isEnum($desired); + $isChangeFromEnum = static::isEnum($current) && !static::isEnum($desired); + $isChangedEnum = static::isEnumValuesChanged($current, $desired); if ($isChangedEnum) { // Generation for change enum values not supported. Do it manually // This action require several steps and can't be applied during single transaction @@ -210,11 +210,6 @@ public function modifyCurrent(ColumnSchema $current): void if ($current->phpType === 'integer' && $current->defaultValue !== null) { $current->defaultValue = (int)$current->defaultValue; } - // TODO this is not concretely correct, reason is in BaseMigrationBuilder - if (!empty($current->enumValues)) { - $current->type = 'enum'; - $current->dbType = 'enum'; - } } public function modifyDesired(ColumnSchema $desired): void @@ -227,11 +222,6 @@ public function modifyDesired(ColumnSchema $desired): void $desired->precision = $decimalAttributes['precision']; $desired->scale = $decimalAttributes['scale']; } - // TODO this is not concretely correct, reason is in BaseMigrationBuilder - if (!empty($desired->enumValues)) { - $desired->type = 'enum'; - $desired->dbType = 'enum'; - } } public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void diff --git a/tests/specs/enum/enum.yaml b/tests/specs/enum/enum.yaml index df860f3e..72a0e894 100644 --- a/tests/specs/enum/enum.yaml +++ b/tests/specs/enum/enum.yaml @@ -32,6 +32,34 @@ components: TV nullable: false + Newcolumn: + type: object + description: New Fresh table with new enum column for migration code generation + required: + - id + properties: + id: + type: integer + # device: + # type: string + # maxLength: 8 + # enum: + # - MOBILE + # - TV + # - COMPUTER + # default: + # TV + # nullable: false + new_column: + type: string + enum: + - ONE + - TWO + - THREE + default: + ONE + nullable: false + Editcolumn: type: object description: Table with edit enum columns for migration code generation diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 440c7de4..20fa08df 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -41,6 +41,26 @@ public function testEnumFresh() // $this->compareFiles($actualFiles, $expectedFiles); } + public function testAddNewEnumColumn() + { + $this->deleteTables(); + $this->createTableForNewEnumColumn(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'mysql'); + + $this->changeDbToMariadb(); + $this->deleteTables(); + $this->createTableForNewEnumColumn(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'maria'); + + $this->changeDbToPgsql(); + $this->deleteTables(); + $this->createTableForNewEnumColumn(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'pgsql'); + } + public function testEnumToString() { $this->deleteTables(); @@ -83,6 +103,9 @@ private function deleteTables() { if (ApiGenerator::isPostgres()) { Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_device CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_connection CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_new_column CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_delete_col CASCADE')->execute(); } Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute(); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%newcolumns}}')->execute(); @@ -107,4 +130,21 @@ private function createTableForEditEnumToString() 'connection' => 'string' ])->execute(); } + + private function createTableForNewEnumColumn() + { + if (ApiGenerator::isPostgres()) { + Yii::$app->db->createCommand('CREATE TYPE enum_delete_col AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')')->execute(); + Yii::$app->db->createCommand()->createTable('{{%newcolumns}}', [ + 'id' => 'pk', + 'delete_col' => 'enum_delete_col' + ])->execute(); + return; + } + + Yii::$app->db->createCommand()->createTable('{{%newcolumns}}', [ + 'id' => 'pk', + 'delete_col' => 'enum("FOUR", "FIVE", "SIX")' + ])->execute(); + } } From dae8e4cb86ba9d432fd0a99cadaaef0c4b9fc20c Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 24 Dec 2022 19:53:33 +0530 Subject: [PATCH 19/62] Implement Enum change value migration - WIP --- tests/specs/enum/enum.yaml | 23 +++++++++++++++++++ tests/unit/EnumTest.php | 45 +++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/specs/enum/enum.yaml b/tests/specs/enum/enum.yaml index 72a0e894..c130b3d2 100644 --- a/tests/specs/enum/enum.yaml +++ b/tests/specs/enum/enum.yaml @@ -78,3 +78,26 @@ components: default: WIRED nullable: false + + Enumvaluechange: + type: object + description: Table with enum value change for migration code generation + required: + - id + properties: + id: + type: integer + add_one_mood_at_last: + type: string + enum: + # admiration, adoration, appreciation of beauty, amusement, anger, anxiety, awe, awkwardness, boredom, calmness, confusion, craving, disgust, empathic pain, entrancement, excitement, fear, horror, interest, joy, nostalgia, relief + - INTEREST + - JOY + - NOSTALGIA + - RELIEF # this is it + remove_last_mood: + type: string + enum: + - INTEREST + - JOY + diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 20fa08df..1c7b5ffa 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -61,7 +61,7 @@ public function testAddNewEnumColumn() $this->runGenerator($testFile, 'pgsql'); } - public function testEnumToString() + public function testEnumToString() // edit { $this->deleteTables(); $this->createTableForEditEnumToString(); @@ -83,6 +83,28 @@ public function testEnumToString() $this->runGenerator($testFile, 'pgsql'); } + public function testEnumValuesChange() + { + $this->deleteTables(); + $this->createTableForEnumValueChange(); + $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->runGenerator($testFile, 'mysql'); + + + // $this->changeDbToMariadb(); + // $this->deleteTables(); + // $this->createTableForEditEnumToString(); + // $testFile = Yii::getAlias("@specs/enum/enum.php"); + // $this->runGenerator($testFile, 'maria'); + + + // $this->changeDbToPgsql(); + // $this->deleteTables(); + // $this->createTableForEditEnumToString(); + // $testFile = Yii::getAlias("@specs/enum/enum.php"); + // $this->runGenerator($testFile, 'pgsql'); + } + // public function testStringToEnum() // { // $this->deleteTables(); @@ -106,11 +128,13 @@ private function deleteTables() Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_connection CASCADE')->execute(); Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_new_column CASCADE')->execute(); Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_delete_col CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_add_one_mood_at_last CASCADE')->execute(); } Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute(); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%newcolumns}}')->execute(); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%editcolumns}}')->execute(); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%alldbdatatypes}}')->execute(); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%enumvaluechanges}}')->execute(); } private function createTableForEditEnumToString() @@ -147,4 +171,23 @@ private function createTableForNewEnumColumn() 'delete_col' => 'enum("FOUR", "FIVE", "SIX")' ])->execute(); } + + private function createTableForEnumValueChange() + { + // removing a enum value is directly not supported in PgSQL + if (ApiGenerator::isPostgres()) { + Yii::$app->db->createCommand('CREATE TYPE enum_add_one_mood_at_last AS ENUM(\'INTEREST\', \'JOY\', \'NOSTALGIA\')')->execute(); + Yii::$app->db->createCommand()->createTable('{{%enumvaluechanges}}', [ + 'id' => 'pk', + 'add_one_mood_at_last' => 'enum_add_one_mood_at_last' + ])->execute(); + return; + } + + Yii::$app->db->createCommand()->createTable('{{%enumvaluechanges}}', [ + 'id' => 'pk', + 'add_one_mood_at_last' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + 'remove_last_mood' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + ])->execute(); + } } From e67e8e1757a11b3b0e4b6452625e1078db880781 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 24 Dec 2022 20:43:45 +0530 Subject: [PATCH 20/62] WIP --- tests/specs/enum/enum.yaml | 13 +++++++++++++ tests/unit/EnumTest.php | 2 ++ 2 files changed, 15 insertions(+) diff --git a/tests/specs/enum/enum.yaml b/tests/specs/enum/enum.yaml index c130b3d2..980de919 100644 --- a/tests/specs/enum/enum.yaml +++ b/tests/specs/enum/enum.yaml @@ -100,4 +100,17 @@ components: enum: - INTEREST - JOY + add_mood_in_between: + type: string + enum: + - INTEREST + - RELIEF # this is it + - JOY + - NOSTALGIA + rename_last_mood: + type: string + enum: + - INTEREST + - JOY + - NOSTALGIA_2 diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 1c7b5ffa..286a3712 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -188,6 +188,8 @@ private function createTableForEnumValueChange() 'id' => 'pk', 'add_one_mood_at_last' => 'enum("INTEREST", "JOY", "NOSTALGIA")', 'remove_last_mood' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + 'add_mood_in_between' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + 'rename_last_mood' => 'enum("INTEREST", "JOY", "NOSTALGIA")', ])->execute(); } } From ded2d2bcb4e81c16e989a24958ae47d4452c692f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 27 Dec 2022 13:08:44 +0530 Subject: [PATCH 21/62] Complete enum tests + add stub for enum values change test --- 1 | 70 +++++++ README.md | 3 +- src/lib/migrations/BaseMigrationBuilder.php | 3 +- tests/DbTestCase.php | 15 ++ ...200000_000000_change_table_editcolumns.php | 19 ++ ...m200000_000001_create_table_newcolumns.php | 20 ++ .../m200000_000002_create_table_pristines.php | 20 ++ ...200000_000000_change_table_editcolumns.php | 19 ++ ...m200000_000001_create_table_newcolumns.php | 20 ++ .../m200000_000002_create_table_pristines.php | 20 ++ ...200000_000000_change_table_editcolumns.php | 31 +++ ...m200000_000001_create_table_newcolumns.php | 22 ++ .../m200000_000002_create_table_pristines.php | 22 ++ ...200000_000000_create_table_editcolumns.php | 21 ++ ...m200000_000001_create_table_newcolumns.php | 20 ++ .../m200000_000002_create_table_pristines.php | 20 ++ ...200000_000000_create_table_editcolumns.php | 21 ++ ...m200000_000001_create_table_newcolumns.php | 20 ++ .../m200000_000002_create_table_pristines.php | 20 ++ tests/specs/enum/{ => fresh/mysql}/enum.php | 2 +- tests/specs/enum/{ => fresh/mysql}/enum.yaml | 68 +++--- ...200000_000000_create_table_editcolumns.php | 23 +++ ...m200000_000001_create_table_newcolumns.php | 22 ++ .../m200000_000002_create_table_pristines.php | 22 ++ ...200000_000000_create_table_editcolumns.php | 21 ++ ...m200000_000001_change_table_newcolumns.php | 19 ++ .../m200000_000002_create_table_pristines.php | 20 ++ ...200000_000000_create_table_editcolumns.php | 21 ++ ...m200000_000001_change_table_newcolumns.php | 19 ++ .../m200000_000002_create_table_pristines.php | 20 ++ ...200000_000000_create_table_editcolumns.php | 23 +++ ...m200000_000001_change_table_newcolumns.php | 23 +++ .../m200000_000002_create_table_pristines.php | 22 ++ tests/unit/EnumTest.php | 193 ++++++++++++------ tests/unit/XDbTypeTest.php | 33 +-- 35 files changed, 833 insertions(+), 124 deletions(-) create mode 100644 1 create mode 100644 tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php create mode 100644 tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php create mode 100644 tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php create mode 100644 tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php create mode 100644 tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php create mode 100644 tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php create mode 100644 tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php create mode 100644 tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php create mode 100644 tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php create mode 100644 tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php create mode 100644 tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php create mode 100644 tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php create mode 100644 tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php create mode 100644 tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php create mode 100644 tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php rename tests/specs/enum/{ => fresh/mysql}/enum.php (80%) rename tests/specs/enum/{ => fresh/mysql}/enum.yaml (59%) create mode 100644 tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php create mode 100644 tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php create mode 100644 tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php create mode 100644 tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php create mode 100644 tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000001_change_table_newcolumns.php create mode 100644 tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php create mode 100644 tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php create mode 100644 tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000001_change_table_newcolumns.php create mode 100644 tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php create mode 100644 tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php create mode 100644 tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php create mode 100644 tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php diff --git a/1 b/1 new file mode 100644 index 00000000..5ca2c12a --- /dev/null +++ b/1 @@ -0,0 +1,70 @@ +[196] Log opened at 2022-12-27 07:37:06 +[196] I: Checking remote connect back address. +[196] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[196] I: Checking header 'REMOTE_ADDR'. +[196] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[196] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[196] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[196] E: Could not connect to client. :-( +[196] Log closed at 2022-12-27 07:37:06 + +[200] Log opened at 2022-12-27 07:37:39 +[200] I: Checking remote connect back address. +[200] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[200] I: Checking header 'REMOTE_ADDR'. +[200] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[200] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[200] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[200] E: Could not connect to client. :-( +[200] Log closed at 2022-12-27 07:37:39 + +[201] Log opened at 2022-12-27 07:37:39 +[201] I: Checking remote connect back address. +[201] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[201] I: Checking header 'REMOTE_ADDR'. +[201] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[201] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[201] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[201] E: Could not connect to client. :-( +[201] Log closed at 2022-12-27 07:37:39 + +[202] Log opened at 2022-12-27 07:37:39 +[202] I: Checking remote connect back address. +[202] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[202] I: Checking header 'REMOTE_ADDR'. +[202] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[202] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[202] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[202] E: Could not connect to client. :-( +[202] Log closed at 2022-12-27 07:37:39 + +[213] Log opened at 2022-12-27 07:37:50 +[213] I: Checking remote connect back address. +[213] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[213] I: Checking header 'REMOTE_ADDR'. +[213] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[213] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[213] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[213] E: Could not connect to client. :-( +[213] Log closed at 2022-12-27 07:37:50 + +[214] Log opened at 2022-12-27 07:37:50 +[214] I: Checking remote connect back address. +[214] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[214] I: Checking header 'REMOTE_ADDR'. +[214] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[214] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[214] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[214] E: Could not connect to client. :-( +[214] Log closed at 2022-12-27 07:37:50 + +[215] Log opened at 2022-12-27 07:37:50 +[215] I: Checking remote connect back address. +[215] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[215] I: Checking header 'REMOTE_ADDR'. +[215] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[215] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[215] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[215] E: Could not connect to client. :-( +[215] Log closed at 2022-12-27 07:37:50 + diff --git a/README.md b/README.md index 94b53f46..4bdff4d3 100644 --- a/README.md +++ b/README.md @@ -372,9 +372,10 @@ Generated files: # Development -There commands are available to develop and check the tests. It can be used inside the Docker container. To enter into bash of container run `make cli` . +There commands are available to develop and check the tests. It is available inside the Docker container. To enter into bash shell of container, run `make cli` . ```bash +cd tests ./yii migrate-mysql/up ./yii migrate-mysql/down 4 diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index d091b63a..e678bf16 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -466,8 +466,7 @@ public static function isEnum(\yii\db\ColumnSchema $columnSchema): bool public static function isEnumValuesChanged( \yii\db\ColumnSchema $current, \yii\db\ColumnSchema $desired - ): bool - { + ): bool { if (static::isEnum($current) && static::isEnum($desired) && $current->enumValues !== $desired->enumValues) { return true; diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index 8e59422a..1013cea0 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -89,4 +89,19 @@ protected function changeDbToPgsql() self::assertNotInstanceOf(MySqlSchema::class, Yii::$app->db->schema); self::assertInstanceOf(PgSqlSchema::class, Yii::$app->db->schema); } + + protected function checkFiles(array $actual, array $expected) + { + self::assertEquals( + count($actual), + count($expected) + ); + foreach ($actual as $index => $file) { + $expectedFilePath = $expected[$index]; + self::assertFileExists($file); + self::assertFileExists($expectedFilePath); + + $this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath"); + } + } } diff --git a/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php new file mode 100644 index 00000000..9d0b16ab --- /dev/null +++ b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php @@ -0,0 +1,19 @@ +alterColumn('{{%editcolumns}}', 'connection', 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\''); + $this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null()->defaultValue(null)); + } + + public function down() + { + $this->alterColumn('{{%editcolumns}}', 'device', 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\''); + $this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null()->defaultValue(null)); + } +} diff --git a/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php new file mode 100644 index 00000000..36971de6 --- /dev/null +++ b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php @@ -0,0 +1,20 @@ +createTable('{{%newcolumns}}', [ + 'id' => $this->primaryKey(), + 'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'', + ]); + } + + public function down() + { + $this->dropTable('{{%newcolumns}}'); + } +} diff --git a/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..bdd46f98 --- /dev/null +++ b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,20 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', + ]); + } + + public function down() + { + $this->dropTable('{{%pristines}}'); + } +} diff --git a/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php new file mode 100644 index 00000000..7a79e1fc --- /dev/null +++ b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php @@ -0,0 +1,19 @@ +alterColumn('{{%editcolumns}}', 'connection', 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\''); + $this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null()); + } + + public function down() + { + $this->alterColumn('{{%editcolumns}}', 'device', 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\''); + $this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null()->defaultValue(null)); + } +} diff --git a/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php new file mode 100644 index 00000000..36971de6 --- /dev/null +++ b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php @@ -0,0 +1,20 @@ +createTable('{{%newcolumns}}', [ + 'id' => $this->primaryKey(), + 'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'', + ]); + } + + public function down() + { + $this->dropTable('{{%newcolumns}}'); + } +} diff --git a/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..bdd46f98 --- /dev/null +++ b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,20 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', + ]); + } + + public function down() + { + $this->dropTable('{{%pristines}}'); + } +} 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 new file mode 100644 index 00000000..5069d918 --- /dev/null +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php @@ -0,0 +1,31 @@ +execute('CREATE TYPE enum_connection AS ENUM(\'WIRED\', \'WIRELESS\')'); + $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', "DROP NOT NULL"); + $this->alterColumn('{{%editcolumns}}', 'device', "DROP DEFAULT"); + $this->execute('DROP TYPE enum_device'); + } + + 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', "DROP NOT NULL"); + $this->alterColumn('{{%editcolumns}}', 'connection', "DROP DEFAULT"); + $this->execute('DROP TYPE enum_connection'); + $this->alterColumn('{{%editcolumns}}', 'device', "SET NOT NULL"); + $this->alterColumn('{{%editcolumns}}', 'device', "SET DEFAULT 'TV'"); + } +} diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php new file mode 100644 index 00000000..75c86ca3 --- /dev/null +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -0,0 +1,22 @@ +execute('CREATE TYPE enum_new_column AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->createTable('{{%newcolumns}}', [ + 'id' => $this->primaryKey(), + 'new_column' => 'enum_new_column NOT NULL DEFAULT \'ONE\'', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%newcolumns}}'); + $this->execute('DROP TYPE enum_new_column'); + } +} diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..57dbe3c2 --- /dev/null +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,22 @@ +execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%pristines}}'); + $this->execute('DROP TYPE enum_device'); + } +} diff --git a/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php new file mode 100644 index 00000000..1778bc15 --- /dev/null +++ b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php @@ -0,0 +1,21 @@ +createTable('{{%editcolumns}}', [ + 'id' => $this->primaryKey(), + 'device' => $this->text()->null()->defaultValue(null), + 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + ]); + } + + public function down() + { + $this->dropTable('{{%editcolumns}}'); + } +} diff --git a/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php new file mode 100644 index 00000000..36971de6 --- /dev/null +++ b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000001_create_table_newcolumns.php @@ -0,0 +1,20 @@ +createTable('{{%newcolumns}}', [ + 'id' => $this->primaryKey(), + 'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'', + ]); + } + + public function down() + { + $this->dropTable('{{%newcolumns}}'); + } +} diff --git a/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..bdd46f98 --- /dev/null +++ b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,20 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', + ]); + } + + public function down() + { + $this->dropTable('{{%pristines}}'); + } +} diff --git a/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php new file mode 100644 index 00000000..78e27f4f --- /dev/null +++ b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php @@ -0,0 +1,21 @@ +createTable('{{%editcolumns}}', [ + 'id' => $this->primaryKey(), + 'device' => $this->text()->null(), + 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + ]); + } + + public function down() + { + $this->dropTable('{{%editcolumns}}'); + } +} diff --git a/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php new file mode 100644 index 00000000..36971de6 --- /dev/null +++ b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000001_create_table_newcolumns.php @@ -0,0 +1,20 @@ +createTable('{{%newcolumns}}', [ + 'id' => $this->primaryKey(), + 'new_column' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\'', + ]); + } + + public function down() + { + $this->dropTable('{{%newcolumns}}'); + } +} diff --git a/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..bdd46f98 --- /dev/null +++ b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,20 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', + ]); + } + + public function down() + { + $this->dropTable('{{%pristines}}'); + } +} diff --git a/tests/specs/enum/enum.php b/tests/specs/enum/fresh/mysql/enum.php similarity index 80% rename from tests/specs/enum/enum.php rename to tests/specs/enum/fresh/mysql/enum.php index 30e7ae15..fa0cac3a 100644 --- a/tests/specs/enum/enum.php +++ b/tests/specs/enum/fresh/mysql/enum.php @@ -1,7 +1,7 @@ '@specs/enum/enum.yaml', + 'openApiPath' => '@specs/enum/fresh/mysql/enum.yaml', 'generateUrls' => false, 'generateModels' => false, 'excludeModels' => [ diff --git a/tests/specs/enum/enum.yaml b/tests/specs/enum/fresh/mysql/enum.yaml similarity index 59% rename from tests/specs/enum/enum.yaml rename to tests/specs/enum/fresh/mysql/enum.yaml index 980de919..3017f013 100644 --- a/tests/specs/enum/enum.yaml +++ b/tests/specs/enum/fresh/mysql/enum.yaml @@ -79,38 +79,38 @@ components: WIRED nullable: false - Enumvaluechange: - type: object - description: Table with enum value change for migration code generation - required: - - id - properties: - id: - type: integer - add_one_mood_at_last: - type: string - enum: - # admiration, adoration, appreciation of beauty, amusement, anger, anxiety, awe, awkwardness, boredom, calmness, confusion, craving, disgust, empathic pain, entrancement, excitement, fear, horror, interest, joy, nostalgia, relief - - INTEREST - - JOY - - NOSTALGIA - - RELIEF # this is it - remove_last_mood: - type: string - enum: - - INTEREST - - JOY - add_mood_in_between: - type: string - enum: - - INTEREST - - RELIEF # this is it - - JOY - - NOSTALGIA - rename_last_mood: - type: string - enum: - - INTEREST - - JOY - - NOSTALGIA_2 + # Enumvaluechange: + # type: object + # description: Table with enum value change for migration code generation + # required: + # - id + # properties: + # id: + # type: integer + # add_one_mood_at_last: + # type: string + # enum: + # # admiration, adoration, appreciation of beauty, amusement, anger, anxiety, awe, awkwardness, boredom, calmness, confusion, craving, disgust, empathic pain, entrancement, excitement, fear, horror, interest, joy, nostalgia, relief + # - INTEREST + # - JOY + # - NOSTALGIA + # - RELIEF # this is it + # remove_last_mood: + # type: string + # enum: + # - INTEREST + # - JOY + # add_mood_in_between: + # type: string + # enum: + # - INTEREST + # - RELIEF # this is it + # - JOY + # - NOSTALGIA + # rename_last_mood: + # type: string + # enum: + # - INTEREST + # - JOY + # - NOSTALGIA_2 diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php new file mode 100644 index 00000000..5d4a4340 --- /dev/null +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -0,0 +1,23 @@ +execute('CREATE TYPE enum_connection AS ENUM(\'WIRED\', \'WIRELESS\')'); + $this->createTable('{{%editcolumns}}', [ + 'id' => $this->primaryKey(), + 'device' => $this->text()->null()->defaultValue(null), + 'connection' => 'enum_connection NOT NULL DEFAULT \'WIRED\'', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%editcolumns}}'); + $this->execute('DROP TYPE enum_connection'); + } +} diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php new file mode 100644 index 00000000..75c86ca3 --- /dev/null +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -0,0 +1,22 @@ +execute('CREATE TYPE enum_new_column AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->createTable('{{%newcolumns}}', [ + 'id' => $this->primaryKey(), + 'new_column' => 'enum_new_column NOT NULL DEFAULT \'ONE\'', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%newcolumns}}'); + $this->execute('DROP TYPE enum_new_column'); + } +} diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..57dbe3c2 --- /dev/null +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,22 @@ +execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%pristines}}'); + $this->execute('DROP TYPE enum_device'); + } +} diff --git a/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php new file mode 100644 index 00000000..1778bc15 --- /dev/null +++ b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php @@ -0,0 +1,21 @@ +createTable('{{%editcolumns}}', [ + 'id' => $this->primaryKey(), + 'device' => $this->text()->null()->defaultValue(null), + 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + ]); + } + + public function down() + { + $this->dropTable('{{%editcolumns}}'); + } +} diff --git a/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000001_change_table_newcolumns.php b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000001_change_table_newcolumns.php new file mode 100644 index 00000000..4ef6a191 --- /dev/null +++ b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000001_change_table_newcolumns.php @@ -0,0 +1,19 @@ +addColumn('{{%newcolumns}}', 'new_column', 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\''); + $this->dropColumn('{{%newcolumns}}', 'delete_col'); + } + + public function down() + { + $this->addColumn('{{%newcolumns}}', 'delete_col', 'enum("FOUR", "FIVE", "SIX") NULL DEFAULT NULL'); + $this->dropColumn('{{%newcolumns}}', 'new_column'); + } +} diff --git a/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..bdd46f98 --- /dev/null +++ b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,20 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', + ]); + } + + public function down() + { + $this->dropTable('{{%pristines}}'); + } +} diff --git a/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php new file mode 100644 index 00000000..78e27f4f --- /dev/null +++ b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php @@ -0,0 +1,21 @@ +createTable('{{%editcolumns}}', [ + 'id' => $this->primaryKey(), + 'device' => $this->text()->null(), + 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + ]); + } + + public function down() + { + $this->dropTable('{{%editcolumns}}'); + } +} diff --git a/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000001_change_table_newcolumns.php b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000001_change_table_newcolumns.php new file mode 100644 index 00000000..4ef6a191 --- /dev/null +++ b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000001_change_table_newcolumns.php @@ -0,0 +1,19 @@ +addColumn('{{%newcolumns}}', 'new_column', 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'ONE\''); + $this->dropColumn('{{%newcolumns}}', 'delete_col'); + } + + public function down() + { + $this->addColumn('{{%newcolumns}}', 'delete_col', 'enum("FOUR", "FIVE", "SIX") NULL DEFAULT NULL'); + $this->dropColumn('{{%newcolumns}}', 'new_column'); + } +} diff --git a/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..bdd46f98 --- /dev/null +++ b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,20 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', + ]); + } + + public function down() + { + $this->dropTable('{{%pristines}}'); + } +} diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php new file mode 100644 index 00000000..5d4a4340 --- /dev/null +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -0,0 +1,23 @@ +execute('CREATE TYPE enum_connection AS ENUM(\'WIRED\', \'WIRELESS\')'); + $this->createTable('{{%editcolumns}}', [ + 'id' => $this->primaryKey(), + 'device' => $this->text()->null()->defaultValue(null), + 'connection' => 'enum_connection NOT NULL DEFAULT \'WIRED\'', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%editcolumns}}'); + $this->execute('DROP TYPE enum_connection'); + } +} diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php new file mode 100644 index 00000000..084a0be3 --- /dev/null +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php @@ -0,0 +1,23 @@ +execute('CREATE TYPE enum_new_column AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->addColumn('{{%newcolumns}}', 'new_column', 'enum_new_column NOT NULL DEFAULT \'ONE\''); + $this->dropColumn('{{%newcolumns}}', 'delete_col'); + $this->execute('DROP TYPE enum_delete_col'); + } + + public function safeDown() + { + $this->execute('CREATE TYPE enum_delete_col AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')'); + $this->addColumn('{{%newcolumns}}', 'delete_col', 'enum_delete_col NULL DEFAULT NULL'); + $this->dropColumn('{{%newcolumns}}', 'new_column'); + $this->execute('DROP TYPE enum_new_column'); + } +} diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php new file mode 100644 index 00000000..57dbe3c2 --- /dev/null +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -0,0 +1,22 @@ +execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%pristines}}'); + $this->execute('DROP TYPE enum_device'); + } +} diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 286a3712..247ee344 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -13,103 +13,172 @@ use function getenv; use function strpos; +// For all scenarios we use same yaml file. It has all necessary data for all schenarios. Also see createTable method in this class class EnumTest extends DbTestCase { - public function testEnumFresh() + public function testFresh() { // default DB is Mysql ------------------------------------------------ - // $this->deleteTables(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->deleteTables(); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'mysql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_maria_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/fresh/mysql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); $this->changeDbToMariadb(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->deleteTables(); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'maria'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/fresh/maria/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); $this->changeDbToPgsql(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $this->deleteTables(); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'pgsql'); - - - // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ - // 'recursive' => true, - // 'except' => ['migrations_maria_db', 'migrations_pgsql_db'] - // ]); - // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/fresh/mysql/app"), [ - // 'recursive' => true, - // ]); - // $this->compareFiles($actualFiles, $expectedFiles); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_maria_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/fresh/pgsql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); } - public function testAddNewEnumColumn() + public function testAddNewColumn() // and drop enum column { + // MySQL $this->deleteTables(); $this->createTableForNewEnumColumn(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'mysql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_maria_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/new_column/mysql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + // Mariadb $this->changeDbToMariadb(); $this->deleteTables(); $this->createTableForNewEnumColumn(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'maria'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/new_column/maria/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + // Pgsql $this->changeDbToPgsql(); $this->deleteTables(); $this->createTableForNewEnumColumn(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'pgsql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_maria_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/new_column/pgsql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); } - public function testEnumToString() // edit + public function testChangeToAndFromEnum() // edit enum to string and vice versa { $this->deleteTables(); $this->createTableForEditEnumToString(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'mysql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_maria_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/change/mysql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); - + // Mariadb $this->changeDbToMariadb(); $this->deleteTables(); $this->createTableForEditEnumToString(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'maria'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/change/maria/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); $this->changeDbToPgsql(); $this->deleteTables(); $this->createTableForEditEnumToString(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); + $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); $this->runGenerator($testFile, 'pgsql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_maria_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/enum/change/pgsql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); } - public function testEnumValuesChange() - { - $this->deleteTables(); - $this->createTableForEnumValueChange(); - $testFile = Yii::getAlias("@specs/enum/enum.php"); - $this->runGenerator($testFile, 'mysql'); + // TODO ENH enum change is more work than just changing the eunm values. And for PgSQL it is even more + // public function testEnumValuesChange() + // { + // $this->deleteTables(); + // $this->createTableForEnumValueChange(); + // $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); + // $this->runGenerator($testFile, 'mysql'); - // $this->changeDbToMariadb(); - // $this->deleteTables(); - // $this->createTableForEditEnumToString(); - // $testFile = Yii::getAlias("@specs/enum/enum.php"); - // $this->runGenerator($testFile, 'maria'); + // $this->changeDbToMariadb(); + // $this->deleteTables(); + // $this->createTableForEnumValueChange(); + // $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); + // $this->runGenerator($testFile, 'maria'); - // $this->changeDbToPgsql(); - // $this->deleteTables(); - // $this->createTableForEditEnumToString(); - // $testFile = Yii::getAlias("@specs/enum/enum.php"); - // $this->runGenerator($testFile, 'pgsql'); - } + // $this->changeDbToPgsql(); + // $this->deleteTables(); + // $this->createTableForEnumValueChange(); + // $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); + // $this->runGenerator($testFile, 'pgsql'); + // } // public function testStringToEnum() // { // $this->deleteTables(); // $this->createTableForEditEnumToString(); - // $testFile = Yii::getAlias("@specs/enum/enum.php"); + // $testFile = Yii::getAlias("@specs/enum/fresh/mysql/enum.php"); // $this->runGenerator($testFile, 'mysql'); // } @@ -137,7 +206,7 @@ private function deleteTables() Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%enumvaluechanges}}')->execute(); } - private function createTableForEditEnumToString() + private function createTableForEditEnumToString() // and vice versa { if (ApiGenerator::isPostgres()) { Yii::$app->db->createCommand('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')')->execute(); @@ -172,24 +241,24 @@ private function createTableForNewEnumColumn() ])->execute(); } - private function createTableForEnumValueChange() - { - // removing a enum value is directly not supported in PgSQL - if (ApiGenerator::isPostgres()) { - Yii::$app->db->createCommand('CREATE TYPE enum_add_one_mood_at_last AS ENUM(\'INTEREST\', \'JOY\', \'NOSTALGIA\')')->execute(); - Yii::$app->db->createCommand()->createTable('{{%enumvaluechanges}}', [ - 'id' => 'pk', - 'add_one_mood_at_last' => 'enum_add_one_mood_at_last' - ])->execute(); - return; - } + // private function createTableForEnumValueChange() + // { + // // removing a enum value is directly not supported in PgSQL + // if (ApiGenerator::isPostgres()) { + // Yii::$app->db->createCommand('CREATE TYPE enum_add_one_mood_at_last AS ENUM(\'INTEREST\', \'JOY\', \'NOSTALGIA\')')->execute(); + // Yii::$app->db->createCommand()->createTable('{{%enumvaluechanges}}', [ + // 'id' => 'pk', + // 'add_one_mood_at_last' => 'enum_add_one_mood_at_last' + // ])->execute(); + // return; + // } - Yii::$app->db->createCommand()->createTable('{{%enumvaluechanges}}', [ - 'id' => 'pk', - 'add_one_mood_at_last' => 'enum("INTEREST", "JOY", "NOSTALGIA")', - 'remove_last_mood' => 'enum("INTEREST", "JOY", "NOSTALGIA")', - 'add_mood_in_between' => 'enum("INTEREST", "JOY", "NOSTALGIA")', - 'rename_last_mood' => 'enum("INTEREST", "JOY", "NOSTALGIA")', - ])->execute(); - } + // Yii::$app->db->createCommand()->createTable('{{%enumvaluechanges}}', [ + // 'id' => 'pk', + // 'add_one_mood_at_last' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + // 'remove_last_mood' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + // 'add_mood_in_between' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + // 'rename_last_mood' => 'enum("INTEREST", "JOY", "NOSTALGIA")', + // ])->execute(); + // } } diff --git a/tests/unit/XDbTypeTest.php b/tests/unit/XDbTypeTest.php index 28385841..7f492c22 100644 --- a/tests/unit/XDbTypeTest.php +++ b/tests/unit/XDbTypeTest.php @@ -28,7 +28,7 @@ public function testXDbTypeFresh() $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/fresh/mysql/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); // same yaml file is used for MySQL and MariaDB ---------------------- $this->changeDbToMariadb(); @@ -42,7 +42,7 @@ public function testXDbTypeFresh() $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/fresh/maria/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); // PgSQL ------------------------------------------------ $this->changeDbToPgsql(); @@ -56,7 +56,7 @@ public function testXDbTypeFresh() $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/fresh/pgsql/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); } public function testXDbTypeSecondaryWithNewColumn() // v2 @@ -73,7 +73,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2 $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/new_column/mysql/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); // same yaml file is used for MySQL and MariaDB ---------------------- $this->changeDbToMariadb(); @@ -89,7 +89,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2 $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/new_column/maria/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); // PgSQL ------------------------------------------------ $this->changeDbToPgsql(); @@ -105,7 +105,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2 $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/new_column/pgsql/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); } public function testXDbTypeSecondaryWithEditColumn() // v3 @@ -122,7 +122,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3 $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/edit_column/mysql/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); // same yaml file is used for MySQL and MariaDB ---------------------- $this->changeDbToMariadb(); @@ -138,7 +138,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3 $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/edit_column/maria/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); + $this->checkFiles($actualFiles, $expectedFiles); // PgSQL ------------------------------------------------ $this->changeDbToPgsql(); @@ -154,22 +154,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3 $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/x_db_type/edit_column/pgsql/app"), [ 'recursive' => true, ]); - $this->compareFiles($actualFiles, $expectedFiles); - } - - protected function compareFiles(array $actual, array $expected) - { - self::assertEquals( - count($actual), - count($expected) - ); - foreach ($actual as $index => $file) { - $expectedFilePath = $expected[$index]; - self::assertFileExists($file); - self::assertFileExists($expectedFilePath); - - $this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath"); - } + $this->checkFiles($actualFiles, $expectedFiles); } private function deleteTables() From af614eb918136287ffd07686dc3ccfc144364821 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 27 Dec 2022 13:12:43 +0530 Subject: [PATCH 22/62] Remove unwanted files --- 1 | 70 --------------------------------------------------------------- 1 file changed, 70 deletions(-) delete mode 100644 1 diff --git a/1 b/1 deleted file mode 100644 index 5ca2c12a..00000000 --- a/1 +++ /dev/null @@ -1,70 +0,0 @@ -[196] Log opened at 2022-12-27 07:37:06 -[196] I: Checking remote connect back address. -[196] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[196] I: Checking header 'REMOTE_ADDR'. -[196] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[196] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[196] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[196] E: Could not connect to client. :-( -[196] Log closed at 2022-12-27 07:37:06 - -[200] Log opened at 2022-12-27 07:37:39 -[200] I: Checking remote connect back address. -[200] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[200] I: Checking header 'REMOTE_ADDR'. -[200] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[200] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[200] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[200] E: Could not connect to client. :-( -[200] Log closed at 2022-12-27 07:37:39 - -[201] Log opened at 2022-12-27 07:37:39 -[201] I: Checking remote connect back address. -[201] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[201] I: Checking header 'REMOTE_ADDR'. -[201] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[201] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[201] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[201] E: Could not connect to client. :-( -[201] Log closed at 2022-12-27 07:37:39 - -[202] Log opened at 2022-12-27 07:37:39 -[202] I: Checking remote connect back address. -[202] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[202] I: Checking header 'REMOTE_ADDR'. -[202] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[202] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[202] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[202] E: Could not connect to client. :-( -[202] Log closed at 2022-12-27 07:37:39 - -[213] Log opened at 2022-12-27 07:37:50 -[213] I: Checking remote connect back address. -[213] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[213] I: Checking header 'REMOTE_ADDR'. -[213] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[213] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[213] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[213] E: Could not connect to client. :-( -[213] Log closed at 2022-12-27 07:37:50 - -[214] Log opened at 2022-12-27 07:37:50 -[214] I: Checking remote connect back address. -[214] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[214] I: Checking header 'REMOTE_ADDR'. -[214] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[214] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[214] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[214] E: Could not connect to client. :-( -[214] Log closed at 2022-12-27 07:37:50 - -[215] Log opened at 2022-12-27 07:37:50 -[215] I: Checking remote connect back address. -[215] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[215] I: Checking header 'REMOTE_ADDR'. -[215] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[215] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[215] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[215] E: Could not connect to client. :-( -[215] Log closed at 2022-12-27 07:37:50 - From 39d2ff03c97440453ed011ba09fa1fa7b4ba8e92 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 27 Dec 2022 13:28:25 +0530 Subject: [PATCH 23/62] Add docs in README --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4bdff4d3..e1a60f2d 100644 --- a/README.md +++ b/README.md @@ -316,8 +316,8 @@ e.g. attribute = 'my_property'. nullable: false ``` -### Handling of `enum` (#enum, #MariaDb) -It work on MariaDb. +### Handling of `enum` (#enum) +It works on all 3 DB: MySQL, MariaDb and PgSQL. ```yaml test_table: @@ -329,6 +329,8 @@ It work on MariaDb. - three ``` +Note: Change in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should be handles manually. + ### Handling of `numeric` (#numeric, #MariaDb) precision-default = 10 scale-default = 2 From 5badea4cc318f8eb405cc8c9900d1632eea960eb Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 27 Dec 2022 20:41:59 +0530 Subject: [PATCH 24/62] Restore tests that were deleted in https://github.com/cebe/yii2-openapi/pull/112/files + fix bugs + add multiple DB for PgSQL tests + enure all tests passes --- Makefile | 2 ++ src/lib/migrations/BaseMigrationBuilder.php | 18 ++++++---- .../migrations/PostgresMigrationBuilder.php | 2 +- tests/config/console.php | 7 ++++ tests/fixtures/blog.php | 6 +++- tests/migrations/m100000_000000_maria.php | 1 + tests/migrations/m100000_000000_mysql.php | 1 + tests/migrations/m100000_000000_pgsql.php | 4 +++ tests/specs/blog.yaml | 7 +++- .../m200000_000001_create_table_users.php | 5 +-- .../m200000_000001_create_table_users.php | 5 +-- .../m200000_000001_create_table_users.php | 5 +-- .../m200000_000001_create_table_users.php | 5 +-- tests/specs/blog/models/UserFaker.php | 1 + tests/specs/blog/models/base/User.php | 4 ++- tests/specs/blog_v2.yaml | 35 +++++++++++-------- .../m200000_000001_create_table_v2_users.php | 5 +-- .../m200000_000002_create_table_v2_posts.php | 1 + .../m200000_000003_create_table_v2_tags.php | 1 + .../m200000_000000_change_table_v2_posts.php | 2 ++ .../m200000_000001_create_table_v2_tags.php | 1 + .../m200000_000004_change_table_v2_users.php | 6 ++-- .../m200000_000000_change_table_v2_posts.php | 2 ++ .../m200000_000001_create_table_v2_tags.php | 1 + .../m200000_000004_change_table_v2_users.php | 6 ++-- .../m200000_000000_change_table_v2_posts.php | 4 +++ .../m200000_000001_create_table_v2_tags.php | 3 ++ .../m200000_000004_change_table_v2_users.php | 10 ++++-- tests/specs/blog_v2/models/PostFaker.php | 1 + tests/specs/blog_v2/models/TagFaker.php | 1 + tests/specs/blog_v2/models/UserFaker.php | 1 + tests/specs/blog_v2/models/base/Post.php | 5 ++- tests/specs/blog_v2/models/base/Tag.php | 7 ++-- tests/specs/blog_v2/models/base/User.php | 5 ++- tests/specs/postgres_custom.yaml | 14 ++++---- ...200000_000000_create_table_v3_pgcustom.php | 3 +- ...200000_000000_change_table_v3_pgcustom.php | 4 ++- .../postgres_custom/models/CustomFaker.php | 1 + .../postgres_custom/models/base/Custom.php | 4 +++ tests/unit/PropertySchemaTest.php | 1 + tests/unit/ValidatorRulesBuilderTest.php | 4 +++ 41 files changed, 147 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index 44086f91..718652a8 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,8 @@ 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) + docker-compose exec -T postgres bash -c "psql --username dbuser --dbname testdb -c 'create database pg_test_db_2;'; psql --username dbuser --dbname testdb -c 'grant all privileges on database pg_test_db_2 to dbuser;'" || (docker-compose ps; docker-compose logs; exit 1) + cli: docker-compose exec php bash diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index e678bf16..072628ad 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -416,8 +416,12 @@ protected function isNeedUsingExpression(string $fromType, string $toType):bool public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema { $tableName = 'tmp_table_'; + $db = 'db'; + if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { + $db = 'pg_test_db_2'; + } - Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); + Yii::$app->{$db}->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); if (is_string($columnSchema->xDbType) && !empty($columnSchema->xDbType)) { $column = [$columnSchema->name.' '.$this->newColStr($columnSchema)]; @@ -431,19 +435,19 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): $allEnumValues = array_map(function ($aValue) { return "'$aValue'"; }, $allEnumValues); - Yii::$app->db->createCommand( + Yii::$app->{$db}->createCommand( 'CREATE TYPE enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')' )->execute(); } - Yii::$app->db->createCommand()->createTable($tableName, $column)->execute(); + Yii::$app->{$db}->createCommand()->createTable($tableName, $column)->execute(); - $table = Yii::$app->db->getTableSchema($tableName); + $table = Yii::$app->{$db}->getTableSchema($tableName); - Yii::$app->db->createCommand()->dropTable($tableName)->execute(); + Yii::$app->{$db}->createCommand()->dropTable($tableName)->execute(); - if (ApiGenerator::isPostgres() && !empty($columnSchema->enumValues)) {// drop enum - Yii::$app->db->createCommand('DROP TYPE enum_'.$columnSchema->name)->execute(); + if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum + Yii::$app->{$db}->createCommand('DROP TYPE enum_'.$columnSchema->name)->execute(); } return $table->columns[$columnSchema->name]; diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index add24713..c1e109f6 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -67,7 +67,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir , 'dbType', 'phpType' , 'precision', 'scale', 'unsigned' ], $changed))) { - $addUsing = $this->isNeedUsingExpression($desired->type, $current->type); + $addUsing = $this->isNeedUsingExpression($current->type, $desired->type); $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired)); $this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing)); } diff --git a/tests/config/console.php b/tests/config/console.php index a562c131..e3bb729e 100644 --- a/tests/config/console.php +++ b/tests/config/console.php @@ -53,6 +53,13 @@ 'charset' => 'utf8', 'tablePrefix'=>'itt_', ], + 'pg_test_db_2' => [ + 'class' => \yii\db\Connection::class, + 'dsn' => 'pgsql:host=postgres;dbname=pg_test_db_2', + 'username' => 'dbuser', + 'password' => 'dbpass', + 'charset' => 'utf8', + ], 'mysql' => [ 'class' => \yii\db\Connection::class, 'dsn' => 'mysql:host=mysql;dbname=testdb', diff --git a/tests/fixtures/blog.php b/tests/fixtures/blog.php index c9737bb9..deffbd6d 100644 --- a/tests/fixtures/blog.php +++ b/tests/fixtures/blog.php @@ -19,6 +19,10 @@ ->setSize(200)->setRequired()->setFakerStub('substr($faker->safeEmail, 0, 200)'), 'password' => (new Attribute('password', ['phpType' => 'string', 'dbType' => 'string'])) ->setRequired()->setFakerStub('$faker->password'), + 'role' => (new Attribute('role', ['phpType' => 'string', 'dbType' => 'string'])) + ->setSize(20) + ->setDefault('reader') + ->setFakerStub('$faker->randomElement([\'admin\', \'editor\', \'reader\'])'), 'flags' => (new Attribute('flags', ['phpType'=>'int', 'dbType'=>'integer']))->setDefault(0)->setFakerStub ('$faker->numberBetween(0, 1000000)'), 'created_at' => (new Attribute('created_at', ['phpType' => 'string', 'dbType' => 'datetime'])) @@ -28,7 +32,7 @@ 'indexes' => [ 'users_email_key' => DbIndex::make('users', ['email'], null, true), 'users_username_key' => DbIndex::make('users', ['username'], null, true), - 'users_flags_index' => DbIndex::make('users', ['flags']) + 'users_role_flags_index' => DbIndex::make('users', ['role', 'flags']) ] ]), 'category' => new DbModel([ diff --git a/tests/migrations/m100000_000000_maria.php b/tests/migrations/m100000_000000_maria.php index 53dc91df..936f7c83 100644 --- a/tests/migrations/m100000_000000_maria.php +++ b/tests/migrations/m100000_000000_maria.php @@ -34,6 +34,7 @@ public function up() 'username' => $this->string(200)->notNull()->unique(), 'email' => $this->string(200)->notNull()->unique(), 'password' => $this->string()->notNull(), + 'role' => $this->string(20)->null()->defaultValue('reader'), 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP"), ]); diff --git a/tests/migrations/m100000_000000_mysql.php b/tests/migrations/m100000_000000_mysql.php index d1d0c19a..5b249c06 100644 --- a/tests/migrations/m100000_000000_mysql.php +++ b/tests/migrations/m100000_000000_mysql.php @@ -33,6 +33,7 @@ public function up() 'username' => $this->string(200)->notNull()->unique(), 'email' => $this->string(200)->notNull()->unique(), 'password' => $this->string()->notNull(), + 'role' => $this->string(20)->null()->defaultValue('reader'), 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP"), ]); diff --git a/tests/migrations/m100000_000000_pgsql.php b/tests/migrations/m100000_000000_pgsql.php index 0780107d..0a42c8e9 100644 --- a/tests/migrations/m100000_000000_pgsql.php +++ b/tests/migrations/m100000_000000_pgsql.php @@ -29,6 +29,7 @@ public function safeUp() 'username' => $this->string(200)->notNull()->unique(), 'email' => $this->string(200)->notNull()->unique(), 'password' => $this->string()->notNull(), + 'role' => $this->string(20)->null()->defaultValue('reader'), 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP"), ]); @@ -90,6 +91,7 @@ public function safeUp() 'str_country' => $this->text()->null()->defaultValue(null), ]); + $this->execute('CREATE TYPE enum_status AS ENUM(\'active\', \'draft\')'); $this->createTable('{{%v3_pgcustom}}', [ 'id' => $this->bigPrimaryKey(), @@ -98,6 +100,7 @@ public function safeUp() 'json2' => $this->json()->null()->defaultValue(null), 'json3' => $this->json()->defaultValue(Json::encode(['foo' => 'bar', 'bar' => 'baz'])), 'json4' => "json DEFAULT '" . new Expression(Json::encode(['ffo' => 'bar'])) . "'", + 'status' => 'enum_status', 'search' => 'tsvector' ]); $columns = [ @@ -135,6 +138,7 @@ public function safeDown() $this->dropTable('{{%v2_users}}'); $this->dropTable('{{%v2_categories}}'); $this->dropTable('{{%v3_pgcustom}}'); + $this->execute('DROP TYPE enum_status'); $this->dropTable('{{%default_sizes}}'); } } diff --git a/tests/specs/blog.yaml b/tests/specs/blog.yaml index cd03139d..1021b843 100644 --- a/tests/specs/blog.yaml +++ b/tests/specs/blog.yaml @@ -51,7 +51,7 @@ components: x-indexes: - unique:username - unique:email - - flags + - role,flags properties: id: type: integer @@ -66,6 +66,11 @@ components: password: type: string format: password + role: + type: string + maxLength: 20 + x-faker: "$faker->randomElement(['admin', 'editor', 'reader'])" + default: reader flags: type: integer default: 0 diff --git a/tests/specs/blog/migrations/m200000_000001_create_table_users.php b/tests/specs/blog/migrations/m200000_000001_create_table_users.php index 7afd35d4..f8cd744a 100644 --- a/tests/specs/blog/migrations/m200000_000001_create_table_users.php +++ b/tests/specs/blog/migrations/m200000_000001_create_table_users.php @@ -12,17 +12,18 @@ public function up() 'username' => $this->string(200)->notNull(), 'email' => $this->string(200)->notNull(), 'password' => $this->string()->notNull(), + 'role' => $this->string(20)->null()->defaultValue("reader"), 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP"), ]); $this->createIndex('users_username_key', '{{%users}}', 'username', true); $this->createIndex('users_email_key', '{{%users}}', 'email', true); - $this->createIndex('users_flags_index', '{{%users}}', 'flags', false); + $this->createIndex('users_role_flags_index', '{{%users}}', 'role,flags', false); } public function down() { - $this->dropIndex('users_flags_index', '{{%users}}'); + $this->dropIndex('users_role_flags_index', '{{%users}}'); $this->dropIndex('users_email_key', '{{%users}}'); $this->dropIndex('users_username_key', '{{%users}}'); $this->dropTable('{{%users}}'); diff --git a/tests/specs/blog/migrations_maria_db/m200000_000001_create_table_users.php b/tests/specs/blog/migrations_maria_db/m200000_000001_create_table_users.php index 7afd35d4..f8cd744a 100644 --- a/tests/specs/blog/migrations_maria_db/m200000_000001_create_table_users.php +++ b/tests/specs/blog/migrations_maria_db/m200000_000001_create_table_users.php @@ -12,17 +12,18 @@ public function up() 'username' => $this->string(200)->notNull(), 'email' => $this->string(200)->notNull(), 'password' => $this->string()->notNull(), + 'role' => $this->string(20)->null()->defaultValue("reader"), 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP"), ]); $this->createIndex('users_username_key', '{{%users}}', 'username', true); $this->createIndex('users_email_key', '{{%users}}', 'email', true); - $this->createIndex('users_flags_index', '{{%users}}', 'flags', false); + $this->createIndex('users_role_flags_index', '{{%users}}', 'role,flags', false); } public function down() { - $this->dropIndex('users_flags_index', '{{%users}}'); + $this->dropIndex('users_role_flags_index', '{{%users}}'); $this->dropIndex('users_email_key', '{{%users}}'); $this->dropIndex('users_username_key', '{{%users}}'); $this->dropTable('{{%users}}'); diff --git a/tests/specs/blog/migrations_mysql_db/m200000_000001_create_table_users.php b/tests/specs/blog/migrations_mysql_db/m200000_000001_create_table_users.php index 7afd35d4..f8cd744a 100644 --- a/tests/specs/blog/migrations_mysql_db/m200000_000001_create_table_users.php +++ b/tests/specs/blog/migrations_mysql_db/m200000_000001_create_table_users.php @@ -12,17 +12,18 @@ public function up() 'username' => $this->string(200)->notNull(), 'email' => $this->string(200)->notNull(), 'password' => $this->string()->notNull(), + 'role' => $this->string(20)->null()->defaultValue("reader"), 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP"), ]); $this->createIndex('users_username_key', '{{%users}}', 'username', true); $this->createIndex('users_email_key', '{{%users}}', 'email', true); - $this->createIndex('users_flags_index', '{{%users}}', 'flags', false); + $this->createIndex('users_role_flags_index', '{{%users}}', 'role,flags', false); } public function down() { - $this->dropIndex('users_flags_index', '{{%users}}'); + $this->dropIndex('users_role_flags_index', '{{%users}}'); $this->dropIndex('users_email_key', '{{%users}}'); $this->dropIndex('users_username_key', '{{%users}}'); $this->dropTable('{{%users}}'); diff --git a/tests/specs/blog/migrations_pgsql_db/m200000_000001_create_table_users.php b/tests/specs/blog/migrations_pgsql_db/m200000_000001_create_table_users.php index ceeb4494..23fadf71 100644 --- a/tests/specs/blog/migrations_pgsql_db/m200000_000001_create_table_users.php +++ b/tests/specs/blog/migrations_pgsql_db/m200000_000001_create_table_users.php @@ -12,17 +12,18 @@ public function safeUp() 'username' => $this->string(200)->notNull(), 'email' => $this->string(200)->notNull(), 'password' => $this->string()->notNull(), + 'role' => $this->string(20)->null()->defaultValue("reader"), 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP"), ]); $this->createIndex('users_username_key', '{{%users}}', 'username', true); $this->createIndex('users_email_key', '{{%users}}', 'email', true); - $this->createIndex('users_flags_index', '{{%users}}', 'flags', false); + $this->createIndex('users_role_flags_index', '{{%users}}', 'role,flags', false); } public function safeDown() { - $this->dropIndex('users_flags_index', '{{%users}}'); + $this->dropIndex('users_role_flags_index', '{{%users}}'); $this->dropIndex('users_email_key', '{{%users}}'); $this->dropIndex('users_username_key', '{{%users}}'); $this->dropTable('{{%users}}'); diff --git a/tests/specs/blog/models/UserFaker.php b/tests/specs/blog/models/UserFaker.php index 943eea86..d42fb940 100644 --- a/tests/specs/blog/models/UserFaker.php +++ b/tests/specs/blog/models/UserFaker.php @@ -33,6 +33,7 @@ public function generateModel($attributes = []) $model->username = substr($faker->userName, 0, 200); $model->email = substr($faker->safeEmail, 0, 200); $model->password = $faker->password; + $model->role = $faker->randomElement(['admin', 'editor', 'reader']); $model->flags = $faker->numberBetween(0, 1000000); $model->created_at = $faker->dateTimeThisYear('now', 'UTC')->format(DATE_ATOM); if (!is_callable($attributes)) { diff --git a/tests/specs/blog/models/base/User.php b/tests/specs/blog/models/base/User.php index bc2f5493..614718da 100644 --- a/tests/specs/blog/models/base/User.php +++ b/tests/specs/blog/models/base/User.php @@ -9,6 +9,7 @@ * @property string $username * @property string $email * @property string $password + * @property string $role * @property int $flags * @property string $created_at * @@ -23,7 +24,7 @@ public static function tableName() public function rules() { return [ - 'trim' => [['username', 'email', 'password', 'created_at'], 'trim'], + 'trim' => [['username', 'email', 'password', 'role', 'created_at'], 'trim'], 'required' => [['username', 'email', 'password'], 'required'], 'username_unique' => [['username'], 'unique'], 'email_unique' => [['email'], 'unique'], @@ -31,6 +32,7 @@ public function rules() 'email_string' => [['email'], 'string', 'max' => 200], 'email_email' => [['email'], 'email'], 'password_string' => [['password'], 'string'], + 'role_string' => [['role'], 'string', 'max' => 20], 'flags_integer' => [['flags'], 'integer'], 'created_at_datetime' => [['created_at'], 'datetime'], ]; diff --git a/tests/specs/blog_v2.yaml b/tests/specs/blog_v2.yaml index 81076078..68d09367 100644 --- a/tests/specs/blog_v2.yaml +++ b/tests/specs/blog_v2.yaml @@ -241,7 +241,7 @@ components: x-indexes: - unique:login - unique:email - - hash:flags + - hash:role,flags required: - id - login @@ -261,6 +261,13 @@ components: password: type: string format: password + role: + type: string + enum: + - admin + - editor + - reader + x-faker: "$faker->randomElement(['admin', 'editor', 'reader'])" flags: type: integer default: 0 @@ -325,12 +332,12 @@ components: type: string minLength: 1 maxLength: 200 - # lang: - # type: string - # enum: - # - ru - # - eng - # default: ru + lang: + type: string + enum: + - ru + - eng + default: ru category: $ref: "#/components/schemas/Category" active: @@ -372,7 +379,7 @@ components: type: string meta_data: type: string - example: "type=='ticket'" + example: "type=='ticket' && status=='closed'" minLength: 1 maxLength: 300 default: '' @@ -390,7 +397,7 @@ components: required: - id - name - # - lang + - lang properties: id: type: integer @@ -400,11 +407,11 @@ components: type: string x-db-type: VARCHAR maxLength: 100 - # lang: - # type: string - # enum: - # - ru - # - eng + lang: + type: string + enum: + - ru + - eng posts: type: array items: diff --git a/tests/specs/blog_v2/migrations/m200000_000001_create_table_v2_users.php b/tests/specs/blog_v2/migrations/m200000_000001_create_table_v2_users.php index fd61f856..5dbcb200 100644 --- a/tests/specs/blog_v2/migrations/m200000_000001_create_table_v2_users.php +++ b/tests/specs/blog_v2/migrations/m200000_000001_create_table_v2_users.php @@ -12,17 +12,18 @@ public function up() 'login' => $this->text()->notNull(), 'email' => $this->text()->notNull(), 'password' => $this->string()->notNull(), + 'role' => 'enum(\'admin\', \'editor\', \'reader\') NULL DEFAULT NULL', 'flags' => $this->integer()->null()->defaultValue(0), 'created_at' => $this->timestamp()->null()->defaultValue(null), ]); $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); $this->createIndex('v2_users_email_key', '{{%v2_users}}', 'email', true); - $this->createIndex('v2_users_flags_hash_index', '{{%v2_users}}', 'flags', 'hash'); + $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', 'role,flags', 'hash'); } public function down() { - $this->dropIndex('v2_users_flags_hash_index', '{{%v2_users}}'); + $this->dropIndex('v2_users_role_flags_hash_index', '{{%v2_users}}'); $this->dropIndex('v2_users_email_key', '{{%v2_users}}'); $this->dropIndex('v2_users_login_key', '{{%v2_users}}'); $this->dropTable('{{%v2_users}}'); diff --git a/tests/specs/blog_v2/migrations/m200000_000002_create_table_v2_posts.php b/tests/specs/blog_v2/migrations/m200000_000002_create_table_v2_posts.php index cf3ca27a..ba307de6 100644 --- a/tests/specs/blog_v2/migrations/m200000_000002_create_table_v2_posts.php +++ b/tests/specs/blog_v2/migrations/m200000_000002_create_table_v2_posts.php @@ -11,6 +11,7 @@ public function up() 'id' => $this->bigPrimaryKey(), 'title' => $this->string(255)->notNull(), 'slug' => $this->string(200)->null()->defaultValue(null), + 'lang' => 'enum(\'ru\', \'eng\') NULL DEFAULT \'ru\'', 'category_id' => $this->bigInteger()->notNull(), 'active' => $this->boolean()->notNull(), 'created_at' => $this->date()->null()->defaultValue(null), diff --git a/tests/specs/blog_v2/migrations/m200000_000003_create_table_v2_tags.php b/tests/specs/blog_v2/migrations/m200000_000003_create_table_v2_tags.php index f27f11fe..32aefd34 100644 --- a/tests/specs/blog_v2/migrations/m200000_000003_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations/m200000_000003_create_table_v2_tags.php @@ -10,6 +10,7 @@ public function up() $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), 'name' => $this->string(100)->notNull(), + 'lang' => 'enum(\'ru\', \'eng\') NOT NULL', ]); $this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true); } 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 c7f6a2a3..e42208b9 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 @@ -8,6 +8,7 @@ class m200000_000000_change_table_v2_posts extends \yii\db\Migration public function up() { $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); + $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\''); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); @@ -22,6 +23,7 @@ public function down() $this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer(11)->notNull()); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()->defaultValue(0)); $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger(20)->notNull()); + $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); } } diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000001_create_table_v2_tags.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000001_create_table_v2_tags.php index 50ffc28d..1e671e96 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000001_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000001_create_table_v2_tags.php @@ -10,6 +10,7 @@ public function up() $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), 0 => 'name varchar(100) NOT NULL', + 'lang' => 'enum("ru", "eng") NOT NULL', ]); $this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true); } 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 59003105..c175f1b5 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 @@ -11,16 +11,18 @@ public function up() $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'created_at', $this->timestamp()->null()->defaultValue(null)); $this->alterColumn('{{%v2_users}}', 'email', $this->string(255)->notNull()); + $this->alterColumn('{{%v2_users}}', 'role', 'enum("admin", "editor", "reader") NULL DEFAULT NULL'); $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); - $this->createIndex('v2_users_flags_hash_index', '{{%v2_users}}', 'flags', 'hash'); + $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', 'role,flags', 'hash'); } public function down() { - $this->dropIndex('v2_users_flags_hash_index', '{{%v2_users}}'); + $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()->defaultValue("reader")); $this->alterColumn('{{%v2_users}}', 'email', $this->string(200)->notNull()); $this->alterColumn('{{%v2_users}}', 'created_at', $this->timestamp()->null()->defaultExpression("current_timestamp()")); $this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull()); 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 c7f6a2a3..e42208b9 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 @@ -8,6 +8,7 @@ class m200000_000000_change_table_v2_posts extends \yii\db\Migration public function up() { $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); + $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\''); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); @@ -22,6 +23,7 @@ public function down() $this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer(11)->notNull()); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()->defaultValue(0)); $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger(20)->notNull()); + $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); } } diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000001_create_table_v2_tags.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000001_create_table_v2_tags.php index 50ffc28d..1e671e96 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000001_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000001_create_table_v2_tags.php @@ -10,6 +10,7 @@ public function up() $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), 0 => 'name varchar(100) NOT NULL', + 'lang' => 'enum("ru", "eng") NOT NULL', ]); $this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true); } 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 db190e42..87d83448 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 @@ -11,16 +11,18 @@ public function up() $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'created_at', $this->timestamp()->null()->defaultValue(null)); $this->alterColumn('{{%v2_users}}', 'email', $this->string(255)->notNull()); + $this->alterColumn('{{%v2_users}}', 'role', 'enum("admin", "editor", "reader") NULL DEFAULT NULL'); $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); - $this->createIndex('v2_users_flags_hash_index', '{{%v2_users}}', 'flags', 'hash'); + $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', 'role,flags', 'hash'); } public function down() { - $this->dropIndex('v2_users_flags_hash_index', '{{%v2_users}}'); + $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()->defaultValue("reader")); $this->alterColumn('{{%v2_users}}', 'email', $this->string(200)->notNull()); $this->alterColumn('{{%v2_users}}', 'created_at', $this->timestamp()->null()->defaultExpression("CURRENT_TIMESTAMP")); $this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull()); 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 a03de14a..22a0029b 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 @@ -8,6 +8,8 @@ class m200000_000000_change_table_v2_posts extends \yii\db\Migration public function safeUp() { $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); + $this->execute('CREATE TYPE enum_lang AS ENUM(\'ru\', \'eng\')'); + $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()); @@ -21,7 +23,9 @@ public function safeDown() $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->integer()->null()); $this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer()->notNull()); $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull()); + $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); + $this->execute('DROP TYPE enum_lang'); $this->alterColumn('{{%v2_posts}}', 'active', "SET DEFAULT 'f'"); } } diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php index a8d8a98c..ea6d7ff8 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php @@ -7,9 +7,11 @@ class m200000_000001_create_table_v2_tags extends \yii\db\Migration { public function safeUp() { + $this->execute('CREATE TYPE enum_lang AS ENUM(\'ru\', \'eng\')'); $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), 0 => 'name varchar(100) NOT NULL', + 'lang' => 'enum_lang NOT NULL', ]); $this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true); } @@ -18,5 +20,6 @@ public function safeDown() { $this->dropIndex('v2_tags_name_key', '{{%v2_tags}}'); $this->dropTable('{{%v2_tags}}'); + $this->execute('DROP TYPE enum_lang'); } } 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 8dbf692d..7bfe1bf9 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 @@ -7,23 +7,29 @@ class m200000_000004_change_table_v2_users extends \yii\db\Migration { public function safeUp() { + $this->execute('CREATE TYPE enum_role AS ENUM(\'admin\', \'editor\', \'reader\')'); $this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull()); $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT"); $this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN email SET DATA TYPE varchar(255)')->execute(); + $this->alterColumn('{{%v2_users}}', 'role', 'enum_role USING "role"::enum_role'); + $this->alterColumn('{{%v2_users}}', 'role', "DROP DEFAULT"); $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); - $this->createIndex('v2_users_flags_hash_index', '{{%v2_users}}', 'flags', 'hash'); + $this->createIndex('v2_users_role_flags_hash_index', '{{%v2_users}}', 'role,flags', 'hash'); } public function safeDown() { - $this->dropIndex('v2_users_flags_hash_index', '{{%v2_users}}'); + $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}}', 'email', $this->string(200)->notNull()); $this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull()); $this->dropColumn('{{%v2_users}}', 'login'); $this->alterColumn('{{%v2_users}}', 'created_at', "SET DEFAULT CURRENT_TIMESTAMP"); + $this->alterColumn('{{%v2_users}}', 'role', "SET DEFAULT 'reader'"); + $this->execute('DROP TYPE enum_role'); } } diff --git a/tests/specs/blog_v2/models/PostFaker.php b/tests/specs/blog_v2/models/PostFaker.php index 6fc36c0e..a1e8eb81 100644 --- a/tests/specs/blog_v2/models/PostFaker.php +++ b/tests/specs/blog_v2/models/PostFaker.php @@ -32,6 +32,7 @@ public function generateModel($attributes = []) //$model->id = $uniqueFaker->numberBetween(0, 1000000); $model->title = substr($faker->sentence, 0, 255); $model->slug = substr($uniqueFaker->slug, 0, 200); + $model->lang = $faker->randomElement(['ru','eng']); $model->active = $faker->boolean; $model->created_at = $faker->dateTimeThisCentury->format('Y-m-d'); if (!is_callable($attributes)) { diff --git a/tests/specs/blog_v2/models/TagFaker.php b/tests/specs/blog_v2/models/TagFaker.php index 6308fa68..8e0af887 100644 --- a/tests/specs/blog_v2/models/TagFaker.php +++ b/tests/specs/blog_v2/models/TagFaker.php @@ -31,6 +31,7 @@ public function generateModel($attributes = []) $model = new Tag(); //$model->id = $uniqueFaker->numberBetween(0, 1000000); $model->name = substr($faker->text(100), 0, 100); + $model->lang = $faker->randomElement(['ru','eng']); if (!is_callable($attributes)) { $model->setAttributes($attributes, false); } else { diff --git a/tests/specs/blog_v2/models/UserFaker.php b/tests/specs/blog_v2/models/UserFaker.php index 9810c37a..9e2344f9 100644 --- a/tests/specs/blog_v2/models/UserFaker.php +++ b/tests/specs/blog_v2/models/UserFaker.php @@ -33,6 +33,7 @@ public function generateModel($attributes = []) $model->login = $faker->userName; $model->email = substr($faker->safeEmail, 0, 255); $model->password = $faker->password; + $model->role = $faker->randomElement(['admin', 'editor', 'reader']); $model->flags = $faker->numberBetween(0, 1000000); $model->created_at = $faker->dateTimeThisYear('now', 'UTC')->format(DATE_ATOM); if (!is_callable($attributes)) { diff --git a/tests/specs/blog_v2/models/base/Post.php b/tests/specs/blog_v2/models/base/Post.php index 3599eb90..aec86342 100644 --- a/tests/specs/blog_v2/models/base/Post.php +++ b/tests/specs/blog_v2/models/base/Post.php @@ -8,6 +8,7 @@ * @property int $id * @property string $title * @property string $slug + * @property string $lang * @property int $category_id Category of posts * @property bool $active * @property string $created_at @@ -28,7 +29,7 @@ public static function tableName() public function rules() { return [ - 'trim' => [['title', 'slug', 'created_at'], 'trim'], + 'trim' => [['title', 'slug', 'lang', 'created_at'], 'trim'], 'required' => [['title', 'category_id', 'active'], 'required'], 'category_id_integer' => [['category_id'], 'integer'], 'category_id_exist' => [['category_id'], 'exist', 'targetRelation' => 'Category'], @@ -37,6 +38,8 @@ public function rules() 'title_unique' => [['title'], 'unique'], 'title_string' => [['title'], 'string', 'max' => 255], 'slug_string' => [['slug'], 'string', 'min' => 1, 'max' => 200], + 'lang_string' => [['lang'], 'string'], + 'lang_in' => [['lang'], 'in', 'range' => ['ru', 'eng']], 'active_boolean' => [['active'], 'boolean'], 'created_at_date' => [['created_at'], 'date'], ]; diff --git a/tests/specs/blog_v2/models/base/Tag.php b/tests/specs/blog_v2/models/base/Tag.php index 761507a8..2d0e7d6c 100644 --- a/tests/specs/blog_v2/models/base/Tag.php +++ b/tests/specs/blog_v2/models/base/Tag.php @@ -7,6 +7,7 @@ * * @property int $id * @property string $name + * @property string $lang * * @property array|\app\models\Post[] $posts */ @@ -20,10 +21,12 @@ public static function tableName() public function rules() { return [ - 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], + 'trim' => [['name', 'lang'], 'trim'], + 'required' => [['name', 'lang'], 'required'], 'name_unique' => [['name'], 'unique'], 'name_string' => [['name'], 'string', 'max' => 100], + 'lang_string' => [['lang'], 'string'], + 'lang_in' => [['lang'], 'in', 'range' => ['ru', 'eng']], ]; } diff --git a/tests/specs/blog_v2/models/base/User.php b/tests/specs/blog_v2/models/base/User.php index 3470798f..3ac6d337 100644 --- a/tests/specs/blog_v2/models/base/User.php +++ b/tests/specs/blog_v2/models/base/User.php @@ -9,6 +9,7 @@ * @property string $login * @property string $email * @property string $password + * @property string $role * @property int $flags * @property string $created_at * @@ -23,7 +24,7 @@ public static function tableName() public function rules() { return [ - 'trim' => [['login', 'email', 'password', 'created_at'], 'trim'], + 'trim' => [['login', 'email', 'password', 'role', 'created_at'], 'trim'], 'required' => [['login', 'email', 'password'], 'required'], 'login_unique' => [['login'], 'unique'], 'email_unique' => [['email'], 'unique'], @@ -31,6 +32,8 @@ public function rules() 'email_string' => [['email'], 'string', 'max' => 255], 'email_email' => [['email'], 'email'], 'password_string' => [['password'], 'string'], + 'role_string' => [['role'], 'string'], + 'role_in' => [['role'], 'in', 'range' => ['admin', 'editor', 'reader']], 'flags_integer' => [['flags'], 'integer'], 'created_at_datetime' => [['created_at'], 'datetime'], ]; diff --git a/tests/specs/postgres_custom.yaml b/tests/specs/postgres_custom.yaml index 68c67ae1..519af1ab 100644 --- a/tests/specs/postgres_custom.yaml +++ b/tests/specs/postgres_custom.yaml @@ -35,7 +35,7 @@ components: Custom: x-table: v3_pgcustom x-indexes: - - "gin(to_tsvector('english')):search" + - "gin(to_tsvector('english', status)):search" required: - id properties: @@ -64,12 +64,12 @@ components: type: array x-db-type: jsonb default: '{"foo": "bar", "bar": "baz"}' - # status: - # type: string - # default: draft - # enum: - # - active - # - draft + status: + type: string + default: draft + enum: + - active + - draft search: type: string x-db-type: tsvector diff --git a/tests/specs/postgres_custom/migrations/m200000_000000_create_table_v3_pgcustom.php b/tests/specs/postgres_custom/migrations/m200000_000000_create_table_v3_pgcustom.php index 6855df2a..fbf76227 100644 --- a/tests/specs/postgres_custom/migrations/m200000_000000_create_table_v3_pgcustom.php +++ b/tests/specs/postgres_custom/migrations/m200000_000000_create_table_v3_pgcustom.php @@ -14,9 +14,10 @@ public function up() 'json2' => 'json NOT NULL DEFAULT \'[]\'', 'json3' => 'json NOT NULL DEFAULT \'[{"foo":"foobar"},{"xxx":"yyy"}]\'', 'json4' => 'json NOT NULL DEFAULT \'{"foo":"bar","bar":"baz"}\'', + 'status' => 'enum(\'active\', \'draft\') NULL DEFAULT \'draft\'', 'search' => 'tsvector NULL', ]); - $this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'search', 'gin(to_tsvector(\'english\'))'); + $this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'search', 'gin(to_tsvector(\'english\', status))'); } public function down() 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 f0608474..c04adc88 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 @@ -15,7 +15,8 @@ public function safeUp() $this->alterColumn('{{%v3_pgcustom}}', 'json3', "SET DEFAULT '[{\"foo\":\"foobar\"},{\"xxx\":\"yyy\"}]'"); $this->alterColumn('{{%v3_pgcustom}}', 'json4', "SET NOT NULL"); $this->alterColumn('{{%v3_pgcustom}}', 'json4', "SET DEFAULT '{\"foo\":\"bar\",\"bar\":\"baz\"}'"); - $this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'search', 'gin(to_tsvector(\'english\'))'); + $this->alterColumn('{{%v3_pgcustom}}', 'status', "SET DEFAULT 'draft'"); + $this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'search', 'gin(to_tsvector(\'english\', status))'); } public function safeDown() @@ -29,5 +30,6 @@ public function safeDown() $this->alterColumn('{{%v3_pgcustom}}', 'json3', "SET DEFAULT '{\"bar\":\"baz\",\"foo\":\"bar\"}'"); $this->alterColumn('{{%v3_pgcustom}}', 'json4', "DROP NOT NULL"); $this->alterColumn('{{%v3_pgcustom}}', 'json4', "SET DEFAULT '{\"ffo\":\"bar\"}'"); + $this->alterColumn('{{%v3_pgcustom}}', 'status', "DROP DEFAULT"); } } diff --git a/tests/specs/postgres_custom/models/CustomFaker.php b/tests/specs/postgres_custom/models/CustomFaker.php index 373217df..6c7de598 100644 --- a/tests/specs/postgres_custom/models/CustomFaker.php +++ b/tests/specs/postgres_custom/models/CustomFaker.php @@ -35,6 +35,7 @@ public function generateModel($attributes = []) $model->json2 = []; $model->json3 = []; $model->json4 = []; + $model->status = $faker->randomElement(['active','draft']); if (!is_callable($attributes)) { $model->setAttributes($attributes, false); } else { diff --git a/tests/specs/postgres_custom/models/base/Custom.php b/tests/specs/postgres_custom/models/base/Custom.php index 9190b60a..e19f3700 100644 --- a/tests/specs/postgres_custom/models/base/Custom.php +++ b/tests/specs/postgres_custom/models/base/Custom.php @@ -11,6 +11,7 @@ * @property array $json2 * @property array $json3 * @property array $json4 + * @property string $status * @property string $search * */ @@ -24,7 +25,10 @@ public static function tableName() public function rules() { return [ + 'trim' => [['status'], 'trim'], 'num_integer' => [['num'], 'integer'], + 'status_string' => [['status'], 'string'], + 'status_in' => [['status'], 'in', 'range' => ['active', 'draft']], 'safe' => [['json1', 'json2', 'json3', 'json4'], 'safe'], ]; } diff --git a/tests/unit/PropertySchemaTest.php b/tests/unit/PropertySchemaTest.php index 417d91fd..68b6e07d 100644 --- a/tests/unit/PropertySchemaTest.php +++ b/tests/unit/PropertySchemaTest.php @@ -26,6 +26,7 @@ public function testPkProperty() self::assertEquals(128, $prop->getMaxLength()); self::assertEquals(null, $prop->getMinLength()); self::assertEquals(true, $prop->isReadonly()); + self::assertFalse($prop->hasEnum()); } public function testSimpleProperty() diff --git a/tests/unit/ValidatorRulesBuilderTest.php b/tests/unit/ValidatorRulesBuilderTest.php index 86e4d832..dbad43b9 100644 --- a/tests/unit/ValidatorRulesBuilderTest.php +++ b/tests/unit/ValidatorRulesBuilderTest.php @@ -28,6 +28,7 @@ public function testBuild() (new Attribute('active'))->setPhpType('bool')->setDbType('boolean'), (new Attribute('category'))->asReference('Category') ->setRequired(true)->setPhpType('int')->setDbType('integer'), + (new Attribute('state'))->setPhpType('string')->setDbType('string')->setEnumValues(['active', 'draft']), (new Attribute('created_at'))->setPhpType('string')->setDbType('datetime'), (new Attribute('contact_email'))->setPhpType('string')->setDbType('string'), (new Attribute('required_with_def'))->setPhpType('string') @@ -41,6 +42,7 @@ public function testBuild() 'trim' => new ValidationRule([ 'title', 'article', + 'state', 'created_at', 'contact_email', 'required_with_def', @@ -55,6 +57,8 @@ public function testBuild() 'title_string' => new ValidationRule(['title'], 'string', ['max' => 60]), 'article_string' => new ValidationRule(['article'], 'string'), 'active_boolean' => new ValidationRule(['active'], 'boolean'), + 'state_string' => new ValidationRule(['state'], 'string'), + 'state_in' => new ValidationRule(['state'], 'in', ['range' => ['active', 'draft']]), 'created_at_datetime' => new ValidationRule(['created_at'], 'datetime'), 'contact_email_string' => new ValidationRule(['contact_email'], 'string'), 'contact_email_email' => new ValidationRule(['contact_email'], 'email'), From 72c2a795a7166a02ce8e84cc22f2d1c8a598a806 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 27 Dec 2022 20:48:32 +0530 Subject: [PATCH 25/62] Add enh. --- Makefile | 1 + README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 718652a8..9a7e0370 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,7 @@ 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 docker-compose exec -T postgres bash -c "psql --username dbuser --dbname testdb -c 'create database pg_test_db_2;'; psql --username dbuser --dbname testdb -c 'grant all privileges on database pg_test_db_2 to dbuser;'" || (docker-compose ps; docker-compose logs; exit 1) diff --git a/README.md b/README.md index e1a60f2d..9d20b16a 100644 --- a/README.md +++ b/README.md @@ -329,7 +329,7 @@ It works on all 3 DB: MySQL, MariaDb and PgSQL. - three ``` -Note: Change in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should be handles manually. +Note: Change in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should be handled manually. ### Handling of `numeric` (#numeric, #MariaDb) precision-default = 10 From c9322ad848c777176d06cb6f22f3cd97185025ed Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 28 Dec 2022 13:25:04 +0530 Subject: [PATCH 26/62] test --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9d20b16a..35c5f5dd 100644 --- a/README.md +++ b/README.md @@ -409,3 +409,4 @@ https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). +test From a8d3b615bd7a28f668125b31b9d3f9b9c24fd23a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 28 Dec 2022 13:28:03 +0530 Subject: [PATCH 27/62] Undo test --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 35c5f5dd..107df33f 100644 --- a/README.md +++ b/README.md @@ -408,5 +408,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - -test From c0563e7a71fd5eabb27c1e95b96445ab0d032e86 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 28 Dec 2022 16:03:47 +0530 Subject: [PATCH 28/62] Fix issue: defaultValue change detection for expression was not working properly --- src/lib/migrations/BaseMigrationBuilder.php | 21 ++++++++++++++++++- src/lib/migrations/MysqlMigrationBuilder.php | 10 +++++++-- .../migrations/PostgresMigrationBuilder.php | 10 +++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 072628ad..6a88fb48 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -15,7 +15,7 @@ use Yii; use yii\db\ColumnSchema; use yii\helpers\VarDumper; -use yii\db\Connection; +use yii\db\{Connection, Expression}; abstract class BaseMigrationBuilder { @@ -477,4 +477,23 @@ public static function isEnumValuesChanged( } return false; } + + public function isDefaultValueChanged(ColumnSchema $current, + ColumnSchema $desired): bool + { + // if the default value is object of \yii\db\Expression then default value is expression instead of constant. See https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html + // in such case instead of comparing two objects, we should compare expression + + if ($current->defaultValue instanceof Expression && + $desired->defaultValue instanceof Expression + && $current->defaultValue->expression === $desired->defaultValue->expression + ) { + return false; + } + + if ($current->defaultValue !== $desired->defaultValue) { + return true; + } + return false; + } } diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index c9f13c32..9b899354 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -53,8 +53,14 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): , 'dbType', 'phpType' , 'precision', 'scale', 'unsigned' ] as $attr) { - if ($current->$attr !== $desiredFromDb->$attr) { - $changedAttributes[] = $attr; + if ($attr === 'defaultValue') { + if ($this->isDefaultValueChanged($current, $desiredFromDb)) { + $changedAttributes[] = $attr; + } + } else { + if ($current->$attr !== $desiredFromDb->$attr) { + $changedAttributes[] = $attr; + } } } return $changedAttributes; diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index c1e109f6..f9627cf6 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -124,8 +124,14 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): , 'dbType', 'phpType' , 'precision', 'scale', 'unsigned' ] as $attr) { - if ($current->$attr !== $desiredFromDb->$attr) { - $changedAttributes[] = $attr; + if ($attr === 'defaultValue') { + if ($this->isDefaultValueChanged($current, $desiredFromDb)) { + $changedAttributes[] = $attr; + } + } else { + if ($current->$attr !== $desiredFromDb->$attr) { + $changedAttributes[] = $attr; + } } } return $changedAttributes; From 20e960a7fa7d362029c5bff30b2d5ee794901bc9 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 29 Dec 2022 19:07:15 +0530 Subject: [PATCH 29/62] test to trigger CI --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 107df33f..9d20b16a 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From 4066e9333eb6060e926d96095fac0fbaaed05c53 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 29 Dec 2022 19:09:16 +0530 Subject: [PATCH 30/62] test to trigger CI 2 --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 9d20b16a..107df33f 100644 --- a/README.md +++ b/README.md @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - From ca7232206a57437862b5e6df47c9db3b7b1008ff Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 29 Dec 2022 19:10:51 +0530 Subject: [PATCH 31/62] test to trigger CI 3 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 728e0d95..f8f7e07d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,7 +1,7 @@ name: yii2-openapi on: push: - branches: [ master, wip] + branches: [ master, wip, 104-remove-id-from-rules-method-from-base-model-during-code-generation] pull_request: branches: [ master, wip ] paths-ignore: From 672ae45689776da278ac8a00d1f58f3684ccad10 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 29 Dec 2022 19:31:38 +0530 Subject: [PATCH 32/62] test to trigger CI 4 --- .github/workflows/test.yml | 2 +- src/lib/migrations/BaseMigrationBuilder.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f8f7e07d..728e0d95 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,7 +1,7 @@ name: yii2-openapi on: push: - branches: [ master, wip, 104-remove-id-from-rules-method-from-base-model-during-code-generation] + branches: [ master, wip] pull_request: branches: [ master, wip ] paths-ignore: diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 6a88fb48..cbaec3c7 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -497,3 +497,4 @@ public function isDefaultValueChanged(ColumnSchema $current, return false; } } + From 7da7317a5be1d1e9b28d0702c927d6f4fe165d7b Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 29 Dec 2022 19:41:48 +0530 Subject: [PATCH 33/62] Fix style --- src/lib/migrations/BaseMigrationBuilder.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index cbaec3c7..4b88a6e6 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -15,7 +15,8 @@ use Yii; use yii\db\ColumnSchema; use yii\helpers\VarDumper; -use yii\db\{Connection, Expression}; +use yii\db\Connection; +use yii\db\Expression; abstract class BaseMigrationBuilder { @@ -478,8 +479,10 @@ public static function isEnumValuesChanged( return false; } - public function isDefaultValueChanged(ColumnSchema $current, - ColumnSchema $desired): bool + public function isDefaultValueChanged( + ColumnSchema $current, + ColumnSchema $desired + ): bool { // if the default value is object of \yii\db\Expression then default value is expression instead of constant. See https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html // in such case instead of comparing two objects, we should compare expression @@ -497,4 +500,3 @@ public function isDefaultValueChanged(ColumnSchema $current, return false; } } - From 53d4de4ab3fb34bd0b62906a60d4daedb992a933 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 29 Dec 2022 19:50:27 +0530 Subject: [PATCH 34/62] test to trigger CI 5 --- src/lib/migrations/BaseMigrationBuilder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 4b88a6e6..053ecde8 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -500,3 +500,4 @@ public function isDefaultValueChanged( return false; } } + From 0041f7b7418b5f1283fde5fd206dda218812caf4 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 29 Dec 2022 19:56:16 +0530 Subject: [PATCH 35/62] Fix style --- src/lib/migrations/BaseMigrationBuilder.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 053ecde8..ab49d0bf 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -482,8 +482,7 @@ public static function isEnumValuesChanged( public function isDefaultValueChanged( ColumnSchema $current, ColumnSchema $desired - ): bool - { + ): bool { // if the default value is object of \yii\db\Expression then default value is expression instead of constant. See https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html // in such case instead of comparing two objects, we should compare expression @@ -500,4 +499,3 @@ public function isDefaultValueChanged( return false; } } - From a2d0c8047693b8a4df7341bfe8be58868ad32ca8 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 16:03:35 +0530 Subject: [PATCH 36/62] Fix https://github.com/cebe/yii2-openapi/issues/104 --- 1 | 10 ++ src/lib/ValidationRulesBuilder.php | 8 + tests/DbTestCase.php | 2 +- .../app/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../id_not_in_rules/app/models/Fruit.php | 10 ++ .../id_not_in_rules/app/models/FruitFaker.php | 41 +++++ .../id_not_in_rules/app/models/base/Fruit.php | 27 ++++ .../specs/id_not_in_rules/id_not_in_rules.php | 13 ++ .../id_not_in_rules/id_not_in_rules.yaml | 26 ++++ tests/unit/IdNotInRulesTest.php | 23 +++ tests/unit/MultiDbFreshMigrationTest.php | 2 +- tests/unit/MultiDbSecondaryMigrationTest.php | 2 +- 12 files changed, 305 insertions(+), 3 deletions(-) create mode 100644 1 create mode 100644 tests/specs/id_not_in_rules/app/models/BaseModelFaker.php create mode 100644 tests/specs/id_not_in_rules/app/models/Fruit.php create mode 100644 tests/specs/id_not_in_rules/app/models/FruitFaker.php create mode 100644 tests/specs/id_not_in_rules/app/models/base/Fruit.php create mode 100644 tests/specs/id_not_in_rules/id_not_in_rules.php create mode 100644 tests/specs/id_not_in_rules/id_not_in_rules.yaml create mode 100644 tests/unit/IdNotInRulesTest.php diff --git a/1 b/1 new file mode 100644 index 00000000..e223ee45 --- /dev/null +++ b/1 @@ -0,0 +1,10 @@ +[241] Log opened at 2022-12-30 10:33:14 +[241] I: Checking remote connect back address. +[241] I: Checking header 'HTTP_X_FORWARDED_FOR'. +[241] I: Checking header 'REMOTE_ADDR'. +[241] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| +[241] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). +[241] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. +[241] E: Could not connect to client. :-( +[241] Log closed at 2022-12-30 10:33:14 + diff --git a/src/lib/ValidationRulesBuilder.php b/src/lib/ValidationRulesBuilder.php index 746976b0..e696fc21 100644 --- a/src/lib/ValidationRulesBuilder.php +++ b/src/lib/ValidationRulesBuilder.php @@ -63,6 +63,10 @@ public function build():array } } foreach ($this->model->attributes as $attribute) { + // column/field/property with name `id` is considered as Primary Key by this library and it is automatically handled by DB/Yii; so remove it from validation `rules()` + if ($attribute->columnName === 'id' || $attribute->propertyName === 'id') { + continue; + } $this->resolveAttributeRules($attribute); } @@ -181,6 +185,10 @@ private function prepareTypeScope():void if ($attribute->isReadOnly()) { continue; } + // column/field/property with name `id` is considered as Primary Key by this library and it is automatically handled by DB/Yii; so remove it from validation `rules()` + if ($attribute->columnName === 'id' || $attribute->propertyName === 'id') { + continue; + } if ($attribute->defaultValue === null && $attribute->isRequired()) { $this->typeScope['required'][$attribute->columnName] = $attribute->columnName; } diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index 1013cea0..bb77194c 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -53,7 +53,7 @@ protected function tearDown() } } - protected function runGenerator($configFile, string $dbName) + protected function runGenerator($configFile, string $dbName = 'mysql') { $config = require $configFile; $config['migrationPath'] = "@app/migrations_{$dbName}_db/"; diff --git a/tests/specs/id_not_in_rules/app/models/BaseModelFaker.php b/tests/specs/id_not_in_rules/app/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/id_not_in_rules/app/models/BaseModelFaker.php @@ -0,0 +1,144 @@ +faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language)); + $this->uniqueFaker = new UniqueGenerator($this->faker); + } + + abstract public function generateModel($attributes = []); + + public function getFaker():Generator + { + return $this->faker; + } + + public function getUniqueFaker():UniqueGenerator + { + return $this->uniqueFaker; + } + + public function setFaker(Generator $faker):void + { + $this->faker = $faker; + } + + public function setUniqueFaker(UniqueGenerator $faker):void + { + $this->uniqueFaker = $faker; + } + + /** + * Generate and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::makeOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + $model = $fakeBuilder->generateModel($attributes); + return $model; + } + + /** + * Generate, save and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::saveOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $model = static::makeOne($attributes, $uniqueFaker); + $model->save(); + return $model; + } + + /** + * Generate and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + return $model; + }, range(0, $number -1)); + } + + /** + * Generate, save and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + $model->save(); + return $model; + }, range(0, $number -1)); + } +} diff --git a/tests/specs/id_not_in_rules/app/models/Fruit.php b/tests/specs/id_not_in_rules/app/models/Fruit.php new file mode 100644 index 00000000..c74c53d9 --- /dev/null +++ b/tests/specs/id_not_in_rules/app/models/Fruit.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new Fruit(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/id_not_in_rules/app/models/base/Fruit.php b/tests/specs/id_not_in_rules/app/models/base/Fruit.php new file mode 100644 index 00000000..4b1fcd72 --- /dev/null +++ b/tests/specs/id_not_in_rules/app/models/base/Fruit.php @@ -0,0 +1,27 @@ + [['name'], 'trim'], + 'required' => [['name'], 'required'], + 'name_string' => [['name'], 'string'], + ]; + } +} diff --git a/tests/specs/id_not_in_rules/id_not_in_rules.php b/tests/specs/id_not_in_rules/id_not_in_rules.php new file mode 100644 index 00000000..73a9ccc7 --- /dev/null +++ b/tests/specs/id_not_in_rules/id_not_in_rules.php @@ -0,0 +1,13 @@ + '@specs/id_not_in_rules/id_not_in_rules.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => false, + 'generateModelFaker' => true, +]; diff --git a/tests/specs/id_not_in_rules/id_not_in_rules.yaml b/tests/specs/id_not_in_rules/id_not_in_rules.yaml new file mode 100644 index 00000000..0d684370 --- /dev/null +++ b/tests/specs/id_not_in_rules/id_not_in_rules.yaml @@ -0,0 +1,26 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: ID not in rules test +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Fruit: + type: object + description: Test model for model code generation that should not contain id column in rules + required: + - id + - name + properties: + id: + type: integer + name: + type: string diff --git a/tests/unit/IdNotInRulesTest.php b/tests/unit/IdNotInRulesTest.php new file mode 100644 index 00000000..021b3ac6 --- /dev/null +++ b/tests/unit/IdNotInRulesTest.php @@ -0,0 +1,23 @@ +runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/id_not_in_rules/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } +} diff --git a/tests/unit/MultiDbFreshMigrationTest.php b/tests/unit/MultiDbFreshMigrationTest.php index 0a86726b..15ade7e2 100644 --- a/tests/unit/MultiDbFreshMigrationTest.php +++ b/tests/unit/MultiDbFreshMigrationTest.php @@ -71,7 +71,7 @@ protected function tearDown() } } - protected function runGenerator($configFile, string $dbName) + protected function runGenerator($configFile, string $dbName = 'mysql') { $config = require $configFile; $config['migrationPath'] = "@app/migrations_{$dbName}_db/"; diff --git a/tests/unit/MultiDbSecondaryMigrationTest.php b/tests/unit/MultiDbSecondaryMigrationTest.php index 5680cc75..2ae0b662 100644 --- a/tests/unit/MultiDbSecondaryMigrationTest.php +++ b/tests/unit/MultiDbSecondaryMigrationTest.php @@ -85,7 +85,7 @@ protected function tearDown() } } - protected function runGenerator($configFile, string $dbName) + protected function runGenerator($configFile, string $dbName = 'mysql') { $config = require $configFile; $config['migrationPath'] = "@app/migrations_{$dbName}_db/"; From 1f1e71f92c3fa90ffb01e5e7e352b0c471305888 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 16:05:40 +0530 Subject: [PATCH 37/62] remove unwanted files --- 1 | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 1 diff --git a/1 b/1 deleted file mode 100644 index e223ee45..00000000 --- a/1 +++ /dev/null @@ -1,10 +0,0 @@ -[241] Log opened at 2022-12-30 10:33:14 -[241] I: Checking remote connect back address. -[241] I: Checking header 'HTTP_X_FORWARDED_FOR'. -[241] I: Checking header 'REMOTE_ADDR'. -[241] W: Remote address not found, connecting to configured address/port: localhost:9000. :-| -[241] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29). -[241] W: Creating socket for 'localhost:9000', connect: Cannot assign requested address. -[241] E: Could not connect to client. :-( -[241] Log closed at 2022-12-30 10:33:14 - From 8c074a97395887987b2603e710cb475aeccc564f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 16:27:16 +0530 Subject: [PATCH 38/62] Fix https://github.com/cebe/yii2-openapi/issues/115 --- .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + tests/specs/x_db_type/fresh/mysql/x_db_type_mysql.yaml | 5 +++++ .../m200000_000003_create_table_pristines.php | 1 + tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml | 5 +++++ .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + 11 files changed, 19 insertions(+) diff --git a/tests/specs/x_db_type/edit_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/edit_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php index 4d1b1590..6e7ab8a7 100644 --- a/tests/specs/x_db_type/edit_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/edit_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function up() 7 => 'col_9 varchar(9) NULL DEFAULT NULL', 8 => 'col_10 varchar(10) NULL DEFAULT NULL', 9 => 'col_11 text NULL DEFAULT NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/edit_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/edit_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php index aeed5fe1..3e9e8e02 100644 --- a/tests/specs/x_db_type/edit_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/edit_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function up() 7 => 'col_9 varchar(9) NULL DEFAULT NULL', 8 => 'col_10 varchar(10) NULL DEFAULT NULL', 9 => 'col_11 text NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php index e4090b6d..ab2c1eab 100644 --- a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function safeUp() 7 => 'col_9 varchar NULL DEFAULT NULL', 8 => 'col_10 varchar NULL DEFAULT NULL', 9 => 'col_11 text NULL DEFAULT NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/fresh/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/fresh/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php index 4d1b1590..6e7ab8a7 100644 --- a/tests/specs/x_db_type/fresh/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/fresh/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function up() 7 => 'col_9 varchar(9) NULL DEFAULT NULL', 8 => 'col_10 varchar(10) NULL DEFAULT NULL', 9 => 'col_11 text NULL DEFAULT NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/fresh/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/fresh/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php index aeed5fe1..3e9e8e02 100644 --- a/tests/specs/x_db_type/fresh/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/fresh/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function up() 7 => 'col_9 varchar(9) NULL DEFAULT NULL', 8 => 'col_10 varchar(10) NULL DEFAULT NULL', 9 => 'col_11 text NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/fresh/mysql/x_db_type_mysql.yaml b/tests/specs/x_db_type/fresh/mysql/x_db_type_mysql.yaml index a055caad..2a706468 100644 --- a/tests/specs/x_db_type/fresh/mysql/x_db_type_mysql.yaml +++ b/tests/specs/x_db_type/fresh/mysql/x_db_type_mysql.yaml @@ -54,6 +54,11 @@ components: col_11: type: string x-db-type: TEXT + price: + description: price in EUR + type: number + x-db-type: decimal(10,2) + default: 0 Alldbdatatype: # All DB data type diff --git a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php index e4090b6d..ab2c1eab 100644 --- a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function safeUp() 7 => 'col_9 varchar NULL DEFAULT NULL', 8 => 'col_10 varchar NULL DEFAULT NULL', 9 => 'col_11 text NULL DEFAULT NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml b/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml index 692a9831..ff629112 100644 --- a/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml +++ b/tests/specs/x_db_type/fresh/pgsql/x_db_type_pgsql.yaml @@ -54,6 +54,11 @@ components: col_11: type: string x-db-type: TEXT + price: + description: price in EUR + type: number + x-db-type: decimal(10,2) + default: 0 Alldbdatatype: # All DB data type diff --git a/tests/specs/x_db_type/new_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/new_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php index 4d1b1590..6e7ab8a7 100644 --- a/tests/specs/x_db_type/new_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/new_column/maria/app/migrations_maria_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function up() 7 => 'col_9 varchar(9) NULL DEFAULT NULL', 8 => 'col_10 varchar(10) NULL DEFAULT NULL', 9 => 'col_11 text NULL DEFAULT NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/new_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/new_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php index aeed5fe1..3e9e8e02 100644 --- a/tests/specs/x_db_type/new_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/new_column/mysql/app/migrations_mysql_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function up() 7 => 'col_9 varchar(9) NULL DEFAULT NULL', 8 => 'col_10 varchar(10) NULL DEFAULT NULL', 9 => 'col_11 text NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php index e4090b6d..ab2c1eab 100644 --- a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php @@ -19,6 +19,7 @@ public function safeUp() 7 => 'col_9 varchar NULL DEFAULT NULL', 8 => 'col_10 varchar NULL DEFAULT NULL', 9 => 'col_11 text NULL DEFAULT NULL', + 10 => 'price decimal(10,2) NULL DEFAULT 0', ]); } From 91273e8aff4a7465f899912b55c851579614c189 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 16:40:02 +0530 Subject: [PATCH 39/62] test --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 107df33f..9d20b16a 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From 0ac648d2649a5d5a11997c194d712b02252a1509 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 17:19:12 +0530 Subject: [PATCH 40/62] Add tests for this issue --- ...00000_change_table_column_name_changes.php | 19 +++ .../maria/app/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../maria/app/models/ColumnNameChange.php | 10 ++ .../app/models/ColumnNameChangeFaker.php | 42 +++++ .../app/models/base/ColumnNameChange.php | 28 ++++ ...00000_change_table_column_name_changes.php | 19 +++ .../mysql/app/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../mysql/app/models/ColumnNameChange.php | 10 ++ .../app/models/ColumnNameChangeFaker.php | 42 +++++ .../app/models/base/ColumnNameChange.php | 28 ++++ .../mysql/change_column_name.php | 13 ++ .../mysql/change_column_name.yaml | 33 ++++ ...00000_change_table_column_name_changes.php | 19 +++ .../pgsql/app/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../pgsql/app/models/ColumnNameChange.php | 10 ++ .../app/models/ColumnNameChangeFaker.php | 42 +++++ .../app/models/base/ColumnNameChange.php | 28 ++++ .../pgsql/change_column_name.php | 13 ++ .../pgsql/change_column_name.yaml | 33 ++++ tests/unit/ChangeColumnNameTest.php | 71 +++++++++ 20 files changed, 892 insertions(+) create mode 100644 tests/specs/change_column_name/maria/app/migrations_maria_db/m200000_000000_change_table_column_name_changes.php create mode 100644 tests/specs/change_column_name/maria/app/models/BaseModelFaker.php create mode 100644 tests/specs/change_column_name/maria/app/models/ColumnNameChange.php create mode 100644 tests/specs/change_column_name/maria/app/models/ColumnNameChangeFaker.php create mode 100644 tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php create mode 100644 tests/specs/change_column_name/mysql/app/migrations_mysql_db/m200000_000000_change_table_column_name_changes.php create mode 100644 tests/specs/change_column_name/mysql/app/models/BaseModelFaker.php create mode 100644 tests/specs/change_column_name/mysql/app/models/ColumnNameChange.php create mode 100644 tests/specs/change_column_name/mysql/app/models/ColumnNameChangeFaker.php create mode 100644 tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php create mode 100644 tests/specs/change_column_name/mysql/change_column_name.php create mode 100644 tests/specs/change_column_name/mysql/change_column_name.yaml create mode 100644 tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php create mode 100644 tests/specs/change_column_name/pgsql/app/models/BaseModelFaker.php create mode 100644 tests/specs/change_column_name/pgsql/app/models/ColumnNameChange.php create mode 100644 tests/specs/change_column_name/pgsql/app/models/ColumnNameChangeFaker.php create mode 100644 tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php create mode 100644 tests/specs/change_column_name/pgsql/change_column_name.php create mode 100644 tests/specs/change_column_name/pgsql/change_column_name.yaml create mode 100644 tests/unit/ChangeColumnNameTest.php diff --git a/tests/specs/change_column_name/maria/app/migrations_maria_db/m200000_000000_change_table_column_name_changes.php b/tests/specs/change_column_name/maria/app/migrations_maria_db/m200000_000000_change_table_column_name_changes.php new file mode 100644 index 00000000..1a75dbd3 --- /dev/null +++ b/tests/specs/change_column_name/maria/app/migrations_maria_db/m200000_000000_change_table_column_name_changes.php @@ -0,0 +1,19 @@ +db->createCommand('ALTER TABLE {{%column_name_changes}} ADD COLUMN updated_at_2 datetime NOT NULL')->execute(); + $this->dropColumn('{{%column_name_changes}}', 'updated_at'); + } + + public function down() + { + $this->addColumn('{{%column_name_changes}}', 'updated_at', $this->datetime()->notNull()); + $this->dropColumn('{{%column_name_changes}}', 'updated_at_2'); + } +} diff --git a/tests/specs/change_column_name/maria/app/models/BaseModelFaker.php b/tests/specs/change_column_name/maria/app/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/change_column_name/maria/app/models/BaseModelFaker.php @@ -0,0 +1,144 @@ +faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language)); + $this->uniqueFaker = new UniqueGenerator($this->faker); + } + + abstract public function generateModel($attributes = []); + + public function getFaker():Generator + { + return $this->faker; + } + + public function getUniqueFaker():UniqueGenerator + { + return $this->uniqueFaker; + } + + public function setFaker(Generator $faker):void + { + $this->faker = $faker; + } + + public function setUniqueFaker(UniqueGenerator $faker):void + { + $this->uniqueFaker = $faker; + } + + /** + * Generate and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::makeOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + $model = $fakeBuilder->generateModel($attributes); + return $model; + } + + /** + * Generate, save and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::saveOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $model = static::makeOne($attributes, $uniqueFaker); + $model->save(); + return $model; + } + + /** + * Generate and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + return $model; + }, range(0, $number -1)); + } + + /** + * Generate, save and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + $model->save(); + return $model; + }, range(0, $number -1)); + } +} diff --git a/tests/specs/change_column_name/maria/app/models/ColumnNameChange.php b/tests/specs/change_column_name/maria/app/models/ColumnNameChange.php new file mode 100644 index 00000000..a6fd1c91 --- /dev/null +++ b/tests/specs/change_column_name/maria/app/models/ColumnNameChange.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new ColumnNameChange(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->sentence; + $model->updated_at_2 = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php b/tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php new file mode 100644 index 00000000..2289ab14 --- /dev/null +++ b/tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php @@ -0,0 +1,28 @@ + [['name'], 'trim'], + 'required' => [['name'], 'required'], + 'name_string' => [['name'], 'string'], + ]; + } +} diff --git a/tests/specs/change_column_name/mysql/app/migrations_mysql_db/m200000_000000_change_table_column_name_changes.php b/tests/specs/change_column_name/mysql/app/migrations_mysql_db/m200000_000000_change_table_column_name_changes.php new file mode 100644 index 00000000..1a75dbd3 --- /dev/null +++ b/tests/specs/change_column_name/mysql/app/migrations_mysql_db/m200000_000000_change_table_column_name_changes.php @@ -0,0 +1,19 @@ +db->createCommand('ALTER TABLE {{%column_name_changes}} ADD COLUMN updated_at_2 datetime NOT NULL')->execute(); + $this->dropColumn('{{%column_name_changes}}', 'updated_at'); + } + + public function down() + { + $this->addColumn('{{%column_name_changes}}', 'updated_at', $this->datetime()->notNull()); + $this->dropColumn('{{%column_name_changes}}', 'updated_at_2'); + } +} diff --git a/tests/specs/change_column_name/mysql/app/models/BaseModelFaker.php b/tests/specs/change_column_name/mysql/app/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/change_column_name/mysql/app/models/BaseModelFaker.php @@ -0,0 +1,144 @@ +faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language)); + $this->uniqueFaker = new UniqueGenerator($this->faker); + } + + abstract public function generateModel($attributes = []); + + public function getFaker():Generator + { + return $this->faker; + } + + public function getUniqueFaker():UniqueGenerator + { + return $this->uniqueFaker; + } + + public function setFaker(Generator $faker):void + { + $this->faker = $faker; + } + + public function setUniqueFaker(UniqueGenerator $faker):void + { + $this->uniqueFaker = $faker; + } + + /** + * Generate and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::makeOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + $model = $fakeBuilder->generateModel($attributes); + return $model; + } + + /** + * Generate, save and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::saveOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $model = static::makeOne($attributes, $uniqueFaker); + $model->save(); + return $model; + } + + /** + * Generate and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + return $model; + }, range(0, $number -1)); + } + + /** + * Generate, save and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + $model->save(); + return $model; + }, range(0, $number -1)); + } +} diff --git a/tests/specs/change_column_name/mysql/app/models/ColumnNameChange.php b/tests/specs/change_column_name/mysql/app/models/ColumnNameChange.php new file mode 100644 index 00000000..a6fd1c91 --- /dev/null +++ b/tests/specs/change_column_name/mysql/app/models/ColumnNameChange.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new ColumnNameChange(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->sentence; + $model->updated_at_2 = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php b/tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php new file mode 100644 index 00000000..2289ab14 --- /dev/null +++ b/tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php @@ -0,0 +1,28 @@ + [['name'], 'trim'], + 'required' => [['name'], 'required'], + 'name_string' => [['name'], 'string'], + ]; + } +} diff --git a/tests/specs/change_column_name/mysql/change_column_name.php b/tests/specs/change_column_name/mysql/change_column_name.php new file mode 100644 index 00000000..9784a19b --- /dev/null +++ b/tests/specs/change_column_name/mysql/change_column_name.php @@ -0,0 +1,13 @@ + '@specs/change_column_name/mysql/change_column_name.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => true, +]; diff --git a/tests/specs/change_column_name/mysql/change_column_name.yaml b/tests/specs/change_column_name/mysql/change_column_name.yaml new file mode 100644 index 00000000..fb34c6b7 --- /dev/null +++ b/tests/specs/change_column_name/mysql/change_column_name.yaml @@ -0,0 +1,33 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Change column name test +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Column_name_change: + type: object + description: Test model for change in column name test + required: + - id + - name + properties: + id: + type: integer + name: + type: string + x-db-type: varchar(255) + updated_at_2: + readOnly: true + type: string + format: datetime + nullable: false + x-db-type: datetime diff --git a/tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php b/tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php new file mode 100644 index 00000000..60bb3ff0 --- /dev/null +++ b/tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php @@ -0,0 +1,19 @@ +db->createCommand('ALTER TABLE {{%column_name_changes}} ADD COLUMN updated_at_2 timestamp NOT NULL')->execute(); + $this->dropColumn('{{%column_name_changes}}', 'updated_at'); + } + + public function safeDown() + { + $this->addColumn('{{%column_name_changes}}', 'updated_at', $this->timestamp()->notNull()); + $this->dropColumn('{{%column_name_changes}}', 'updated_at_2'); + } +} diff --git a/tests/specs/change_column_name/pgsql/app/models/BaseModelFaker.php b/tests/specs/change_column_name/pgsql/app/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/change_column_name/pgsql/app/models/BaseModelFaker.php @@ -0,0 +1,144 @@ +faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language)); + $this->uniqueFaker = new UniqueGenerator($this->faker); + } + + abstract public function generateModel($attributes = []); + + public function getFaker():Generator + { + return $this->faker; + } + + public function getUniqueFaker():UniqueGenerator + { + return $this->uniqueFaker; + } + + public function setFaker(Generator $faker):void + { + $this->faker = $faker; + } + + public function setUniqueFaker(UniqueGenerator $faker):void + { + $this->uniqueFaker = $faker; + } + + /** + * Generate and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::makeOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + $model = $fakeBuilder->generateModel($attributes); + return $model; + } + + /** + * Generate, save and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::saveOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $model = static::makeOne($attributes, $uniqueFaker); + $model->save(); + return $model; + } + + /** + * Generate and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + return $model; + }, range(0, $number -1)); + } + + /** + * Generate, save and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + $model->save(); + return $model; + }, range(0, $number -1)); + } +} diff --git a/tests/specs/change_column_name/pgsql/app/models/ColumnNameChange.php b/tests/specs/change_column_name/pgsql/app/models/ColumnNameChange.php new file mode 100644 index 00000000..a6fd1c91 --- /dev/null +++ b/tests/specs/change_column_name/pgsql/app/models/ColumnNameChange.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new ColumnNameChange(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->sentence; + $model->updated_at_2 = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php b/tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php new file mode 100644 index 00000000..6af1a5ac --- /dev/null +++ b/tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php @@ -0,0 +1,28 @@ + [['name'], 'trim'], + 'required' => [['name'], 'required'], + 'name_string' => [['name'], 'string'], + ]; + } +} diff --git a/tests/specs/change_column_name/pgsql/change_column_name.php b/tests/specs/change_column_name/pgsql/change_column_name.php new file mode 100644 index 00000000..e15f2c06 --- /dev/null +++ b/tests/specs/change_column_name/pgsql/change_column_name.php @@ -0,0 +1,13 @@ + '@specs/change_column_name/pgsql/change_column_name.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => true, +]; diff --git a/tests/specs/change_column_name/pgsql/change_column_name.yaml b/tests/specs/change_column_name/pgsql/change_column_name.yaml new file mode 100644 index 00000000..ece7f2d2 --- /dev/null +++ b/tests/specs/change_column_name/pgsql/change_column_name.yaml @@ -0,0 +1,33 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Change column name test for Pgsql +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Column_name_change: + type: object + description: Test model for change in column name test for Pgsql + required: + - id + - name + properties: + id: + type: integer + name: + type: string + x-db-type: varchar(255) + updated_at_2: + readOnly: true + type: string + format: datetime + nullable: false + x-db-type: timestamp diff --git a/tests/unit/ChangeColumnNameTest.php b/tests/unit/ChangeColumnNameTest.php new file mode 100644 index 00000000..b8cdf02b --- /dev/null +++ b/tests/unit/ChangeColumnNameTest.php @@ -0,0 +1,71 @@ +deleteTables(); + $this->createTableForTest(); + $testFile = Yii::getAlias("@specs/change_column_name/mysql/change_column_name.php"); + $this->runGenerator($testFile, 'mysql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_maria_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/change_column_name/mysql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + + // Maria DB + $this->changeDbToMariadb(); + $this->deleteTables(); + $this->createTableForTest(); + $testFile = Yii::getAlias("@specs/change_column_name/mysql/change_column_name.php"); + $this->runGenerator($testFile, 'maria'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/change_column_name/maria/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + + // Pgsql + $this->changeDbToPgsql(); + $this->deleteTables(); + $this->createTableForTest(); + $testFile = Yii::getAlias("@specs/change_column_name/pgsql/change_column_name.php"); + $this->runGenerator($testFile, 'pgsql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db', 'migrations_maria_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/change_column_name/pgsql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } + + private function createTableForTest() + { + Yii::$app->db->createCommand()->createTable('{{%column_name_changes}}', [ + 'id' => 'pk', + 'name' => 'varchar(255) NOT NULL', + 'updated_at' => 'datetime NOT NULL', + ])->execute(); + } + + private function deleteTables() + { + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%column_name_changes}}')->execute(); + } +} From d7e57a0241bbcc351f1465473f6ce6ecb8db15c6 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 17:20:22 +0530 Subject: [PATCH 41/62] remove unwanted space from README --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 9d20b16a..107df33f 100644 --- a/README.md +++ b/README.md @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - From 220d1ee4e6bcf3e7e3e92472effe773b6f8827cb Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 19:09:54 +0530 Subject: [PATCH 42/62] Test --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 107df33f..9d20b16a 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From c73c16d99ea688ed09af3500d53d95dec342c2f1 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Dec 2022 20:47:36 +0530 Subject: [PATCH 43/62] Fix https://github.com/cebe/yii2-openapi/issues/125 --- README.md | 1 - src/lib/migrations/BaseMigrationBuilder.php | 12 ++--- .../migrations/PostgresMigrationBuilder.php | 2 +- .../m200000_000000_change_table_v2_posts.php | 4 +- ...0000_000003_change_table_v2_categories.php | 2 +- .../m200000_000004_change_table_v2_users.php | 2 +- ...200000_000000_change_table_editcolumns.php | 2 +- .../mysql/no_syntax_error_107.php | 13 +++++ .../mysql/no_syntax_error_107.yaml | 41 +++++++++++++++ ...200000_000001_change_table_editcolumns.php | 4 +- tests/unit/IssueFixTest.php | 51 +++++++++++++++++++ 11 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php create mode 100644 tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.yaml create mode 100644 tests/unit/IssueFixTest.php diff --git a/README.md b/README.md index 9d20b16a..107df33f 100644 --- a/README.md +++ b/README.md @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index ab49d0bf..7cd7e42e 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -400,18 +400,12 @@ 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; } public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index f9627cf6..da9e236b 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -67,7 +67,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir , 'dbType', 'phpType' , 'precision', 'scale', 'unsigned' ], $changed))) { - $addUsing = $this->isNeedUsingExpression($current->type, $desired->type); + $addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType); $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired)); $this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing)); } 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..25a2289c 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 @@ -20,8 +20,8 @@ public function safeUp() 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..c887dcde 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 @@ -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 7bfe1bf9..594232f7 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/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..821eca5e 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 @@ -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/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..d91e8904 --- /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 79e81cd7..ca847c25 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 @@ -29,11 +29,11 @@ public function safeUp() 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/IssueFixTest.php b/tests/unit/IssueFixTest.php new file mode 100644 index 00000000..43ae394f --- /dev/null +++ b/tests/unit/IssueFixTest.php @@ -0,0 +1,51 @@ +runGenerator($testFile, 'mysql'); + // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + // 'recursive' => true, + // ]); + // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/id_not_in_rules/app"), [ + // 'recursive' => true, + // ]); + // $this->checkFiles($actualFiles, $expectedFiles); + + // $this->changeDbToMariadb(); + // $this->deleteTablesForNoSyntaxError107(); + // $this->createTableForNoSyntaxError107(); + // $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php"); + // $this->runGenerator($testFile, 'maria'); + + $this->changeDbToPgsql(); + $this->deleteTablesForNoSyntaxError107(); + $this->createTableForNoSyntaxError107(); + $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php"); + $this->runGenerator($testFile, 'pgsql'); + } + + 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(); + } +} From a6fc5b1c9fbae58c21f7f02a06790b4de1542c09 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 31 Dec 2022 13:42:59 +0530 Subject: [PATCH 44/62] Run actual migrations --- src/lib/ColumnToCode.php | 5 ++++- src/lib/migrations/BaseMigrationBuilder.php | 1 + .../migrations/PostgresMigrationBuilder.php | 2 +- tests/DbTestCase.php | 21 +++++++++++++++++++ .../m200000_000000_change_table_v2_posts.php | 4 ++-- ...0000_000003_change_table_v2_categories.php | 2 +- ...200000_000005_change_table_v2_comments.php | 6 +++--- ...200000_000000_change_table_editcolumns.php | 2 +- ...200000_000001_change_table_editcolumns.php | 8 +++---- tests/unit/EnumTest.php | 9 ++++++++ tests/unit/XDbTypeTest.php | 9 ++++++++ 11 files changed, 56 insertions(+), 13 deletions(-) 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 7cd7e42e..24a22bad 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -408,6 +408,7 @@ protected function isNeedUsingExpression(string $fromDbType, string $toDbType):b 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 da9e236b..d1a314bf 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -68,7 +68,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir , 'precision', 'scale', 'unsigned' ], $changed))) { $addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType); - $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired)); + $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..33f914b6 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -104,4 +104,25 @@ 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.' migrations were applied.'); + // 1 migration was 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.' 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 25a2289c..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,8 +12,8 @@ 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}}'); } 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 c887dcde..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); } 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 821eca5e..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'); 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 ca847c25..4ab0d848 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,19 +11,19 @@ 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"); } 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/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() From 09a5fb85c52fee411076c8b0b20f84e2c2fc08e5 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 31 Dec 2022 21:09:39 +0530 Subject: [PATCH 45/62] Add a test for float issue in PgSQL --- .../issue_fix/float_issue/float_issue.php | 13 ++++++++ .../issue_fix/float_issue/float_issue.yaml | 28 +++++++++++++++++ tests/unit/IssueFixTest.php | 30 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 tests/specs/issue_fix/float_issue/float_issue.php create mode 100644 tests/specs/issue_fix/float_issue/float_issue.yaml 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/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 43ae394f..86944ed8 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -9,6 +9,7 @@ // This class contains tests for various issues present at GitHub class IssueFixTest extends DbTestCase { + // TODO WIP resume from here // fix https://github.com/cebe/yii2-openapi/issues/107 // 107_no_syntax_error public function testMigrationsAreNotGeneratedWithSyntaxError() @@ -48,4 +49,33 @@ private function createTableForNoSyntaxError107() '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(); + } } From 67ad00cc5b846e854bffab7ca9738cbbfc068cb6 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 31 Dec 2022 21:12:56 +0530 Subject: [PATCH 46/62] test --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 107df33f..9d20b16a 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From 2e382e48d5f2471894050d022b1af76caa59eb65 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 31 Dec 2022 21:16:04 +0530 Subject: [PATCH 47/62] Undo test --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 9d20b16a..107df33f 100644 --- a/README.md +++ b/README.md @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - From a8d87882b4951ebb0a367192d1c9240cad309c20 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 31 Dec 2022 21:23:24 +0530 Subject: [PATCH 48/62] WIP --- src/lib/migrations/BaseMigrationBuilder.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index ab49d0bf..4403ff59 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -418,16 +418,22 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): { $tableName = 'tmp_table_'; $db = 'db'; - if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - $db = 'pg_test_db_2'; - } + // if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { + // $db = 'pg_test_db_2'; + // } Yii::$app->{$db}->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); if (is_string($columnSchema->xDbType) && !empty($columnSchema->xDbType)) { $column = [$columnSchema->name.' '.$this->newColStr($columnSchema)]; + if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { + $column = strtr($column, ['enum_'.$columnSchema->name => '_tmp_enum_'.$columnSchema->name]); + } } else { $column = [$columnSchema->name => $this->newColStr($columnSchema)]; + if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { + $column[$columnSchema->name] = strtr($columnSchema->name, ['enum_'.$columnSchema->name => '_tmp_enum_'.$columnSchema->name]); + } } // create enum if relevant @@ -437,7 +443,7 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): return "'$aValue'"; }, $allEnumValues); Yii::$app->{$db}->createCommand( - 'CREATE TYPE enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')' + 'CREATE TYPE _tmp_enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')' )->execute(); } @@ -448,7 +454,7 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): Yii::$app->{$db}->createCommand()->dropTable($tableName)->execute(); if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum - Yii::$app->{$db}->createCommand('DROP TYPE enum_'.$columnSchema->name)->execute(); + Yii::$app->{$db}->createCommand('DROP TYPE _tmp_enum_'.$columnSchema->name)->execute(); } return $table->columns[$columnSchema->name]; From 433e7f0337d70de9ac2dfb567d6477555af89d8d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 2 Jan 2023 13:40:21 +0530 Subject: [PATCH 49/62] Fix https://github.com/cebe/yii2-openapi/issues/126 --- Makefile | 2 -- src/lib/migrations/BaseMigrationBuilder.php | 28 +++++++++++---------- tests/config/console.php | 7 ------ 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 9a7e0370..9f6547a4 100644 --- a/Makefile +++ b/Makefile @@ -34,8 +34,6 @@ up: 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 - docker-compose exec -T postgres bash -c "psql --username dbuser --dbname testdb -c 'create database pg_test_db_2;'; psql --username dbuser --dbname testdb -c 'grant all privileges on database pg_test_db_2 to dbuser;'" || (docker-compose ps; docker-compose logs; exit 1) - cli: docker-compose exec php bash diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 4403ff59..cc1d20a5 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -417,22 +417,18 @@ protected function isNeedUsingExpression(string $fromType, string $toType):bool public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema { $tableName = 'tmp_table_'; - $db = 'db'; - // if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - // $db = 'pg_test_db_2'; - // } - Yii::$app->{$db}->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); if (is_string($columnSchema->xDbType) && !empty($columnSchema->xDbType)) { $column = [$columnSchema->name.' '.$this->newColStr($columnSchema)]; if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - $column = strtr($column, ['enum_'.$columnSchema->name => '_tmp_enum_'.$columnSchema->name]); + $column = strtr($column, ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']); } } else { $column = [$columnSchema->name => $this->newColStr($columnSchema)]; if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - $column[$columnSchema->name] = strtr($columnSchema->name, ['enum_'.$columnSchema->name => '_tmp_enum_'.$columnSchema->name]); + $column[$columnSchema->name] = strtr($column[$columnSchema->name], ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']); } } @@ -442,19 +438,25 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): $allEnumValues = array_map(function ($aValue) { return "'$aValue'"; }, $allEnumValues); - Yii::$app->{$db}->createCommand( - 'CREATE TYPE _tmp_enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')' + Yii::$app->db->createCommand( + 'CREATE TYPE tmp_enum_'.$columnSchema->name.'_ AS ENUM('.implode(', ', $allEnumValues).')' )->execute(); } - Yii::$app->{$db}->createCommand()->createTable($tableName, $column)->execute(); + Yii::$app->db->createCommand()->createTable($tableName, $column)->execute(); - $table = Yii::$app->{$db}->getTableSchema($tableName); + $table = Yii::$app->db->getTableSchema($tableName); - Yii::$app->{$db}->createCommand()->dropTable($tableName)->execute(); + Yii::$app->db->createCommand()->dropTable($tableName)->execute(); if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum - Yii::$app->{$db}->createCommand('DROP TYPE _tmp_enum_'.$columnSchema->name)->execute(); + Yii::$app->db->createCommand('DROP TYPE tmp_enum_'.$columnSchema->name.'_')->execute(); + if ($table->columns[$columnSchema->name]->dbType !== 'tmp_enum_'.$columnSchema->name.'_') { + throw new \Exception('Unknown error related to PgSQL enum'); + } + // reset back column enum name to original as we are comparing with current + // e.g. we get different enum type name such as `enum_status` and `tmp_enum_status_` even there is no change, so below statement fix this issue + $table->columns[$columnSchema->name]->dbType = 'enum_'.$columnSchema->name; } return $table->columns[$columnSchema->name]; diff --git a/tests/config/console.php b/tests/config/console.php index e3bb729e..a562c131 100644 --- a/tests/config/console.php +++ b/tests/config/console.php @@ -53,13 +53,6 @@ 'charset' => 'utf8', 'tablePrefix'=>'itt_', ], - 'pg_test_db_2' => [ - 'class' => \yii\db\Connection::class, - 'dsn' => 'pgsql:host=postgres;dbname=pg_test_db_2', - 'username' => 'dbuser', - 'password' => 'dbpass', - 'charset' => 'utf8', - ], 'mysql' => [ 'class' => \yii\db\Connection::class, 'dsn' => 'mysql:host=mysql;dbname=testdb', From d0b83eb3c9057ea96f891f10ea0c59868d263223 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 2 Jan 2023 13:54:40 +0530 Subject: [PATCH 50/62] Remove unwanted line --- Makefile | 1 - 1 file changed, 1 deletion(-) 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 From 9e341e9fee14e7886bb8a080ef58e32078d4870e Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 2 Jan 2023 21:47:34 +0530 Subject: [PATCH 51/62] Initial commit of this branch --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 107df33f..9d20b16a 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From 4da1db27dd4d61fc2309a20d4d83d4e2dd325858 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 2 Jan 2023 22:25:40 +0530 Subject: [PATCH 52/62] Fix https://github.com/cebe/yii2-openapi/issues/127 --- README.md | 1 - src/lib/migrations/BaseMigrationBuilder.php | 3 +- src/lib/migrations/MigrationRecordBuilder.php | 23 ++- ...m200000_000002_create_table_blog_posts.php | 2 +- ...0000_000004_create_table_post_comments.php | 4 +- .../m200000_000001_create_table_v2_tags.php | 2 +- .../m200000_000004_change_table_v2_users.php | 2 +- ...00000_change_table_column_name_changes.php | 2 +- .../m200000_000000_create_table_menus.php | 4 +- ...000_000000_create_table_alldbdatatypes.php | 186 +++++++++--------- ...200000_000001_change_table_editcolumns.php | 16 +- ...m200000_000002_create_table_newcolumns.php | 18 +- .../m200000_000003_create_table_pristines.php | 22 +-- ...000_000000_create_table_alldbdatatypes.php | 186 +++++++++--------- ...200000_000001_create_table_editcolumns.php | 20 +- ...m200000_000002_create_table_newcolumns.php | 18 +- .../m200000_000003_create_table_pristines.php | 22 +-- ...000_000000_create_table_alldbdatatypes.php | 186 +++++++++--------- ...200000_000001_create_table_editcolumns.php | 20 +- ...m200000_000002_change_table_newcolumns.php | 16 +- .../m200000_000003_create_table_pristines.php | 22 +-- 21 files changed, 394 insertions(+), 381 deletions(-) diff --git a/README.md b/README.md index 9d20b16a..107df33f 100644 --- a/README.md +++ b/README.md @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index cc1d20a5..4e902083 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -421,7 +421,8 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); if (is_string($columnSchema->xDbType) && !empty($columnSchema->xDbType)) { - $column = [$columnSchema->name.' '.$this->newColStr($columnSchema)]; + $name = MigrationRecordBuilder::quote($columnSchema->name); + $column = [$name.' '.$this->newColStr($columnSchema)]; if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { $column = strtr($column, ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']); } diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index d15ee070..4c90ec89 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -33,13 +33,14 @@ final class MigrationRecordBuilder public const ADD_FK = MigrationRecordBuilder::INDENT . "\$this->addForeignKey('%s', '%s', '%s', '%s', '%s');"; public const ADD_PK = MigrationRecordBuilder::INDENT . "\$this->addPrimaryKey('%s', '%s', '%s');"; public const ADD_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addColumn('%s', '%s', %s);"; + public const ALTER_COLUMN = MigrationRecordBuilder::INDENT . "\$this->alterColumn('%s', '%s', %s);"; - public const ADD_COLUMN_RAW = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ADD COLUMN %s %s')->execute();"; - public const ALTER_COLUMN = MigrationRecordBuilder::INDENT . "\$this->alterColumn('%s', '%s', %s);"; + public const ADD_COLUMN_RAW = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ADD COLUMN %s %s')->execute();"; public const ALTER_COLUMN_RAW = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s MODIFY %s %s')->execute();"; - public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE %s')->execute();"; + + public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN \"%s\" SET DATA TYPE %s')->execute();"; /** * @var \yii\db\Schema @@ -62,7 +63,8 @@ public function createTable(string $tableAlias, array $columns):string $codeColumns = []; foreach ($columns as $columnName => $cebeDbColumnSchema) { if (is_string($cebeDbColumnSchema->xDbType) && !empty($cebeDbColumnSchema->xDbType)) { - $codeColumns[] = $columnName.' '.$this->columnToCode($cebeDbColumnSchema, false)->getCode(); + $name = static::quote($columnName); + $codeColumns[] = $name.' '.$this->columnToCode($cebeDbColumnSchema, false)->getCode(); } else { $codeColumns[$columnName] = $this->columnToCode($cebeDbColumnSchema, false)->getCode(); } @@ -79,7 +81,8 @@ public function addColumn(string $tableAlias, ColumnSchema $column):string { if (is_string($column->xDbType) && !empty($column->xDbType)) { $converter = $this->columnToCode($column, false); - return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $column->name, $converter->getCode()); + $name = static::quote($column->name); + return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $name, $converter->getCode()); } $converter = $this->columnToCode($column, false); @@ -93,6 +96,7 @@ public function addDbColumn(string $tableAlias, ColumnSchema $column):string { if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) { $converter = $this->columnToCode($column, true); + $name = static::quote($column->name); return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $column->name, $converter->getCode()); } $converter = $this->columnToCode($column, true); @@ -269,4 +273,13 @@ private function columnToCode( $alterByXDbType ]); } + + // https://github.com/cebe/yii2-openapi/issues/127 + public static function quote(string $columnName): string + { + if (ApiGenerator::isPostgres()) { + return '"'.$columnName.'"'; + } + return $columnName; + } } diff --git a/tests/specs/blog/migrations_pgsql_db/m200000_000002_create_table_blog_posts.php b/tests/specs/blog/migrations_pgsql_db/m200000_000002_create_table_blog_posts.php index 780ed900..9e9d85dd 100644 --- a/tests/specs/blog/migrations_pgsql_db/m200000_000002_create_table_blog_posts.php +++ b/tests/specs/blog/migrations_pgsql_db/m200000_000002_create_table_blog_posts.php @@ -8,7 +8,7 @@ class m200000_000002_create_table_blog_posts extends \yii\db\Migration public function safeUp() { $this->createTable('{{%blog_posts}}', [ - 0 => 'uid varchar(128) NOT NULL', + 0 => '"uid" varchar(128) NOT NULL', 'title' => $this->string(255)->notNull(), 'slug' => $this->string(200)->null()->defaultValue(null), 'category_id' => $this->integer()->notNull(), diff --git a/tests/specs/blog/migrations_pgsql_db/m200000_000004_create_table_post_comments.php b/tests/specs/blog/migrations_pgsql_db/m200000_000004_create_table_post_comments.php index 731ec10e..2aa48549 100644 --- a/tests/specs/blog/migrations_pgsql_db/m200000_000004_create_table_post_comments.php +++ b/tests/specs/blog/migrations_pgsql_db/m200000_000004_create_table_post_comments.php @@ -11,8 +11,8 @@ public function safeUp() 'id' => $this->bigPrimaryKey(), 'post_id' => $this->string(128)->notNull(), 'author_id' => $this->integer()->notNull(), - 0 => 'message json NOT NULL DEFAULT \'[]\'', - 1 => 'meta_data json NOT NULL DEFAULT \'[]\'', + 0 => '"message" json NOT NULL DEFAULT \'[]\'', + 1 => '"meta_data" json NOT NULL DEFAULT \'[]\'', 'created_at' => $this->integer()->notNull(), ]); $this->addForeignKey('fk_post_comments_post_id_blog_posts_uid', '{{%post_comments}}', 'post_id', '{{%blog_posts}}', 'uid'); diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php index ea6d7ff8..bcef7fa8 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php @@ -10,7 +10,7 @@ public function safeUp() $this->execute('CREATE TYPE enum_lang AS ENUM(\'ru\', \'eng\')'); $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), - 0 => 'name varchar(100) NOT NULL', + 0 => '"name" varchar(100) NOT NULL', 'lang' => 'enum_lang NOT NULL', ]); $this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true); 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 7bfe1bf9..0b58811c 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 @@ -11,7 +11,7 @@ public function safeUp() $this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull()); $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT"); - $this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN email SET DATA TYPE varchar(255)')->execute(); + $this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN "email" SET DATA TYPE varchar(255)')->execute(); $this->alterColumn('{{%v2_users}}', 'role', 'enum_role USING "role"::enum_role'); $this->alterColumn('{{%v2_users}}', 'role', "DROP DEFAULT"); $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); diff --git a/tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php b/tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php index 60bb3ff0..803cb366 100644 --- a/tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php +++ b/tests/specs/change_column_name/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_column_name_changes.php @@ -7,7 +7,7 @@ class m200000_000000_change_table_column_name_changes extends \yii\db\Migration { public function safeUp() { - $this->db->createCommand('ALTER TABLE {{%column_name_changes}} ADD COLUMN updated_at_2 timestamp NOT NULL')->execute(); + $this->db->createCommand('ALTER TABLE {{%column_name_changes}} ADD COLUMN "updated_at_2" timestamp NOT NULL')->execute(); $this->dropColumn('{{%column_name_changes}}', 'updated_at'); } diff --git a/tests/specs/menu/migrations/m200000_000000_create_table_menus.php b/tests/specs/menu/migrations/m200000_000000_create_table_menus.php index 03cde247..c26156c3 100644 --- a/tests/specs/menu/migrations/m200000_000000_create_table_menus.php +++ b/tests/specs/menu/migrations/m200000_000000_create_table_menus.php @@ -11,8 +11,8 @@ public function safeUp() 'id' => $this->bigPrimaryKey(), 'name' => $this->string(100)->notNull(), 'parent_id' => $this->bigInteger()->null()->defaultValue(null), - 0 => 'args text[] NULL DEFAULT \'{"foo","bar","baz"}\'', - 1 => 'kwargs json NOT NULL DEFAULT \'[{"foo":"bar"},{"buzz":"fizz"}]\'', + 0 => '"args" text[] NULL DEFAULT \'{"foo","bar","baz"}\'', + 1 => '"kwargs" json NOT NULL DEFAULT \'[{"foo":"bar"},{"buzz":"fizz"}]\'', ]); $this->addForeignKey('fk_menus_parent_id_menus_id', '{{%menus}}', 'parent_id', '{{%menus}}', 'id'); } diff --git a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php index 53498798..e0cac52a 100644 --- a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php +++ b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php @@ -9,99 +9,99 @@ public function safeUp() { $this->createTable('{{%alldbdatatypes}}', [ 'id' => $this->bigPrimaryKey(), - 0 => 'string_col varchar NULL DEFAULT NULL', - 1 => 'varchar_col varchar NULL DEFAULT NULL', - 2 => 'text_col text NULL DEFAULT NULL', - 3 => 'text_col_array text[] NULL DEFAULT NULL', - 4 => 'varchar_4_col varchar(4) NULL DEFAULT NULL', - 5 => 'varchar_5_col varchar(5) NULL DEFAULT NULL', - 6 => 'char_4_col char(4) NULL DEFAULT NULL', - 7 => 'char_5_col char NULL DEFAULT NULL', - 8 => 'char_6_col char NOT NULL', - 9 => 'char_7_col char(6) NOT NULL', - 10 => 'char_8_col char NULL DEFAULT \'d\'', - 11 => 'decimal_col decimal(12,3) NULL DEFAULT NULL', - 12 => 'bytea_col_2 bytea NULL DEFAULT NULL', - 13 => 'bit_col bit NULL DEFAULT NULL', - 14 => 'bit_2 bit(1) NULL DEFAULT NULL', - 15 => 'bit_3 bit(64) NULL DEFAULT NULL', - 16 => 'ti smallint NULL DEFAULT NULL', - 17 => 'int2_col int2 NULL DEFAULT NULL', - 18 => 'smallserial_col smallserial NOT NULL', - 19 => 'serial2_col serial2 NOT NULL', - 20 => 'si_col smallint NULL DEFAULT NULL', - 21 => 'si_col_2 smallint NULL DEFAULT NULL', - 22 => 'bi bigint NULL DEFAULT NULL', - 23 => 'bi2 int8 NULL DEFAULT NULL', - 24 => 'int4_col int4 NULL DEFAULT NULL', - 25 => 'bigserial_col bigserial NOT NULL', - 26 => 'bigserial_col_2 serial8 NOT NULL', - 27 => 'int_col int NULL DEFAULT NULL', - 28 => 'int_col_2 integer NULL DEFAULT NULL', - 29 => 'numeric_col numeric NULL DEFAULT NULL', - 30 => 'numeric_col_2 numeric(10) NULL DEFAULT NULL', - 31 => 'numeric_col_3 numeric(10,2) NULL DEFAULT NULL', - 32 => 'double_p_2 double precision NULL DEFAULT NULL', - 33 => 'double_p_3 double precision NULL DEFAULT NULL', - 34 => 'real_col real NULL DEFAULT NULL', - 35 => 'float4_col float4 NULL DEFAULT NULL', - 36 => 'date_col date NULL DEFAULT NULL', - 37 => 'time_col time NULL DEFAULT NULL', - 38 => 'time_col_2 time with time zone NULL DEFAULT NULL', - 39 => 'time_col_3 time without time zone NULL DEFAULT NULL', - 40 => 'time_col_4 time(3) without time zone NULL DEFAULT NULL', - 41 => 'timetz_col timetz NULL DEFAULT NULL', - 42 => 'timetz_col_2 timetz(3) NULL DEFAULT NULL', - 43 => 'timestamp_col timestamp NULL DEFAULT NULL', - 44 => 'timestamp_col_2 timestamp with time zone NULL DEFAULT NULL', - 45 => 'timestamp_col_3 timestamp without time zone NULL DEFAULT NULL', - 46 => 'timestamp_col_4 timestamp(3) without time zone NULL DEFAULT NULL', - 47 => 'timestamptz_col timestamptz NULL DEFAULT NULL', - 48 => 'timestamptz_col_2 timestamptz(3) NULL DEFAULT NULL', - 49 => 'date2 date NULL DEFAULT NULL', - 50 => 'timestamp_col_z timestamp NULL DEFAULT NULL', - 51 => 'bit_varying bit varying NULL DEFAULT NULL', - 52 => 'bit_varying_n bit varying(8) NULL DEFAULT NULL', - 53 => 'bit_varying_n_2 varbit NULL DEFAULT NULL', - 54 => 'bit_varying_n_3 varbit(3) NULL DEFAULT NULL', - 55 => 'bool_col boolean NULL DEFAULT NULL', - 56 => 'bool_col_2 bool NULL DEFAULT NULL', - 57 => 'box_col box NULL DEFAULT NULL', - 58 => 'character_col character NULL DEFAULT NULL', - 59 => 'character_n character(12) NULL DEFAULT NULL', - 60 => 'character_varying character varying NULL DEFAULT NULL', - 61 => 'character_varying_n character varying(12) NULL DEFAULT NULL', - 62 => 'json_col json NOT NULL', - 63 => 'jsonb_col jsonb NOT NULL', - 64 => 'json_col_def json NOT NULL DEFAULT \'[]\'', - 65 => 'json_col_def_2 json NOT NULL DEFAULT \'[]\'', - 66 => 'bytea_def bytea NULL DEFAULT \'the bytea blob default\'', - 67 => 'text_def text NULL DEFAULT \'the text\'', - 68 => 'json_def json NOT NULL DEFAULT \'{"a":"b"}\'', - 69 => 'jsonb_def jsonb NOT NULL DEFAULT \'{"ba":"bb"}\'', - 70 => 'cidr_col cidr NULL DEFAULT NULL', - 71 => 'circle_col circle NULL DEFAULT NULL', - 72 => 'date_col_z date NULL DEFAULT NULL', - 73 => 'float8_col float8 NULL DEFAULT NULL', - 74 => 'inet_col inet NULL DEFAULT NULL', - 75 => 'interval_col interval NULL DEFAULT NULL', - 76 => 'interval_col_2 interval year NULL DEFAULT NULL', - 77 => 'interval_col_3 interval day to second(3) NULL DEFAULT NULL', - 78 => 'line_col line NULL DEFAULT NULL', - 79 => 'lseg_col lseg NULL DEFAULT NULL', - 80 => 'macaddr_col macaddr NULL DEFAULT NULL', - 81 => 'money_col money NULL DEFAULT NULL', - 82 => 'path_col path NULL DEFAULT NULL', - 83 => 'pg_lsn_col pg_lsn NULL DEFAULT NULL', - 84 => 'point_col point NULL DEFAULT NULL', - 85 => 'polygon_col polygon NULL DEFAULT NULL', - 86 => 'serial_col serial NOT NULL', - 87 => 'serial4_col serial4 NOT NULL', - 88 => 'tsquery_col tsquery NULL DEFAULT NULL', - 89 => 'tsvector_col tsvector NULL', - 90 => 'txid_snapshot_col txid_snapshot NULL DEFAULT NULL', - 91 => 'uuid_col uuid NULL DEFAULT NULL', - 92 => 'xml_col xml NULL DEFAULT NULL', + 0 => '"string_col" varchar NULL DEFAULT NULL', + 1 => '"varchar_col" varchar NULL DEFAULT NULL', + 2 => '"text_col" text NULL DEFAULT NULL', + 3 => '"text_col_array" text[] NULL DEFAULT NULL', + 4 => '"varchar_4_col" varchar(4) NULL DEFAULT NULL', + 5 => '"varchar_5_col" varchar(5) NULL DEFAULT NULL', + 6 => '"char_4_col" char(4) NULL DEFAULT NULL', + 7 => '"char_5_col" char NULL DEFAULT NULL', + 8 => '"char_6_col" char NOT NULL', + 9 => '"char_7_col" char(6) NOT NULL', + 10 => '"char_8_col" char NULL DEFAULT \'d\'', + 11 => '"decimal_col" decimal(12,3) NULL DEFAULT NULL', + 12 => '"bytea_col_2" bytea NULL DEFAULT NULL', + 13 => '"bit_col" bit NULL DEFAULT NULL', + 14 => '"bit_2" bit(1) NULL DEFAULT NULL', + 15 => '"bit_3" bit(64) NULL DEFAULT NULL', + 16 => '"ti" smallint NULL DEFAULT NULL', + 17 => '"int2_col" int2 NULL DEFAULT NULL', + 18 => '"smallserial_col" smallserial NOT NULL', + 19 => '"serial2_col" serial2 NOT NULL', + 20 => '"si_col" smallint NULL DEFAULT NULL', + 21 => '"si_col_2" smallint NULL DEFAULT NULL', + 22 => '"bi" bigint NULL DEFAULT NULL', + 23 => '"bi2" int8 NULL DEFAULT NULL', + 24 => '"int4_col" int4 NULL DEFAULT NULL', + 25 => '"bigserial_col" bigserial NOT NULL', + 26 => '"bigserial_col_2" serial8 NOT NULL', + 27 => '"int_col" int NULL DEFAULT NULL', + 28 => '"int_col_2" integer NULL DEFAULT NULL', + 29 => '"numeric_col" numeric NULL DEFAULT NULL', + 30 => '"numeric_col_2" numeric(10) NULL DEFAULT NULL', + 31 => '"numeric_col_3" numeric(10,2) NULL DEFAULT NULL', + 32 => '"double_p_2" double precision NULL DEFAULT NULL', + 33 => '"double_p_3" double precision NULL DEFAULT NULL', + 34 => '"real_col" real NULL DEFAULT NULL', + 35 => '"float4_col" float4 NULL DEFAULT NULL', + 36 => '"date_col" date NULL DEFAULT NULL', + 37 => '"time_col" time NULL DEFAULT NULL', + 38 => '"time_col_2" time with time zone NULL DEFAULT NULL', + 39 => '"time_col_3" time without time zone NULL DEFAULT NULL', + 40 => '"time_col_4" time(3) without time zone NULL DEFAULT NULL', + 41 => '"timetz_col" timetz NULL DEFAULT NULL', + 42 => '"timetz_col_2" timetz(3) NULL DEFAULT NULL', + 43 => '"timestamp_col" timestamp NULL DEFAULT NULL', + 44 => '"timestamp_col_2" timestamp with time zone NULL DEFAULT NULL', + 45 => '"timestamp_col_3" timestamp without time zone NULL DEFAULT NULL', + 46 => '"timestamp_col_4" timestamp(3) without time zone NULL DEFAULT NULL', + 47 => '"timestamptz_col" timestamptz NULL DEFAULT NULL', + 48 => '"timestamptz_col_2" timestamptz(3) NULL DEFAULT NULL', + 49 => '"date2" date NULL DEFAULT NULL', + 50 => '"timestamp_col_z" timestamp NULL DEFAULT NULL', + 51 => '"bit_varying" bit varying NULL DEFAULT NULL', + 52 => '"bit_varying_n" bit varying(8) NULL DEFAULT NULL', + 53 => '"bit_varying_n_2" varbit NULL DEFAULT NULL', + 54 => '"bit_varying_n_3" varbit(3) NULL DEFAULT NULL', + 55 => '"bool_col" boolean NULL DEFAULT NULL', + 56 => '"bool_col_2" bool NULL DEFAULT NULL', + 57 => '"box_col" box NULL DEFAULT NULL', + 58 => '"character_col" character NULL DEFAULT NULL', + 59 => '"character_n" character(12) NULL DEFAULT NULL', + 60 => '"character_varying" character varying NULL DEFAULT NULL', + 61 => '"character_varying_n" character varying(12) NULL DEFAULT NULL', + 62 => '"json_col" json NOT NULL', + 63 => '"jsonb_col" jsonb NOT NULL', + 64 => '"json_col_def" json NOT NULL DEFAULT \'[]\'', + 65 => '"json_col_def_2" json NOT NULL DEFAULT \'[]\'', + 66 => '"bytea_def" bytea NULL DEFAULT \'the bytea blob default\'', + 67 => '"text_def" text NULL DEFAULT \'the text\'', + 68 => '"json_def" json NOT NULL DEFAULT \'{"a":"b"}\'', + 69 => '"jsonb_def" jsonb NOT NULL DEFAULT \'{"ba":"bb"}\'', + 70 => '"cidr_col" cidr NULL DEFAULT NULL', + 71 => '"circle_col" circle NULL DEFAULT NULL', + 72 => '"date_col_z" date NULL DEFAULT NULL', + 73 => '"float8_col" float8 NULL DEFAULT NULL', + 74 => '"inet_col" inet NULL DEFAULT NULL', + 75 => '"interval_col" interval NULL DEFAULT NULL', + 76 => '"interval_col_2" interval year NULL DEFAULT NULL', + 77 => '"interval_col_3" interval day to second(3) NULL DEFAULT NULL', + 78 => '"line_col" line NULL DEFAULT NULL', + 79 => '"lseg_col" lseg NULL DEFAULT NULL', + 80 => '"macaddr_col" macaddr NULL DEFAULT NULL', + 81 => '"money_col" money NULL DEFAULT NULL', + 82 => '"path_col" path NULL DEFAULT NULL', + 83 => '"pg_lsn_col" pg_lsn NULL DEFAULT NULL', + 84 => '"point_col" point NULL DEFAULT NULL', + 85 => '"polygon_col" polygon NULL DEFAULT NULL', + 86 => '"serial_col" serial NOT NULL', + 87 => '"serial4_col" serial4 NOT NULL', + 88 => '"tsquery_col" tsquery NULL DEFAULT NULL', + 89 => '"tsvector_col" tsvector NULL', + 90 => '"txid_snapshot_col" txid_snapshot NULL DEFAULT NULL', + 91 => '"uuid_col" uuid NULL DEFAULT NULL', + 92 => '"xml_col" xml NULL DEFAULT NULL', ]); } 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 79e81cd7..f753b3fe 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 @@ -7,20 +7,20 @@ class m200000_000001_change_table_editcolumns extends \yii\db\Migration { public function safeUp() { - $this->db->createCommand('ALTER TABLE {{%editcolumns}} ADD COLUMN first_name varchar NULL DEFAULT NULL')->execute(); - $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}} ADD COLUMN "first_name" varchar NULL DEFAULT NULL')->execute(); + $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->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')->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->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')->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()); diff --git a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php index 3604dce0..4bd08302 100644 --- a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php +++ b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php @@ -9,16 +9,16 @@ public function safeUp() { $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), - 0 => 'name varchar NOT NULL', - 1 => 'first_name varchar NULL DEFAULT NULL', + 0 => '"name" varchar NOT NULL', + 1 => '"first_name" varchar NULL DEFAULT NULL', 'last_name' => $this->text()->null()->defaultValue(null), - 2 => 'dec_col decimal(12,4) NULL DEFAULT NULL', - 3 => 'json_col json NOT NULL', - 4 => 'varchar_col varchar NULL DEFAULT NULL', - 5 => 'numeric_col double precision NULL DEFAULT NULL', - 6 => 'json_col_def_n json NOT NULL DEFAULT \'[]\'', - 7 => 'json_col_def_n_2 json NOT NULL DEFAULT \'[]\'', - 8 => 'text_col_array text[] NULL DEFAULT NULL', + 2 => '"dec_col" decimal(12,4) NULL DEFAULT NULL', + 3 => '"json_col" json NOT NULL', + 4 => '"varchar_col" varchar NULL DEFAULT NULL', + 5 => '"numeric_col" double precision NULL DEFAULT NULL', + 6 => '"json_col_def_n" json NOT NULL DEFAULT \'[]\'', + 7 => '"json_col_def_n_2" json NOT NULL DEFAULT \'[]\'', + 8 => '"text_col_array" text[] NULL DEFAULT NULL', ]); } diff --git a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php index ab2c1eab..a5ac94cd 100644 --- a/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/edit_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php @@ -8,18 +8,18 @@ class m200000_000003_create_table_pristines extends \yii\db\Migration public function safeUp() { $this->createTable('{{%pristines}}', [ - 0 => 'custom_id_col serial primary key NOT NULL', - 1 => 'name text NOT NULL', + 0 => '"custom_id_col" serial primary key NOT NULL', + 1 => '"name" text NOT NULL', 'tag' => $this->text()->null()->defaultValue("4 leg"), - 2 => 'new_col varchar NULL DEFAULT NULL', - 3 => 'col_5 decimal(12,4) NULL DEFAULT NULL', - 4 => 'col_6 decimal(11,2) NULL DEFAULT NULL', - 5 => 'col_7 decimal(10,2) NULL DEFAULT NULL', - 6 => 'col_8 json NOT NULL', - 7 => 'col_9 varchar NULL DEFAULT NULL', - 8 => 'col_10 varchar NULL DEFAULT NULL', - 9 => 'col_11 text NULL DEFAULT NULL', - 10 => 'price decimal(10,2) NULL DEFAULT 0', + 2 => '"new_col" varchar NULL DEFAULT NULL', + 3 => '"col_5" decimal(12,4) NULL DEFAULT NULL', + 4 => '"col_6" decimal(11,2) NULL DEFAULT NULL', + 5 => '"col_7" decimal(10,2) NULL DEFAULT NULL', + 6 => '"col_8" json NOT NULL', + 7 => '"col_9" varchar NULL DEFAULT NULL', + 8 => '"col_10" varchar NULL DEFAULT NULL', + 9 => '"col_11" text NULL DEFAULT NULL', + 10 => '"price" decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php index 53498798..e0cac52a 100644 --- a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php +++ b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php @@ -9,99 +9,99 @@ public function safeUp() { $this->createTable('{{%alldbdatatypes}}', [ 'id' => $this->bigPrimaryKey(), - 0 => 'string_col varchar NULL DEFAULT NULL', - 1 => 'varchar_col varchar NULL DEFAULT NULL', - 2 => 'text_col text NULL DEFAULT NULL', - 3 => 'text_col_array text[] NULL DEFAULT NULL', - 4 => 'varchar_4_col varchar(4) NULL DEFAULT NULL', - 5 => 'varchar_5_col varchar(5) NULL DEFAULT NULL', - 6 => 'char_4_col char(4) NULL DEFAULT NULL', - 7 => 'char_5_col char NULL DEFAULT NULL', - 8 => 'char_6_col char NOT NULL', - 9 => 'char_7_col char(6) NOT NULL', - 10 => 'char_8_col char NULL DEFAULT \'d\'', - 11 => 'decimal_col decimal(12,3) NULL DEFAULT NULL', - 12 => 'bytea_col_2 bytea NULL DEFAULT NULL', - 13 => 'bit_col bit NULL DEFAULT NULL', - 14 => 'bit_2 bit(1) NULL DEFAULT NULL', - 15 => 'bit_3 bit(64) NULL DEFAULT NULL', - 16 => 'ti smallint NULL DEFAULT NULL', - 17 => 'int2_col int2 NULL DEFAULT NULL', - 18 => 'smallserial_col smallserial NOT NULL', - 19 => 'serial2_col serial2 NOT NULL', - 20 => 'si_col smallint NULL DEFAULT NULL', - 21 => 'si_col_2 smallint NULL DEFAULT NULL', - 22 => 'bi bigint NULL DEFAULT NULL', - 23 => 'bi2 int8 NULL DEFAULT NULL', - 24 => 'int4_col int4 NULL DEFAULT NULL', - 25 => 'bigserial_col bigserial NOT NULL', - 26 => 'bigserial_col_2 serial8 NOT NULL', - 27 => 'int_col int NULL DEFAULT NULL', - 28 => 'int_col_2 integer NULL DEFAULT NULL', - 29 => 'numeric_col numeric NULL DEFAULT NULL', - 30 => 'numeric_col_2 numeric(10) NULL DEFAULT NULL', - 31 => 'numeric_col_3 numeric(10,2) NULL DEFAULT NULL', - 32 => 'double_p_2 double precision NULL DEFAULT NULL', - 33 => 'double_p_3 double precision NULL DEFAULT NULL', - 34 => 'real_col real NULL DEFAULT NULL', - 35 => 'float4_col float4 NULL DEFAULT NULL', - 36 => 'date_col date NULL DEFAULT NULL', - 37 => 'time_col time NULL DEFAULT NULL', - 38 => 'time_col_2 time with time zone NULL DEFAULT NULL', - 39 => 'time_col_3 time without time zone NULL DEFAULT NULL', - 40 => 'time_col_4 time(3) without time zone NULL DEFAULT NULL', - 41 => 'timetz_col timetz NULL DEFAULT NULL', - 42 => 'timetz_col_2 timetz(3) NULL DEFAULT NULL', - 43 => 'timestamp_col timestamp NULL DEFAULT NULL', - 44 => 'timestamp_col_2 timestamp with time zone NULL DEFAULT NULL', - 45 => 'timestamp_col_3 timestamp without time zone NULL DEFAULT NULL', - 46 => 'timestamp_col_4 timestamp(3) without time zone NULL DEFAULT NULL', - 47 => 'timestamptz_col timestamptz NULL DEFAULT NULL', - 48 => 'timestamptz_col_2 timestamptz(3) NULL DEFAULT NULL', - 49 => 'date2 date NULL DEFAULT NULL', - 50 => 'timestamp_col_z timestamp NULL DEFAULT NULL', - 51 => 'bit_varying bit varying NULL DEFAULT NULL', - 52 => 'bit_varying_n bit varying(8) NULL DEFAULT NULL', - 53 => 'bit_varying_n_2 varbit NULL DEFAULT NULL', - 54 => 'bit_varying_n_3 varbit(3) NULL DEFAULT NULL', - 55 => 'bool_col boolean NULL DEFAULT NULL', - 56 => 'bool_col_2 bool NULL DEFAULT NULL', - 57 => 'box_col box NULL DEFAULT NULL', - 58 => 'character_col character NULL DEFAULT NULL', - 59 => 'character_n character(12) NULL DEFAULT NULL', - 60 => 'character_varying character varying NULL DEFAULT NULL', - 61 => 'character_varying_n character varying(12) NULL DEFAULT NULL', - 62 => 'json_col json NOT NULL', - 63 => 'jsonb_col jsonb NOT NULL', - 64 => 'json_col_def json NOT NULL DEFAULT \'[]\'', - 65 => 'json_col_def_2 json NOT NULL DEFAULT \'[]\'', - 66 => 'bytea_def bytea NULL DEFAULT \'the bytea blob default\'', - 67 => 'text_def text NULL DEFAULT \'the text\'', - 68 => 'json_def json NOT NULL DEFAULT \'{"a":"b"}\'', - 69 => 'jsonb_def jsonb NOT NULL DEFAULT \'{"ba":"bb"}\'', - 70 => 'cidr_col cidr NULL DEFAULT NULL', - 71 => 'circle_col circle NULL DEFAULT NULL', - 72 => 'date_col_z date NULL DEFAULT NULL', - 73 => 'float8_col float8 NULL DEFAULT NULL', - 74 => 'inet_col inet NULL DEFAULT NULL', - 75 => 'interval_col interval NULL DEFAULT NULL', - 76 => 'interval_col_2 interval year NULL DEFAULT NULL', - 77 => 'interval_col_3 interval day to second(3) NULL DEFAULT NULL', - 78 => 'line_col line NULL DEFAULT NULL', - 79 => 'lseg_col lseg NULL DEFAULT NULL', - 80 => 'macaddr_col macaddr NULL DEFAULT NULL', - 81 => 'money_col money NULL DEFAULT NULL', - 82 => 'path_col path NULL DEFAULT NULL', - 83 => 'pg_lsn_col pg_lsn NULL DEFAULT NULL', - 84 => 'point_col point NULL DEFAULT NULL', - 85 => 'polygon_col polygon NULL DEFAULT NULL', - 86 => 'serial_col serial NOT NULL', - 87 => 'serial4_col serial4 NOT NULL', - 88 => 'tsquery_col tsquery NULL DEFAULT NULL', - 89 => 'tsvector_col tsvector NULL', - 90 => 'txid_snapshot_col txid_snapshot NULL DEFAULT NULL', - 91 => 'uuid_col uuid NULL DEFAULT NULL', - 92 => 'xml_col xml NULL DEFAULT NULL', + 0 => '"string_col" varchar NULL DEFAULT NULL', + 1 => '"varchar_col" varchar NULL DEFAULT NULL', + 2 => '"text_col" text NULL DEFAULT NULL', + 3 => '"text_col_array" text[] NULL DEFAULT NULL', + 4 => '"varchar_4_col" varchar(4) NULL DEFAULT NULL', + 5 => '"varchar_5_col" varchar(5) NULL DEFAULT NULL', + 6 => '"char_4_col" char(4) NULL DEFAULT NULL', + 7 => '"char_5_col" char NULL DEFAULT NULL', + 8 => '"char_6_col" char NOT NULL', + 9 => '"char_7_col" char(6) NOT NULL', + 10 => '"char_8_col" char NULL DEFAULT \'d\'', + 11 => '"decimal_col" decimal(12,3) NULL DEFAULT NULL', + 12 => '"bytea_col_2" bytea NULL DEFAULT NULL', + 13 => '"bit_col" bit NULL DEFAULT NULL', + 14 => '"bit_2" bit(1) NULL DEFAULT NULL', + 15 => '"bit_3" bit(64) NULL DEFAULT NULL', + 16 => '"ti" smallint NULL DEFAULT NULL', + 17 => '"int2_col" int2 NULL DEFAULT NULL', + 18 => '"smallserial_col" smallserial NOT NULL', + 19 => '"serial2_col" serial2 NOT NULL', + 20 => '"si_col" smallint NULL DEFAULT NULL', + 21 => '"si_col_2" smallint NULL DEFAULT NULL', + 22 => '"bi" bigint NULL DEFAULT NULL', + 23 => '"bi2" int8 NULL DEFAULT NULL', + 24 => '"int4_col" int4 NULL DEFAULT NULL', + 25 => '"bigserial_col" bigserial NOT NULL', + 26 => '"bigserial_col_2" serial8 NOT NULL', + 27 => '"int_col" int NULL DEFAULT NULL', + 28 => '"int_col_2" integer NULL DEFAULT NULL', + 29 => '"numeric_col" numeric NULL DEFAULT NULL', + 30 => '"numeric_col_2" numeric(10) NULL DEFAULT NULL', + 31 => '"numeric_col_3" numeric(10,2) NULL DEFAULT NULL', + 32 => '"double_p_2" double precision NULL DEFAULT NULL', + 33 => '"double_p_3" double precision NULL DEFAULT NULL', + 34 => '"real_col" real NULL DEFAULT NULL', + 35 => '"float4_col" float4 NULL DEFAULT NULL', + 36 => '"date_col" date NULL DEFAULT NULL', + 37 => '"time_col" time NULL DEFAULT NULL', + 38 => '"time_col_2" time with time zone NULL DEFAULT NULL', + 39 => '"time_col_3" time without time zone NULL DEFAULT NULL', + 40 => '"time_col_4" time(3) without time zone NULL DEFAULT NULL', + 41 => '"timetz_col" timetz NULL DEFAULT NULL', + 42 => '"timetz_col_2" timetz(3) NULL DEFAULT NULL', + 43 => '"timestamp_col" timestamp NULL DEFAULT NULL', + 44 => '"timestamp_col_2" timestamp with time zone NULL DEFAULT NULL', + 45 => '"timestamp_col_3" timestamp without time zone NULL DEFAULT NULL', + 46 => '"timestamp_col_4" timestamp(3) without time zone NULL DEFAULT NULL', + 47 => '"timestamptz_col" timestamptz NULL DEFAULT NULL', + 48 => '"timestamptz_col_2" timestamptz(3) NULL DEFAULT NULL', + 49 => '"date2" date NULL DEFAULT NULL', + 50 => '"timestamp_col_z" timestamp NULL DEFAULT NULL', + 51 => '"bit_varying" bit varying NULL DEFAULT NULL', + 52 => '"bit_varying_n" bit varying(8) NULL DEFAULT NULL', + 53 => '"bit_varying_n_2" varbit NULL DEFAULT NULL', + 54 => '"bit_varying_n_3" varbit(3) NULL DEFAULT NULL', + 55 => '"bool_col" boolean NULL DEFAULT NULL', + 56 => '"bool_col_2" bool NULL DEFAULT NULL', + 57 => '"box_col" box NULL DEFAULT NULL', + 58 => '"character_col" character NULL DEFAULT NULL', + 59 => '"character_n" character(12) NULL DEFAULT NULL', + 60 => '"character_varying" character varying NULL DEFAULT NULL', + 61 => '"character_varying_n" character varying(12) NULL DEFAULT NULL', + 62 => '"json_col" json NOT NULL', + 63 => '"jsonb_col" jsonb NOT NULL', + 64 => '"json_col_def" json NOT NULL DEFAULT \'[]\'', + 65 => '"json_col_def_2" json NOT NULL DEFAULT \'[]\'', + 66 => '"bytea_def" bytea NULL DEFAULT \'the bytea blob default\'', + 67 => '"text_def" text NULL DEFAULT \'the text\'', + 68 => '"json_def" json NOT NULL DEFAULT \'{"a":"b"}\'', + 69 => '"jsonb_def" jsonb NOT NULL DEFAULT \'{"ba":"bb"}\'', + 70 => '"cidr_col" cidr NULL DEFAULT NULL', + 71 => '"circle_col" circle NULL DEFAULT NULL', + 72 => '"date_col_z" date NULL DEFAULT NULL', + 73 => '"float8_col" float8 NULL DEFAULT NULL', + 74 => '"inet_col" inet NULL DEFAULT NULL', + 75 => '"interval_col" interval NULL DEFAULT NULL', + 76 => '"interval_col_2" interval year NULL DEFAULT NULL', + 77 => '"interval_col_3" interval day to second(3) NULL DEFAULT NULL', + 78 => '"line_col" line NULL DEFAULT NULL', + 79 => '"lseg_col" lseg NULL DEFAULT NULL', + 80 => '"macaddr_col" macaddr NULL DEFAULT NULL', + 81 => '"money_col" money NULL DEFAULT NULL', + 82 => '"path_col" path NULL DEFAULT NULL', + 83 => '"pg_lsn_col" pg_lsn NULL DEFAULT NULL', + 84 => '"point_col" point NULL DEFAULT NULL', + 85 => '"polygon_col" polygon NULL DEFAULT NULL', + 86 => '"serial_col" serial NOT NULL', + 87 => '"serial4_col" serial4 NOT NULL', + 88 => '"tsquery_col" tsquery NULL DEFAULT NULL', + 89 => '"tsvector_col" tsvector NULL', + 90 => '"txid_snapshot_col" txid_snapshot NULL DEFAULT NULL', + 91 => '"uuid_col" uuid NULL DEFAULT NULL', + 92 => '"xml_col" xml NULL DEFAULT NULL', ]); } diff --git a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php index cf34df76..b8f286b1 100644 --- a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php +++ b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php @@ -9,18 +9,18 @@ public function safeUp() { $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), - 0 => 'name varchar(254) NOT NULL DEFAULT \'Horse-2\'', + 0 => '"name" varchar(254) NOT NULL DEFAULT \'Horse-2\'', 'tag' => $this->text()->null()->defaultValue(null), - 1 => 'first_name varchar NULL DEFAULT NULL', + 1 => '"first_name" varchar NULL DEFAULT NULL', 'string_col' => $this->text()->null()->defaultValue(null), - 2 => 'dec_col decimal(12,2) NULL DEFAULT 3.14', - 3 => 'str_col_def varchar NOT NULL', - 4 => 'json_col text NOT NULL DEFAULT \'fox jumps over dog\'', - 5 => 'json_col_2 jsonb NOT NULL DEFAULT \'[]\'', - 6 => 'numeric_col double precision NULL DEFAULT NULL', - 7 => 'json_col_def_n json NOT NULL DEFAULT \'[]\'', - 8 => 'json_col_def_n_2 json NOT NULL DEFAULT \'[]\'', - 9 => 'text_col_array text[] NULL DEFAULT NULL', + 2 => '"dec_col" decimal(12,2) NULL DEFAULT 3.14', + 3 => '"str_col_def" varchar NOT NULL', + 4 => '"json_col" text NOT NULL DEFAULT \'fox jumps over dog\'', + 5 => '"json_col_2" jsonb NOT NULL DEFAULT \'[]\'', + 6 => '"numeric_col" double precision NULL DEFAULT NULL', + 7 => '"json_col_def_n" json NOT NULL DEFAULT \'[]\'', + 8 => '"json_col_def_n_2" json NOT NULL DEFAULT \'[]\'', + 9 => '"text_col_array" text[] NULL DEFAULT NULL', ]); } diff --git a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php index 3604dce0..4bd08302 100644 --- a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php +++ b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_newcolumns.php @@ -9,16 +9,16 @@ public function safeUp() { $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), - 0 => 'name varchar NOT NULL', - 1 => 'first_name varchar NULL DEFAULT NULL', + 0 => '"name" varchar NOT NULL', + 1 => '"first_name" varchar NULL DEFAULT NULL', 'last_name' => $this->text()->null()->defaultValue(null), - 2 => 'dec_col decimal(12,4) NULL DEFAULT NULL', - 3 => 'json_col json NOT NULL', - 4 => 'varchar_col varchar NULL DEFAULT NULL', - 5 => 'numeric_col double precision NULL DEFAULT NULL', - 6 => 'json_col_def_n json NOT NULL DEFAULT \'[]\'', - 7 => 'json_col_def_n_2 json NOT NULL DEFAULT \'[]\'', - 8 => 'text_col_array text[] NULL DEFAULT NULL', + 2 => '"dec_col" decimal(12,4) NULL DEFAULT NULL', + 3 => '"json_col" json NOT NULL', + 4 => '"varchar_col" varchar NULL DEFAULT NULL', + 5 => '"numeric_col" double precision NULL DEFAULT NULL', + 6 => '"json_col_def_n" json NOT NULL DEFAULT \'[]\'', + 7 => '"json_col_def_n_2" json NOT NULL DEFAULT \'[]\'', + 8 => '"text_col_array" text[] NULL DEFAULT NULL', ]); } diff --git a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php index ab2c1eab..a5ac94cd 100644 --- a/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/fresh/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php @@ -8,18 +8,18 @@ class m200000_000003_create_table_pristines extends \yii\db\Migration public function safeUp() { $this->createTable('{{%pristines}}', [ - 0 => 'custom_id_col serial primary key NOT NULL', - 1 => 'name text NOT NULL', + 0 => '"custom_id_col" serial primary key NOT NULL', + 1 => '"name" text NOT NULL', 'tag' => $this->text()->null()->defaultValue("4 leg"), - 2 => 'new_col varchar NULL DEFAULT NULL', - 3 => 'col_5 decimal(12,4) NULL DEFAULT NULL', - 4 => 'col_6 decimal(11,2) NULL DEFAULT NULL', - 5 => 'col_7 decimal(10,2) NULL DEFAULT NULL', - 6 => 'col_8 json NOT NULL', - 7 => 'col_9 varchar NULL DEFAULT NULL', - 8 => 'col_10 varchar NULL DEFAULT NULL', - 9 => 'col_11 text NULL DEFAULT NULL', - 10 => 'price decimal(10,2) NULL DEFAULT 0', + 2 => '"new_col" varchar NULL DEFAULT NULL', + 3 => '"col_5" decimal(12,4) NULL DEFAULT NULL', + 4 => '"col_6" decimal(11,2) NULL DEFAULT NULL', + 5 => '"col_7" decimal(10,2) NULL DEFAULT NULL', + 6 => '"col_8" json NOT NULL', + 7 => '"col_9" varchar NULL DEFAULT NULL', + 8 => '"col_10" varchar NULL DEFAULT NULL', + 9 => '"col_11" text NULL DEFAULT NULL', + 10 => '"price" decimal(10,2) NULL DEFAULT 0', ]); } diff --git a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php index 53498798..e0cac52a 100644 --- a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php +++ b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_alldbdatatypes.php @@ -9,99 +9,99 @@ public function safeUp() { $this->createTable('{{%alldbdatatypes}}', [ 'id' => $this->bigPrimaryKey(), - 0 => 'string_col varchar NULL DEFAULT NULL', - 1 => 'varchar_col varchar NULL DEFAULT NULL', - 2 => 'text_col text NULL DEFAULT NULL', - 3 => 'text_col_array text[] NULL DEFAULT NULL', - 4 => 'varchar_4_col varchar(4) NULL DEFAULT NULL', - 5 => 'varchar_5_col varchar(5) NULL DEFAULT NULL', - 6 => 'char_4_col char(4) NULL DEFAULT NULL', - 7 => 'char_5_col char NULL DEFAULT NULL', - 8 => 'char_6_col char NOT NULL', - 9 => 'char_7_col char(6) NOT NULL', - 10 => 'char_8_col char NULL DEFAULT \'d\'', - 11 => 'decimal_col decimal(12,3) NULL DEFAULT NULL', - 12 => 'bytea_col_2 bytea NULL DEFAULT NULL', - 13 => 'bit_col bit NULL DEFAULT NULL', - 14 => 'bit_2 bit(1) NULL DEFAULT NULL', - 15 => 'bit_3 bit(64) NULL DEFAULT NULL', - 16 => 'ti smallint NULL DEFAULT NULL', - 17 => 'int2_col int2 NULL DEFAULT NULL', - 18 => 'smallserial_col smallserial NOT NULL', - 19 => 'serial2_col serial2 NOT NULL', - 20 => 'si_col smallint NULL DEFAULT NULL', - 21 => 'si_col_2 smallint NULL DEFAULT NULL', - 22 => 'bi bigint NULL DEFAULT NULL', - 23 => 'bi2 int8 NULL DEFAULT NULL', - 24 => 'int4_col int4 NULL DEFAULT NULL', - 25 => 'bigserial_col bigserial NOT NULL', - 26 => 'bigserial_col_2 serial8 NOT NULL', - 27 => 'int_col int NULL DEFAULT NULL', - 28 => 'int_col_2 integer NULL DEFAULT NULL', - 29 => 'numeric_col numeric NULL DEFAULT NULL', - 30 => 'numeric_col_2 numeric(10) NULL DEFAULT NULL', - 31 => 'numeric_col_3 numeric(10,2) NULL DEFAULT NULL', - 32 => 'double_p_2 double precision NULL DEFAULT NULL', - 33 => 'double_p_3 double precision NULL DEFAULT NULL', - 34 => 'real_col real NULL DEFAULT NULL', - 35 => 'float4_col float4 NULL DEFAULT NULL', - 36 => 'date_col date NULL DEFAULT NULL', - 37 => 'time_col time NULL DEFAULT NULL', - 38 => 'time_col_2 time with time zone NULL DEFAULT NULL', - 39 => 'time_col_3 time without time zone NULL DEFAULT NULL', - 40 => 'time_col_4 time(3) without time zone NULL DEFAULT NULL', - 41 => 'timetz_col timetz NULL DEFAULT NULL', - 42 => 'timetz_col_2 timetz(3) NULL DEFAULT NULL', - 43 => 'timestamp_col timestamp NULL DEFAULT NULL', - 44 => 'timestamp_col_2 timestamp with time zone NULL DEFAULT NULL', - 45 => 'timestamp_col_3 timestamp without time zone NULL DEFAULT NULL', - 46 => 'timestamp_col_4 timestamp(3) without time zone NULL DEFAULT NULL', - 47 => 'timestamptz_col timestamptz NULL DEFAULT NULL', - 48 => 'timestamptz_col_2 timestamptz(3) NULL DEFAULT NULL', - 49 => 'date2 date NULL DEFAULT NULL', - 50 => 'timestamp_col_z timestamp NULL DEFAULT NULL', - 51 => 'bit_varying bit varying NULL DEFAULT NULL', - 52 => 'bit_varying_n bit varying(8) NULL DEFAULT NULL', - 53 => 'bit_varying_n_2 varbit NULL DEFAULT NULL', - 54 => 'bit_varying_n_3 varbit(3) NULL DEFAULT NULL', - 55 => 'bool_col boolean NULL DEFAULT NULL', - 56 => 'bool_col_2 bool NULL DEFAULT NULL', - 57 => 'box_col box NULL DEFAULT NULL', - 58 => 'character_col character NULL DEFAULT NULL', - 59 => 'character_n character(12) NULL DEFAULT NULL', - 60 => 'character_varying character varying NULL DEFAULT NULL', - 61 => 'character_varying_n character varying(12) NULL DEFAULT NULL', - 62 => 'json_col json NOT NULL', - 63 => 'jsonb_col jsonb NOT NULL', - 64 => 'json_col_def json NOT NULL DEFAULT \'[]\'', - 65 => 'json_col_def_2 json NOT NULL DEFAULT \'[]\'', - 66 => 'bytea_def bytea NULL DEFAULT \'the bytea blob default\'', - 67 => 'text_def text NULL DEFAULT \'the text\'', - 68 => 'json_def json NOT NULL DEFAULT \'{"a":"b"}\'', - 69 => 'jsonb_def jsonb NOT NULL DEFAULT \'{"ba":"bb"}\'', - 70 => 'cidr_col cidr NULL DEFAULT NULL', - 71 => 'circle_col circle NULL DEFAULT NULL', - 72 => 'date_col_z date NULL DEFAULT NULL', - 73 => 'float8_col float8 NULL DEFAULT NULL', - 74 => 'inet_col inet NULL DEFAULT NULL', - 75 => 'interval_col interval NULL DEFAULT NULL', - 76 => 'interval_col_2 interval year NULL DEFAULT NULL', - 77 => 'interval_col_3 interval day to second(3) NULL DEFAULT NULL', - 78 => 'line_col line NULL DEFAULT NULL', - 79 => 'lseg_col lseg NULL DEFAULT NULL', - 80 => 'macaddr_col macaddr NULL DEFAULT NULL', - 81 => 'money_col money NULL DEFAULT NULL', - 82 => 'path_col path NULL DEFAULT NULL', - 83 => 'pg_lsn_col pg_lsn NULL DEFAULT NULL', - 84 => 'point_col point NULL DEFAULT NULL', - 85 => 'polygon_col polygon NULL DEFAULT NULL', - 86 => 'serial_col serial NOT NULL', - 87 => 'serial4_col serial4 NOT NULL', - 88 => 'tsquery_col tsquery NULL DEFAULT NULL', - 89 => 'tsvector_col tsvector NULL', - 90 => 'txid_snapshot_col txid_snapshot NULL DEFAULT NULL', - 91 => 'uuid_col uuid NULL DEFAULT NULL', - 92 => 'xml_col xml NULL DEFAULT NULL', + 0 => '"string_col" varchar NULL DEFAULT NULL', + 1 => '"varchar_col" varchar NULL DEFAULT NULL', + 2 => '"text_col" text NULL DEFAULT NULL', + 3 => '"text_col_array" text[] NULL DEFAULT NULL', + 4 => '"varchar_4_col" varchar(4) NULL DEFAULT NULL', + 5 => '"varchar_5_col" varchar(5) NULL DEFAULT NULL', + 6 => '"char_4_col" char(4) NULL DEFAULT NULL', + 7 => '"char_5_col" char NULL DEFAULT NULL', + 8 => '"char_6_col" char NOT NULL', + 9 => '"char_7_col" char(6) NOT NULL', + 10 => '"char_8_col" char NULL DEFAULT \'d\'', + 11 => '"decimal_col" decimal(12,3) NULL DEFAULT NULL', + 12 => '"bytea_col_2" bytea NULL DEFAULT NULL', + 13 => '"bit_col" bit NULL DEFAULT NULL', + 14 => '"bit_2" bit(1) NULL DEFAULT NULL', + 15 => '"bit_3" bit(64) NULL DEFAULT NULL', + 16 => '"ti" smallint NULL DEFAULT NULL', + 17 => '"int2_col" int2 NULL DEFAULT NULL', + 18 => '"smallserial_col" smallserial NOT NULL', + 19 => '"serial2_col" serial2 NOT NULL', + 20 => '"si_col" smallint NULL DEFAULT NULL', + 21 => '"si_col_2" smallint NULL DEFAULT NULL', + 22 => '"bi" bigint NULL DEFAULT NULL', + 23 => '"bi2" int8 NULL DEFAULT NULL', + 24 => '"int4_col" int4 NULL DEFAULT NULL', + 25 => '"bigserial_col" bigserial NOT NULL', + 26 => '"bigserial_col_2" serial8 NOT NULL', + 27 => '"int_col" int NULL DEFAULT NULL', + 28 => '"int_col_2" integer NULL DEFAULT NULL', + 29 => '"numeric_col" numeric NULL DEFAULT NULL', + 30 => '"numeric_col_2" numeric(10) NULL DEFAULT NULL', + 31 => '"numeric_col_3" numeric(10,2) NULL DEFAULT NULL', + 32 => '"double_p_2" double precision NULL DEFAULT NULL', + 33 => '"double_p_3" double precision NULL DEFAULT NULL', + 34 => '"real_col" real NULL DEFAULT NULL', + 35 => '"float4_col" float4 NULL DEFAULT NULL', + 36 => '"date_col" date NULL DEFAULT NULL', + 37 => '"time_col" time NULL DEFAULT NULL', + 38 => '"time_col_2" time with time zone NULL DEFAULT NULL', + 39 => '"time_col_3" time without time zone NULL DEFAULT NULL', + 40 => '"time_col_4" time(3) without time zone NULL DEFAULT NULL', + 41 => '"timetz_col" timetz NULL DEFAULT NULL', + 42 => '"timetz_col_2" timetz(3) NULL DEFAULT NULL', + 43 => '"timestamp_col" timestamp NULL DEFAULT NULL', + 44 => '"timestamp_col_2" timestamp with time zone NULL DEFAULT NULL', + 45 => '"timestamp_col_3" timestamp without time zone NULL DEFAULT NULL', + 46 => '"timestamp_col_4" timestamp(3) without time zone NULL DEFAULT NULL', + 47 => '"timestamptz_col" timestamptz NULL DEFAULT NULL', + 48 => '"timestamptz_col_2" timestamptz(3) NULL DEFAULT NULL', + 49 => '"date2" date NULL DEFAULT NULL', + 50 => '"timestamp_col_z" timestamp NULL DEFAULT NULL', + 51 => '"bit_varying" bit varying NULL DEFAULT NULL', + 52 => '"bit_varying_n" bit varying(8) NULL DEFAULT NULL', + 53 => '"bit_varying_n_2" varbit NULL DEFAULT NULL', + 54 => '"bit_varying_n_3" varbit(3) NULL DEFAULT NULL', + 55 => '"bool_col" boolean NULL DEFAULT NULL', + 56 => '"bool_col_2" bool NULL DEFAULT NULL', + 57 => '"box_col" box NULL DEFAULT NULL', + 58 => '"character_col" character NULL DEFAULT NULL', + 59 => '"character_n" character(12) NULL DEFAULT NULL', + 60 => '"character_varying" character varying NULL DEFAULT NULL', + 61 => '"character_varying_n" character varying(12) NULL DEFAULT NULL', + 62 => '"json_col" json NOT NULL', + 63 => '"jsonb_col" jsonb NOT NULL', + 64 => '"json_col_def" json NOT NULL DEFAULT \'[]\'', + 65 => '"json_col_def_2" json NOT NULL DEFAULT \'[]\'', + 66 => '"bytea_def" bytea NULL DEFAULT \'the bytea blob default\'', + 67 => '"text_def" text NULL DEFAULT \'the text\'', + 68 => '"json_def" json NOT NULL DEFAULT \'{"a":"b"}\'', + 69 => '"jsonb_def" jsonb NOT NULL DEFAULT \'{"ba":"bb"}\'', + 70 => '"cidr_col" cidr NULL DEFAULT NULL', + 71 => '"circle_col" circle NULL DEFAULT NULL', + 72 => '"date_col_z" date NULL DEFAULT NULL', + 73 => '"float8_col" float8 NULL DEFAULT NULL', + 74 => '"inet_col" inet NULL DEFAULT NULL', + 75 => '"interval_col" interval NULL DEFAULT NULL', + 76 => '"interval_col_2" interval year NULL DEFAULT NULL', + 77 => '"interval_col_3" interval day to second(3) NULL DEFAULT NULL', + 78 => '"line_col" line NULL DEFAULT NULL', + 79 => '"lseg_col" lseg NULL DEFAULT NULL', + 80 => '"macaddr_col" macaddr NULL DEFAULT NULL', + 81 => '"money_col" money NULL DEFAULT NULL', + 82 => '"path_col" path NULL DEFAULT NULL', + 83 => '"pg_lsn_col" pg_lsn NULL DEFAULT NULL', + 84 => '"point_col" point NULL DEFAULT NULL', + 85 => '"polygon_col" polygon NULL DEFAULT NULL', + 86 => '"serial_col" serial NOT NULL', + 87 => '"serial4_col" serial4 NOT NULL', + 88 => '"tsquery_col" tsquery NULL DEFAULT NULL', + 89 => '"tsvector_col" tsvector NULL', + 90 => '"txid_snapshot_col" txid_snapshot NULL DEFAULT NULL', + 91 => '"uuid_col" uuid NULL DEFAULT NULL', + 92 => '"xml_col" xml NULL DEFAULT NULL', ]); } diff --git a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php index cf34df76..b8f286b1 100644 --- a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php +++ b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_editcolumns.php @@ -9,18 +9,18 @@ public function safeUp() { $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), - 0 => 'name varchar(254) NOT NULL DEFAULT \'Horse-2\'', + 0 => '"name" varchar(254) NOT NULL DEFAULT \'Horse-2\'', 'tag' => $this->text()->null()->defaultValue(null), - 1 => 'first_name varchar NULL DEFAULT NULL', + 1 => '"first_name" varchar NULL DEFAULT NULL', 'string_col' => $this->text()->null()->defaultValue(null), - 2 => 'dec_col decimal(12,2) NULL DEFAULT 3.14', - 3 => 'str_col_def varchar NOT NULL', - 4 => 'json_col text NOT NULL DEFAULT \'fox jumps over dog\'', - 5 => 'json_col_2 jsonb NOT NULL DEFAULT \'[]\'', - 6 => 'numeric_col double precision NULL DEFAULT NULL', - 7 => 'json_col_def_n json NOT NULL DEFAULT \'[]\'', - 8 => 'json_col_def_n_2 json NOT NULL DEFAULT \'[]\'', - 9 => 'text_col_array text[] NULL DEFAULT NULL', + 2 => '"dec_col" decimal(12,2) NULL DEFAULT 3.14', + 3 => '"str_col_def" varchar NOT NULL', + 4 => '"json_col" text NOT NULL DEFAULT \'fox jumps over dog\'', + 5 => '"json_col_2" jsonb NOT NULL DEFAULT \'[]\'', + 6 => '"numeric_col" double precision NULL DEFAULT NULL', + 7 => '"json_col_def_n" json NOT NULL DEFAULT \'[]\'', + 8 => '"json_col_def_n_2" json NOT NULL DEFAULT \'[]\'', + 9 => '"text_col_array" text[] NULL DEFAULT NULL', ]); } diff --git a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_change_table_newcolumns.php b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_change_table_newcolumns.php index 72747e62..aab2451b 100644 --- a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_change_table_newcolumns.php +++ b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_change_table_newcolumns.php @@ -7,15 +7,15 @@ class m200000_000002_change_table_newcolumns extends \yii\db\Migration { public function safeUp() { - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN dec_col decimal(12,4) NULL DEFAULT NULL')->execute(); - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN first_name varchar NULL DEFAULT NULL')->execute(); - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN json_col json NOT NULL')->execute(); - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN json_col_def_n json NOT NULL DEFAULT \'[]\'')->execute(); - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN json_col_def_n_2 json NOT NULL DEFAULT \'[]\'')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "dec_col" decimal(12,4) NULL DEFAULT NULL')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "first_name" varchar NULL DEFAULT NULL')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "json_col" json NOT NULL')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "json_col_def_n" json NOT NULL DEFAULT \'[]\'')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "json_col_def_n_2" json NOT NULL DEFAULT \'[]\'')->execute(); $this->addColumn('{{%newcolumns}}', 'last_name', $this->text()->null()->defaultValue(null)); - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN numeric_col double precision NULL DEFAULT NULL')->execute(); - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN text_col_array text[] NULL DEFAULT NULL')->execute(); - $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN varchar_col varchar NULL DEFAULT NULL')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "numeric_col" double precision NULL DEFAULT NULL')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "text_col_array" text[] NULL DEFAULT NULL')->execute(); + $this->db->createCommand('ALTER TABLE {{%newcolumns}} ADD COLUMN "varchar_col" varchar NULL DEFAULT NULL')->execute(); } public function safeDown() diff --git a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php index ab2c1eab..a5ac94cd 100644 --- a/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php +++ b/tests/specs/x_db_type/new_column/pgsql/app/migrations_pgsql_db/m200000_000003_create_table_pristines.php @@ -8,18 +8,18 @@ class m200000_000003_create_table_pristines extends \yii\db\Migration public function safeUp() { $this->createTable('{{%pristines}}', [ - 0 => 'custom_id_col serial primary key NOT NULL', - 1 => 'name text NOT NULL', + 0 => '"custom_id_col" serial primary key NOT NULL', + 1 => '"name" text NOT NULL', 'tag' => $this->text()->null()->defaultValue("4 leg"), - 2 => 'new_col varchar NULL DEFAULT NULL', - 3 => 'col_5 decimal(12,4) NULL DEFAULT NULL', - 4 => 'col_6 decimal(11,2) NULL DEFAULT NULL', - 5 => 'col_7 decimal(10,2) NULL DEFAULT NULL', - 6 => 'col_8 json NOT NULL', - 7 => 'col_9 varchar NULL DEFAULT NULL', - 8 => 'col_10 varchar NULL DEFAULT NULL', - 9 => 'col_11 text NULL DEFAULT NULL', - 10 => 'price decimal(10,2) NULL DEFAULT 0', + 2 => '"new_col" varchar NULL DEFAULT NULL', + 3 => '"col_5" decimal(12,4) NULL DEFAULT NULL', + 4 => '"col_6" decimal(11,2) NULL DEFAULT NULL', + 5 => '"col_7" decimal(10,2) NULL DEFAULT NULL', + 6 => '"col_8" json NOT NULL', + 7 => '"col_9" varchar NULL DEFAULT NULL', + 8 => '"col_10" varchar NULL DEFAULT NULL', + 9 => '"col_11" text NULL DEFAULT NULL', + 10 => '"price" decimal(10,2) NULL DEFAULT 0', ]); } From 2f58a9edb2c178f08945f00557dc9ea5fa01efba Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 2 Jan 2023 22:55:06 +0530 Subject: [PATCH 53/62] Fix quote issue in enum in PgSQL --- src/lib/migrations/BaseMigrationBuilder.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 4e902083..87753619 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -417,6 +417,9 @@ protected function isNeedUsingExpression(string $fromType, string $toType):bool public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema { $tableName = 'tmp_table_'; + $tmpEnumName = function (string $columnName): string { + return 'tmp_enum_'.$columnName.'_'; + }; Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); @@ -424,12 +427,12 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): $name = MigrationRecordBuilder::quote($columnSchema->name); $column = [$name.' '.$this->newColStr($columnSchema)]; if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - $column = strtr($column, ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']); + $column = strtr($column, ['enum_'.$columnSchema->name => $tmpEnumName($columnSchema->name)]); } } else { $column = [$columnSchema->name => $this->newColStr($columnSchema)]; if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - $column[$columnSchema->name] = strtr($column[$columnSchema->name], ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']); + $column[$columnSchema->name] = strtr($column[$columnSchema->name], ['enum_'.$columnSchema->name => $tmpEnumName($columnSchema->name)]); } } @@ -440,7 +443,7 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): return "'$aValue'"; }, $allEnumValues); Yii::$app->db->createCommand( - 'CREATE TYPE tmp_enum_'.$columnSchema->name.'_ AS ENUM('.implode(', ', $allEnumValues).')' + 'CREATE TYPE '.$tmpEnumName($columnSchema->name).' AS ENUM('.implode(', ', $allEnumValues).')' )->execute(); } @@ -451,9 +454,9 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): Yii::$app->db->createCommand()->dropTable($tableName)->execute(); if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum - Yii::$app->db->createCommand('DROP TYPE tmp_enum_'.$columnSchema->name.'_')->execute(); - if ($table->columns[$columnSchema->name]->dbType !== 'tmp_enum_'.$columnSchema->name.'_') { - throw new \Exception('Unknown error related to PgSQL enum'); + Yii::$app->db->createCommand('DROP TYPE '.$tmpEnumName($columnSchema->name))->execute(); + if ($table->columns[$columnSchema->name]->dbType !== $tmpEnumName($columnSchema->name)) { + throw new \Exception('Unknown error related to PgSQL enum '.$table->columns[$columnSchema->name]->dbType); } // reset back column enum name to original as we are comparing with current // e.g. we get different enum type name such as `enum_status` and `tmp_enum_status_` even there is no change, so below statement fix this issue From e8f1df1d8ad60dd5f2eff8379157ada441defc50 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 2 Jan 2023 23:03:52 +0530 Subject: [PATCH 54/62] Fix quote issue in enum in PgSQL 2 --- src/lib/migrations/BaseMigrationBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 87753619..b0f8475f 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -418,7 +418,7 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): { $tableName = 'tmp_table_'; $tmpEnumName = function (string $columnName): string { - return 'tmp_enum_'.$columnName.'_'; + return '"tmp_enum_'.$columnName.'_"'; }; Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); @@ -455,7 +455,7 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum Yii::$app->db->createCommand('DROP TYPE '.$tmpEnumName($columnSchema->name))->execute(); - if ($table->columns[$columnSchema->name]->dbType !== $tmpEnumName($columnSchema->name)) { + if ('"'.$table->columns[$columnSchema->name]->dbType.'"' !== $tmpEnumName($columnSchema->name)) { throw new \Exception('Unknown error related to PgSQL enum '.$table->columns[$columnSchema->name]->dbType); } // reset back column enum name to original as we are comparing with current From a6c20ef403474938d06c8facbc580a0bba1c1822 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Jan 2023 12:44:58 +0530 Subject: [PATCH 55/62] Fix failing tests --- .../m200000_000001_change_table_editcolumns.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 5e6e548e..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,16 +11,16 @@ 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', 'text NULL USING "string_col"::text'); From 3fff89a4877dbe40ca946af6cb1fa2252c051126 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Jan 2023 13:22:01 +0530 Subject: [PATCH 56/62] Fix https://github.com/cebe/yii2-openapi/issues/107 --- tests/DbTestCase.php | 5 ++-- .../m200000_000000_change_table_fruits.php | 19 ++++++++++++ .../mysql/no_syntax_error_107.yaml | 2 +- tests/unit/IssueFixTest.php | 30 +++++++------------ 4 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 tests/specs/issue_fix/no_syntax_error_107/mysql/app/migrations_mysql_db/m200000_000000_change_table_fruits.php diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index 33f914b6..9f2b8dd9 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -113,8 +113,9 @@ protected function runActualMigrations(string $db = 'mysql', int $number = 2): v $lastThird = count($upOutput) - 3; $this->assertSame($upExitCode, 0); $this->assertSame($upOutput[$last], 'Migrated up successfully.'); - $this->assertSame($upOutput[$lastThird], $number.' migrations were applied.'); + $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); @@ -122,7 +123,7 @@ protected function runActualMigrations(string $db = 'mysql', int $number = 2): v $lastThird = count($downOutput) - 3; $this->assertSame($downExitCode, 0); $this->assertSame($downOutput[$last], 'Migrated down successfully.'); - $this->assertSame($downOutput[$lastThird], $number.' migrations were reverted.'); + $this->assertSame($downOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' reverted.'); } } 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.yaml b/tests/specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.yaml index d91e8904..cf06addd 100644 --- 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 @@ -29,7 +29,7 @@ components: # default: '{}' type: array - x-db-type: text[] + x-db-type: text nullable: false # default: '{}' diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 86944ed8..4e0cb966 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -9,32 +9,22 @@ // This class contains tests for various issues present at GitHub class IssueFixTest extends DbTestCase { - // TODO WIP resume from here // fix https://github.com/cebe/yii2-openapi/issues/107 // 107_no_syntax_error public function testMigrationsAreNotGeneratedWithSyntaxError() { - // $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php"); - // $this->runGenerator($testFile, 'mysql'); - // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ - // 'recursive' => true, - // ]); - // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/id_not_in_rules/app"), [ - // 'recursive' => true, - // ]); - // $this->checkFiles($actualFiles, $expectedFiles); - - // $this->changeDbToMariadb(); - // $this->deleteTablesForNoSyntaxError107(); - // $this->createTableForNoSyntaxError107(); - // $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php"); - // $this->runGenerator($testFile, 'maria'); - - $this->changeDbToPgsql(); + $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php"); $this->deleteTablesForNoSyntaxError107(); $this->createTableForNoSyntaxError107(); - $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php"); - $this->runGenerator($testFile, 'pgsql'); + $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() From 8aa5697bac2ae088e901fb6a1bab135fb36bbc0d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Jan 2023 13:29:37 +0530 Subject: [PATCH 57/62] Initial commit of this branch to create PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 107df33f..9d20b16a 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From dcbde27ae9f367f14f8c90b62e83cfc2c425323f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Jan 2023 15:22:49 +0530 Subject: [PATCH 58/62] Add tests --- README.md | 1 - tests/DbTestCase.php | 2 +- .../camel_case_127/camel_case_127.php | 13 ++ .../camel_case_127/camel_case_127.yaml | 28 ++++ .../m200000_000000_create_table_pristines.php | 21 +++ .../m200000_000000_create_table_pristines.php | 21 +++ .../m200000_000000_change_table_fruits.php | 17 +++ .../pgsql/app/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../pgsql/app/models/Fruit.php | 10 ++ .../pgsql/app/models/FruitFaker.php | 41 +++++ .../pgsql/app/models/base/Fruit.php | 26 ++++ .../pgsql/quote_in_alter_table.php | 13 ++ .../pgsql/quote_in_alter_table.yaml | 26 ++++ tests/unit/IssueFixTest.php | 59 +++++++ 14 files changed, 420 insertions(+), 2 deletions(-) create mode 100644 tests/specs/issue_fix/camel_case_127/camel_case_127.php create mode 100644 tests/specs/issue_fix/camel_case_127/camel_case_127.yaml create mode 100644 tests/specs/issue_fix/camel_case_127/mysql/app/migrations_mysql_db/m200000_000000_create_table_pristines.php create mode 100644 tests/specs/issue_fix/camel_case_127/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_pristines.php create mode 100644 tests/specs/issue_fix/quote_in_alter_table/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_fruits.php create mode 100644 tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/BaseModelFaker.php create mode 100644 tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/Fruit.php create mode 100644 tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/FruitFaker.php create mode 100644 tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/base/Fruit.php create mode 100644 tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.php create mode 100644 tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.yaml diff --git a/README.md b/README.md index 9d20b16a..107df33f 100644 --- a/README.md +++ b/README.md @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index 9f2b8dd9..bbbdd281 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -8,7 +8,7 @@ use yii\db\mysql\Schema as MySqlSchema; use yii\db\pgsql\Schema as PgSqlSchema; use \SamIT\Yii2\MariaDb\Schema as MariaDbSchema; -use yii\helpers\ArrayHelper; +use yii\helpers\{ArrayHelper, VarDumper}; use yii\helpers\FileHelper; class DbTestCase extends \PHPUnit\Framework\TestCase diff --git a/tests/specs/issue_fix/camel_case_127/camel_case_127.php b/tests/specs/issue_fix/camel_case_127/camel_case_127.php new file mode 100644 index 00000000..4bce1f41 --- /dev/null +++ b/tests/specs/issue_fix/camel_case_127/camel_case_127.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/camel_case_127/camel_case_127.yaml', + 'generateUrls' => false, + 'generateModels' => false, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => false, +]; diff --git a/tests/specs/issue_fix/camel_case_127/camel_case_127.yaml b/tests/specs/issue_fix/camel_case_127/camel_case_127.yaml new file mode 100644 index 00000000..c904c7d3 --- /dev/null +++ b/tests/specs/issue_fix/camel_case_127/camel_case_127.yaml @@ -0,0 +1,28 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Camel case column name issue \#127 +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Pristine: + type: object + description: Camel Case column name test + required: + - id + properties: + id: + type: integer + firstName: + type: string + newColumn: + type: string + x-db-type: varchar(255) diff --git a/tests/specs/issue_fix/camel_case_127/mysql/app/migrations_mysql_db/m200000_000000_create_table_pristines.php b/tests/specs/issue_fix/camel_case_127/mysql/app/migrations_mysql_db/m200000_000000_create_table_pristines.php new file mode 100644 index 00000000..a99836ac --- /dev/null +++ b/tests/specs/issue_fix/camel_case_127/mysql/app/migrations_mysql_db/m200000_000000_create_table_pristines.php @@ -0,0 +1,21 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'firstName' => $this->text()->null(), + 0 => 'newColumn varchar(255) NULL DEFAULT NULL', + ]); + } + + public function down() + { + $this->dropTable('{{%pristines}}'); + } +} diff --git a/tests/specs/issue_fix/camel_case_127/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_pristines.php b/tests/specs/issue_fix/camel_case_127/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_pristines.php new file mode 100644 index 00000000..8b22758a --- /dev/null +++ b/tests/specs/issue_fix/camel_case_127/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_pristines.php @@ -0,0 +1,21 @@ +createTable('{{%pristines}}', [ + 'id' => $this->primaryKey(), + 'firstName' => $this->text()->null()->defaultValue(null), + 0 => '"newColumn" varchar(255) NULL DEFAULT NULL', + ]); + } + + public function safeDown() + { + $this->dropTable('{{%pristines}}'); + } +} diff --git a/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_fruits.php b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_fruits.php new file mode 100644 index 00000000..0472aa35 --- /dev/null +++ b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_fruits.php @@ -0,0 +1,17 @@ +alterColumn('{{%fruits}}', 'colourName', 'text NULL USING "colourName"::text'); + } + + public function safeDown() + { + $this->alterColumn('{{%fruits}}', 'colourName', 'varchar(255) NULL USING "colourName"::varchar'); + } +} diff --git a/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/BaseModelFaker.php b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/BaseModelFaker.php @@ -0,0 +1,144 @@ +faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language)); + $this->uniqueFaker = new UniqueGenerator($this->faker); + } + + abstract public function generateModel($attributes = []); + + public function getFaker():Generator + { + return $this->faker; + } + + public function getUniqueFaker():UniqueGenerator + { + return $this->uniqueFaker; + } + + public function setFaker(Generator $faker):void + { + $this->faker = $faker; + } + + public function setUniqueFaker(UniqueGenerator $faker):void + { + $this->uniqueFaker = $faker; + } + + /** + * Generate and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::makeOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + $model = $fakeBuilder->generateModel($attributes); + return $model; + } + + /** + * Generate, save and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::saveOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $model = static::makeOne($attributes, $uniqueFaker); + $model->save(); + return $model; + } + + /** + * Generate and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + return $model; + }, range(0, $number -1)); + } + + /** + * Generate, save and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + $model->save(); + return $model; + }, range(0, $number -1)); + } +} diff --git a/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/Fruit.php b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/Fruit.php new file mode 100644 index 00000000..c74c53d9 --- /dev/null +++ b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/Fruit.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new Fruit(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->colourName = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/base/Fruit.php b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/base/Fruit.php new file mode 100644 index 00000000..51d59253 --- /dev/null +++ b/tests/specs/issue_fix/quote_in_alter_table/pgsql/app/models/base/Fruit.php @@ -0,0 +1,26 @@ + [['colourName'], 'trim'], + 'colourName_string' => [['colourName'], 'string'], + ]; + } +} diff --git a/tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.php b/tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.php new file mode 100644 index 00000000..53efa3df --- /dev/null +++ b/tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => true, +]; diff --git a/tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.yaml b/tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.yaml new file mode 100644 index 00000000..3c26bd47 --- /dev/null +++ b/tests/specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.yaml @@ -0,0 +1,26 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Quote in alter column in Pgsql +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information + +components: + schemas: + Fruit: + type: object + description: Quote in alter column in Pgsql test + required: + - id + properties: + id: + type: integer + colourName: + type: string + # x-db-type: pg_lsn diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 4e0cb966..fce08548 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -68,4 +68,63 @@ private function createTableForFloatIssue() 'vat_percent' => 'float default 0', ])->execute(); } + + public function testCamelCaseColumnNameIssue127() + { + $testFile = Yii::getAlias("@specs/issue_fix/camel_case_127/camel_case_127.php"); + $this->runGenerator($testFile, 'mysql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_pgsql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/camel_case_127/mysql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('mysql', 1); + + $this->changeDbToPgsql(); + $testFile = Yii::getAlias("@specs/issue_fix/camel_case_127/camel_case_127.php"); + $this->runGenerator($testFile, 'pgsql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/camel_case_127/pgsql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 1); + } + + public function testQuoteInAlterColumn() + { + $this->changeDbToPgsql(); + $this->deleteTableForQuoteInAlterColumn(); + $this->createTableForQuoteInAlterColumn(); + $testFile = Yii::getAlias("@specs/issue_fix/quote_in_alter_table/pgsql/quote_in_alter_table.php"); + $this->runGenerator($testFile, 'pgsql'); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/quote_in_alter_table/pgsql/app"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations('pgsql', 1); + } + + private function deleteTableForQuoteInAlterColumn() + { + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute(); + } + + private function createTableForQuoteInAlterColumn() + { + Yii::$app->db->createCommand()->createTable('{{%fruits}}', [ + 'id' => 'pk', + // 'colourName' => 'circle', + 'colourName' => 'varchar(255)', + ])->execute(); + } } From 123e67afb49dc2666be02e525fdb31a8737584e5 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Jan 2023 16:25:15 +0530 Subject: [PATCH 59/62] Implement quote in enum type names --- src/lib/ColumnToCode.php | 2 +- src/lib/migrations/MigrationRecordBuilder.php | 4 ++-- .../m200000_000000_change_table_v2_posts.php | 4 ++-- .../m200000_000001_create_table_v2_tags.php | 4 ++-- .../m200000_000004_change_table_v2_users.php | 6 +++--- .../m200000_000000_change_table_editcolumns.php | 12 ++++++------ .../m200000_000001_create_table_newcolumns.php | 4 ++-- .../m200000_000002_create_table_pristines.php | 4 ++-- .../m200000_000000_create_table_editcolumns.php | 4 ++-- .../m200000_000001_create_table_newcolumns.php | 4 ++-- .../m200000_000002_create_table_pristines.php | 4 ++-- .../m200000_000000_create_table_editcolumns.php | 4 ++-- .../m200000_000001_change_table_newcolumns.php | 8 ++++---- .../m200000_000002_create_table_pristines.php | 4 ++-- 14 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 271e86ee..398b3fe4 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -160,7 +160,7 @@ public function getCode(bool $quoted = false):string public function getAlterExpression(bool $addUsingExpression = false):string { if ($this->isEnum() && ApiGenerator::isPostgres()) { - return "'" . sprintf('enum_%1$s USING "%1$s"::enum_%1$s', $this->column->name) . "'"; + return "'" . sprintf('enum_%1$s USING "%1$s"::"enum_%1$s"', $this->column->name) . "'"; } if ($this->column->dbType === 'tsvector') { return "'" . $this->rawParts['type'] . "'"; diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index 4c90ec89..21d042dd 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -27,8 +27,8 @@ final class MigrationRecordBuilder public const ADD_UNIQUE = MigrationRecordBuilder::INDENT . "\$this->createIndex('%s', '%s', '%s', true);"; public const ADD_INDEX = MigrationRecordBuilder::INDENT . "\$this->createIndex('%s', '%s', '%s', %s);"; public const DROP_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropColumn('%s', '%s');"; - public const ADD_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('CREATE TYPE enum_%s AS ENUM(%s)');"; - public const DROP_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('DROP TYPE enum_%s');"; + public const ADD_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('CREATE TYPE \"enum_%s\" AS ENUM(%s)');"; + public const DROP_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('DROP TYPE \"enum_%s\"');"; public const DROP_TABLE = MigrationRecordBuilder::INDENT . "\$this->dropTable('%s');"; public const ADD_FK = MigrationRecordBuilder::INDENT . "\$this->addForeignKey('%s', '%s', '%s', '%s', '%s');"; public const ADD_PK = MigrationRecordBuilder::INDENT . "\$this->addPrimaryKey('%s', '%s', '%s');"; 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 20d47cbd..df70f5d2 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 @@ -8,7 +8,7 @@ class m200000_000000_change_table_v2_posts extends \yii\db\Migration public function safeUp() { $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); - $this->execute('CREATE TYPE enum_lang AS ENUM(\'ru\', \'eng\')'); + $this->execute('CREATE TYPE "enum_lang" AS ENUM(\'ru\', \'eng\')'); $this->addColumn('{{%v2_posts}}', 'lang', 'enum_lang NULL DEFAULT \'ru\''); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT"); @@ -25,7 +25,7 @@ public function safeDown() $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull()); $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); - $this->execute('DROP TYPE enum_lang'); + $this->execute('DROP TYPE "enum_lang"'); $this->alterColumn('{{%v2_posts}}', 'active', "SET DEFAULT 'f'"); } } diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php index bcef7fa8..a912555d 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php @@ -7,7 +7,7 @@ class m200000_000001_create_table_v2_tags extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_lang AS ENUM(\'ru\', \'eng\')'); + $this->execute('CREATE TYPE "enum_lang" AS ENUM(\'ru\', \'eng\')'); $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), 0 => '"name" varchar(100) NOT NULL', @@ -20,6 +20,6 @@ public function safeDown() { $this->dropIndex('v2_tags_name_key', '{{%v2_tags}}'); $this->dropTable('{{%v2_tags}}'); - $this->execute('DROP TYPE enum_lang'); + $this->execute('DROP TYPE "enum_lang"'); } } 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 4651987c..16e8663c 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 @@ -7,12 +7,12 @@ class m200000_000004_change_table_v2_users extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_role AS ENUM(\'admin\', \'editor\', \'reader\')'); + $this->execute('CREATE TYPE "enum_role" AS ENUM(\'admin\', \'editor\', \'reader\')'); $this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull()); $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT"); $this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN "email" SET DATA TYPE varchar(255)')->execute(); - $this->alterColumn('{{%v2_users}}', 'role', 'enum_role USING "role"::enum_role'); + $this->alterColumn('{{%v2_users}}', 'role', 'enum_role USING "role"::"enum_role"'); $this->alterColumn('{{%v2_users}}', 'role', "DROP DEFAULT"); $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); @@ -30,6 +30,6 @@ public function safeDown() $this->dropColumn('{{%v2_users}}', 'login'); $this->alterColumn('{{%v2_users}}', 'created_at', "SET DEFAULT CURRENT_TIMESTAMP"); $this->alterColumn('{{%v2_users}}', 'role', "SET DEFAULT 'reader'"); - $this->execute('DROP TYPE enum_role'); + $this->execute('DROP TYPE "enum_role"'); } } 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 52454746..c4fa2ed7 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 @@ -7,24 +7,24 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_connection AS ENUM(\'WIRED\', \'WIRELESS\')'); - $this->alterColumn('{{%editcolumns}}', 'connection', 'enum_connection USING "connection"::enum_connection'); + $this->execute('CREATE TYPE "enum_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); + $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', 'text NULL USING "device"::text'); $this->alterColumn('{{%editcolumns}}', 'device', "DROP NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'device', "DROP DEFAULT"); - $this->execute('DROP TYPE enum_device'); + $this->execute('DROP TYPE "enum_device"'); } 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->execute('CREATE TYPE "enum_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->alterColumn('{{%editcolumns}}', 'device', 'enum_device USING "device"::"enum_device"'); $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'); + $this->execute('DROP TYPE "enum_connection"'); $this->alterColumn('{{%editcolumns}}', 'device', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'device', "SET DEFAULT 'TV'"); } diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php index 75c86ca3..0178c600 100644 --- a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -7,7 +7,7 @@ class m200000_000001_create_table_newcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_new_column AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->execute('CREATE TYPE "enum_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), 'new_column' => 'enum_new_column NOT NULL DEFAULT \'ONE\'', @@ -17,6 +17,6 @@ public function safeUp() public function safeDown() { $this->dropTable('{{%newcolumns}}'); - $this->execute('DROP TYPE enum_new_column'); + $this->execute('DROP TYPE "enum_new_column"'); } } diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index 57dbe3c2..af2bb1ec 100644 --- a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -7,7 +7,7 @@ class m200000_000002_create_table_pristines extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->execute('CREATE TYPE "enum_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', @@ -17,6 +17,6 @@ public function safeUp() public function safeDown() { $this->dropTable('{{%pristines}}'); - $this->execute('DROP TYPE enum_device'); + $this->execute('DROP TYPE "enum_device"'); } } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php index 5d4a4340..46efe201 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -7,7 +7,7 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_connection AS ENUM(\'WIRED\', \'WIRELESS\')'); + $this->execute('CREATE TYPE "enum_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), @@ -18,6 +18,6 @@ public function safeUp() public function safeDown() { $this->dropTable('{{%editcolumns}}'); - $this->execute('DROP TYPE enum_connection'); + $this->execute('DROP TYPE "enum_connection"'); } } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php index 75c86ca3..0178c600 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -7,7 +7,7 @@ class m200000_000001_create_table_newcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_new_column AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->execute('CREATE TYPE "enum_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), 'new_column' => 'enum_new_column NOT NULL DEFAULT \'ONE\'', @@ -17,6 +17,6 @@ public function safeUp() public function safeDown() { $this->dropTable('{{%newcolumns}}'); - $this->execute('DROP TYPE enum_new_column'); + $this->execute('DROP TYPE "enum_new_column"'); } } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index 57dbe3c2..af2bb1ec 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -7,7 +7,7 @@ class m200000_000002_create_table_pristines extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->execute('CREATE TYPE "enum_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', @@ -17,6 +17,6 @@ public function safeUp() public function safeDown() { $this->dropTable('{{%pristines}}'); - $this->execute('DROP TYPE enum_device'); + $this->execute('DROP TYPE "enum_device"'); } } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php index 5d4a4340..46efe201 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -7,7 +7,7 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_connection AS ENUM(\'WIRED\', \'WIRELESS\')'); + $this->execute('CREATE TYPE "enum_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), @@ -18,6 +18,6 @@ public function safeUp() public function safeDown() { $this->dropTable('{{%editcolumns}}'); - $this->execute('DROP TYPE enum_connection'); + $this->execute('DROP TYPE "enum_connection"'); } } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php index 084a0be3..800d637a 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php @@ -7,17 +7,17 @@ class m200000_000001_change_table_newcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_new_column AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->execute('CREATE TYPE "enum_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->addColumn('{{%newcolumns}}', 'new_column', 'enum_new_column NOT NULL DEFAULT \'ONE\''); $this->dropColumn('{{%newcolumns}}', 'delete_col'); - $this->execute('DROP TYPE enum_delete_col'); + $this->execute('DROP TYPE "enum_delete_col"'); } public function safeDown() { - $this->execute('CREATE TYPE enum_delete_col AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')'); + $this->execute('CREATE TYPE "enum_delete_col" AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')'); $this->addColumn('{{%newcolumns}}', 'delete_col', 'enum_delete_col NULL DEFAULT NULL'); $this->dropColumn('{{%newcolumns}}', 'new_column'); - $this->execute('DROP TYPE enum_new_column'); + $this->execute('DROP TYPE "enum_new_column"'); } } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index 57dbe3c2..af2bb1ec 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -7,7 +7,7 @@ class m200000_000002_create_table_pristines extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->execute('CREATE TYPE "enum_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', @@ -17,6 +17,6 @@ public function safeUp() public function safeDown() { $this->dropTable('{{%pristines}}'); - $this->execute('DROP TYPE enum_device'); + $this->execute('DROP TYPE "enum_device"'); } } From d4527f55abeac5084cf4529fd3e20922c7144fcb Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Jan 2023 20:19:35 +0530 Subject: [PATCH 60/62] Implement enum with table name --- src/lib/ColumnToCode.php | 15 ++++++- src/lib/migrations/BaseMigrationBuilder.php | 28 ++++++------ src/lib/migrations/MigrationRecordBuilder.php | 44 ++++++++++--------- src/lib/migrations/MysqlMigrationBuilder.php | 3 +- .../migrations/PostgresMigrationBuilder.php | 24 +++++----- tests/migrations/m100000_000000_pgsql.php | 9 ++-- 6 files changed, 73 insertions(+), 50 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 398b3fe4..094c3a32 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -53,6 +53,12 @@ class ColumnToCode */ private $dbSchema; + /** + * @var string + * @example {{%table}} + */ + private $tableAlias; + /** * @var bool */ @@ -107,6 +113,7 @@ class ColumnToCode */ public function __construct( Schema $dbSchema, + string $tableAlias, ColumnSchema $column, bool $fromDb = false, bool $alter = false, @@ -114,6 +121,7 @@ public function __construct( bool $alterByXDbType = false ) { $this->dbSchema = $dbSchema; + $this->tableAlias = $tableAlias; $this->column = $column; $this->fromDb = $fromDb; $this->alter = $alter; @@ -160,7 +168,9 @@ public function getCode(bool $quoted = false):string public function getAlterExpression(bool $addUsingExpression = false):string { if ($this->isEnum() && ApiGenerator::isPostgres()) { - return "'" . sprintf('enum_%1$s USING "%1$s"::"enum_%1$s"', $this->column->name) . "'"; + $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias); + $enumTypeName = 'enum_'.$rawTableName.'_'.$this->column->name; + return "'" . sprintf($enumTypeName.' USING "%1$s"::"'.$enumTypeName.'"', $this->column->name) . "'"; } if ($this->column->dbType === 'tsvector') { return "'" . $this->rawParts['type'] . "'"; @@ -368,7 +378,8 @@ private function getIsBuiltinType($type, $dbType) private function resolveEnumType():void { if (ApiGenerator::isPostgres()) { - $this->rawParts['type'] = 'enum_' . $this->column->name; + $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias); + $this->rawParts['type'] = 'enum_'.$rawTableName.'_' . $this->column->name; return; } $this->rawParts['type'] = 'enum(' . self::mysqlEnumToString($this->column->enumValues) . ')'; diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 3fdd0d32..3a0cfaad 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -409,25 +409,27 @@ protected function isNeedUsingExpression(string $fromDbType, string $toDbType):b } // 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 + public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema { - $tableName = 'tmp_table_'; + $tmpTableName = 'tmp_table_'; $tmpEnumName = function (string $columnName): string { return '"tmp_enum_'.$columnName.'_"'; }; + $rawTableName = $this->db->schema->getRawTableName($tableAlias); + $innerEnumTypeName = "enum_{$tmpTableName}_$columnSchema->name"; - Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute(); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tmpTableName)->execute(); if (is_string($columnSchema->xDbType) && !empty($columnSchema->xDbType)) { $name = MigrationRecordBuilder::quote($columnSchema->name); - $column = [$name.' '.$this->newColStr($columnSchema)]; + $column = [$name.' '.$this->newColStr($tmpTableName, $columnSchema)]; if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - $column = strtr($column, ['enum_'.$columnSchema->name => $tmpEnumName($columnSchema->name)]); + $column = strtr($column, [$innerEnumTypeName => $tmpEnumName($columnSchema->name)]); } } else { - $column = [$columnSchema->name => $this->newColStr($columnSchema)]; + $column = [$columnSchema->name => $this->newColStr($tmpTableName, $columnSchema)]; if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { - $column[$columnSchema->name] = strtr($column[$columnSchema->name], ['enum_'.$columnSchema->name => $tmpEnumName($columnSchema->name)]); + $column[$columnSchema->name] = strtr($column[$columnSchema->name], [$innerEnumTypeName => $tmpEnumName($columnSchema->name)]); } } @@ -442,11 +444,11 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): )->execute(); } - Yii::$app->db->createCommand()->createTable($tableName, $column)->execute(); + Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute(); - $table = Yii::$app->db->getTableSchema($tableName); + $table = Yii::$app->db->getTableSchema($tmpTableName); - Yii::$app->db->createCommand()->dropTable($tableName)->execute(); + Yii::$app->db->createCommand()->dropTable($tmpTableName)->execute(); if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum Yii::$app->db->createCommand('DROP TYPE '.$tmpEnumName($columnSchema->name))->execute(); @@ -455,15 +457,15 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): } // reset back column enum name to original as we are comparing with current // e.g. we get different enum type name such as `enum_status` and `tmp_enum_status_` even there is no change, so below statement fix this issue - $table->columns[$columnSchema->name]->dbType = 'enum_'.$columnSchema->name; + $table->columns[$columnSchema->name]->dbType = 'enum_'.$rawTableName.'_'.$columnSchema->name; } return $table->columns[$columnSchema->name]; } - public function newColStr(\cebe\yii2openapi\db\ColumnSchema $columnSchema): string + public function newColStr(string $tableAlias, \cebe\yii2openapi\db\ColumnSchema $columnSchema): string { - $ctc = new ColumnToCode(\Yii::$app->db->schema, $columnSchema, false, false, true); + $ctc = new ColumnToCode(\Yii::$app->db->schema, $tableAlias, $columnSchema, false, false, true); return ColumnToCode::undoEscapeQuotes($ctc->getCode()); } diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index 21d042dd..241ffc04 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -27,8 +27,8 @@ final class MigrationRecordBuilder public const ADD_UNIQUE = MigrationRecordBuilder::INDENT . "\$this->createIndex('%s', '%s', '%s', true);"; public const ADD_INDEX = MigrationRecordBuilder::INDENT . "\$this->createIndex('%s', '%s', '%s', %s);"; public const DROP_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropColumn('%s', '%s');"; - public const ADD_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('CREATE TYPE \"enum_%s\" AS ENUM(%s)');"; - public const DROP_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('DROP TYPE \"enum_%s\"');"; + public const ADD_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('CREATE TYPE \"enum_%s_%s\" AS ENUM(%s)');"; + public const DROP_ENUM = MigrationRecordBuilder::INDENT . "\$this->execute('DROP TYPE \"enum_%s_%s\"');"; public const DROP_TABLE = MigrationRecordBuilder::INDENT . "\$this->dropTable('%s');"; public const ADD_FK = MigrationRecordBuilder::INDENT . "\$this->addForeignKey('%s', '%s', '%s', '%s', '%s');"; public const ADD_PK = MigrationRecordBuilder::INDENT . "\$this->addPrimaryKey('%s', '%s', '%s');"; @@ -64,9 +64,9 @@ public function createTable(string $tableAlias, array $columns):string foreach ($columns as $columnName => $cebeDbColumnSchema) { if (is_string($cebeDbColumnSchema->xDbType) && !empty($cebeDbColumnSchema->xDbType)) { $name = static::quote($columnName); - $codeColumns[] = $name.' '.$this->columnToCode($cebeDbColumnSchema, false)->getCode(); + $codeColumns[] = $name.' '.$this->columnToCode($tableAlias, $cebeDbColumnSchema, false)->getCode(); } else { - $codeColumns[$columnName] = $this->columnToCode($cebeDbColumnSchema, false)->getCode(); + $codeColumns[$columnName] = $this->columnToCode($tableAlias, $cebeDbColumnSchema, false)->getCode(); } } @@ -80,12 +80,12 @@ public function createTable(string $tableAlias, array $columns):string public function addColumn(string $tableAlias, ColumnSchema $column):string { if (is_string($column->xDbType) && !empty($column->xDbType)) { - $converter = $this->columnToCode($column, false); + $converter = $this->columnToCode($tableAlias, $column, false); $name = static::quote($column->name); return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $name, $converter->getCode()); } - $converter = $this->columnToCode($column, false); + $converter = $this->columnToCode($tableAlias, $column, false); return sprintf(self::ADD_COLUMN, $tableAlias, $column->name, $converter->getCode(true)); } @@ -95,11 +95,11 @@ public function addColumn(string $tableAlias, ColumnSchema $column):string public function addDbColumn(string $tableAlias, ColumnSchema $column):string { if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) { - $converter = $this->columnToCode($column, true); + $converter = $this->columnToCode($tableAlias, $column, true); $name = static::quote($column->name); return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $column->name, $converter->getCode()); } - $converter = $this->columnToCode($column, true); + $converter = $this->columnToCode($tableAlias, $column, true); return sprintf(self::ADD_COLUMN, $tableAlias, $column->name, $converter->getCode(true)); } @@ -109,7 +109,7 @@ public function addDbColumn(string $tableAlias, ColumnSchema $column):string public function alterColumn(string $tableAlias, ColumnSchema $column):string { if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) { - $converter = $this->columnToCode($column, true, false, true, true); + $converter = $this->columnToCode($tableAlias, $column, true, false, true, true); return sprintf( ApiGenerator::isPostgres() ? self::ALTER_COLUMN_RAW_PGSQL : self::ALTER_COLUMN_RAW, $tableAlias, @@ -117,7 +117,7 @@ public function alterColumn(string $tableAlias, ColumnSchema $column):string $converter->getCode() ); } - $converter = $this->columnToCode($column, true); + $converter = $this->columnToCode($tableAlias, $column, true); return sprintf(self::ALTER_COLUMN, $tableAlias, $column->name, $converter->getCode(true)); } @@ -127,7 +127,7 @@ public function alterColumn(string $tableAlias, ColumnSchema $column):string public function alterColumnType(string $tableAlias, ColumnSchema $column, bool $addUsing = false):string { if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) { - $converter = $this->columnToCode($column, false, false, true, true); + $converter = $this->columnToCode($tableAlias, $column, false, false, true, true); return sprintf( ApiGenerator::isPostgres() ? self::ALTER_COLUMN_RAW_PGSQL : self::ALTER_COLUMN_RAW, $tableAlias, @@ -135,7 +135,7 @@ public function alterColumnType(string $tableAlias, ColumnSchema $column, bool $ rtrim(ltrim($converter->getAlterExpression($addUsing), "'"), "'") ); } - $converter = $this->columnToCode($column, false); + $converter = $this->columnToCode($tableAlias, $column, false); return sprintf(self::ALTER_COLUMN, $tableAlias, $column->name, $converter->getAlterExpression($addUsing)); } @@ -145,7 +145,7 @@ public function alterColumnType(string $tableAlias, ColumnSchema $column, bool $ public function alterColumnTypeFromDb(string $tableAlias, ColumnSchema $column, bool $addUsing = false) :string { if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) { - $converter = $this->columnToCode($column, true, false, true, true); + $converter = $this->columnToCode($tableAlias, $column, true, false, true, true); return sprintf( ApiGenerator::isPostgres() ? self::ALTER_COLUMN_RAW_PGSQL : self::ALTER_COLUMN_RAW, $tableAlias, @@ -153,7 +153,7 @@ public function alterColumnTypeFromDb(string $tableAlias, ColumnSchema $column, rtrim(ltrim($converter->getAlterExpression($addUsing), "'"), "'") ); } - $converter = $this->columnToCode($column, true); + $converter = $this->columnToCode($tableAlias, $column, true); return sprintf(self::ALTER_COLUMN, $tableAlias, $column->name, $converter->getAlterExpression($addUsing)); } @@ -162,7 +162,7 @@ public function alterColumnTypeFromDb(string $tableAlias, ColumnSchema $column, */ public function setColumnDefault(string $tableAlias, ColumnSchema $column):string { - $default = $this->columnToCode($column, false, true)->getDefaultValue(); + $default = $this->columnToCode($tableAlias, $column, false, true)->getDefaultValue(); if ($default === null) { return ''; } @@ -174,7 +174,7 @@ public function setColumnDefault(string $tableAlias, ColumnSchema $column):strin */ public function setColumnDefaultFromDb(string $tableAlias, ColumnSchema $column):string { - $default = $this->columnToCode($column, true, true)->getDefaultValue(); + $default = $this->columnToCode($tableAlias, $column, true, true)->getDefaultValue(); if ($default === null) { return ''; } @@ -196,9 +196,10 @@ public function dropColumnNotNull(string $tableAlias, ColumnSchema $column):stri return sprintf(self::ALTER_COLUMN, $tableAlias, $column->name, '"DROP NOT NULL"'); } - public function createEnum(string $columnName, array $values):string + public function createEnum(string $tableAlias, string $columnName, array $values):string { - return sprintf(self::ADD_ENUM, $columnName, ColumnToCode::enumToString($values)); + $rawTableName = $this->dbSchema->getRawTableName($tableAlias); + return sprintf(self::ADD_ENUM, $rawTableName, $columnName, ColumnToCode::enumToString($values)); } public function addFk(string $fkName, string $tableAlias, string $fkCol, string $refTable, string $refCol):string @@ -234,9 +235,10 @@ public function dropTable(string $tableAlias):string return sprintf(self::DROP_TABLE, $tableAlias); } - public function dropEnum(string $columnName):string + public function dropEnum(string $tableAlias, string $columnName):string { - return sprintf(self::DROP_ENUM, $columnName); + $rawTableName = $this->dbSchema->getRawTableName($tableAlias); + return sprintf(self::DROP_ENUM, $rawTableName, $columnName); } public function dropFk(string $fkName, string $tableAlias):string @@ -258,6 +260,7 @@ public function dropIndex(string $tableAlias, string $indexName):string * @throws \yii\base\InvalidConfigException */ private function columnToCode( + string $tableAlias, ColumnSchema $column, bool $fromDb = false, bool $alter = false, @@ -266,6 +269,7 @@ private function columnToCode( ): ColumnToCode { return Yii::createObject(ColumnToCode::class, [ $this->dbSchema, + $tableAlias, $column, $fromDb, $alter, diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 9b899354..09f3af2b 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -39,13 +39,14 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):array { $changedAttributes = []; + $tableAlias = $this->model->getTableAlias(); $this->modifyCurrent($current); $this->modifyDesired($desired); $this->modifyDesiredInContextOfCurrent($current, $desired); // Why this is needed? Often manually created ColumnSchem instance have dbType 'varchar' with size 255 and ColumnSchema fetched from db have 'varchar(255)'. So varchar !== varchar(255). such normal mistake was leading to errors. So desired column is saved in temporary table and it is fetched from that temp. table and then compared with current ColumnSchema - $desiredFromDb = $this->tmpSaveNewCol($desired); + $desiredFromDb = $this->tmpSaveNewCol($tableAlias, $desired); $this->modifyDesired($desiredFromDb); $this->modifyDesiredInContextOfCurrent($current, $desiredFromDb); diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index d1a314bf..f87cb32c 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -23,8 +23,8 @@ protected function buildColumnsCreation(array $columns):void foreach ($columns as $column) { $tableName = $this->model->getTableAlias(); if (static::isEnum($column)) { - $this->migration->addUpCode($this->recordBuilder->createEnum($column->name, $column->enumValues)) - ->addDownCode($this->recordBuilder->dropEnum($column->name), true); + $this->migration->addUpCode($this->recordBuilder->createEnum($tableName, $column->name, $column->enumValues)) + ->addDownCode($this->recordBuilder->dropEnum($tableName, $column->name), true); } $this->migration->addUpCode($this->recordBuilder->addColumn($tableName, $column)) ->addDownCode($this->recordBuilder->dropColumn($tableName, $column->name)); @@ -42,8 +42,8 @@ protected function buildColumnsDrop(array $columns):void $this->migration->addDownCode($this->recordBuilder->addDbColumn($tableName, $column)) ->addUpCode($this->recordBuilder->dropColumn($tableName, $column->name)); if (static::isEnum($column)) { - $this->migration->addDownCode($this->recordBuilder->createEnum($column->name, $column->enumValues)) - ->addUpCode($this->recordBuilder->dropEnum($column->name)); + $this->migration->addDownCode($this->recordBuilder->createEnum($tableName, $column->name, $column->enumValues)) + ->addUpCode($this->recordBuilder->dropEnum($tableName, $column->name)); } } } @@ -92,31 +92,32 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir } } if ($isChangeToEnum) { - $this->migration->addUpCode($this->recordBuilder->createEnum($desired->name, $desired->enumValues), true); + $this->migration->addUpCode($this->recordBuilder->createEnum($tableName, $desired->name, $desired->enumValues), true); } if ($isChangeFromEnum) { - $this->migration->addUpCode($this->recordBuilder->dropEnum($current->name)); + $this->migration->addUpCode($this->recordBuilder->dropEnum($tableName, $current->name)); } if ($isChangeFromEnum) { $this->migration - ->addDownCode($this->recordBuilder->createEnum($current->name, $current->enumValues)); + ->addDownCode($this->recordBuilder->createEnum($tableName, $current->name, $current->enumValues)); } if ($isChangeToEnum) { - $this->migration->addDownCode($this->recordBuilder->dropEnum($current->name), true); + $this->migration->addDownCode($this->recordBuilder->dropEnum($tableName, $current->name), true); } } protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):array { $changedAttributes = []; + $tableAlias = $this->model->getTableAlias(); $this->modifyCurrent($current); $this->modifyDesired($desired); $this->modifyDesiredInContextOfCurrent($current, $desired); // for docs, please see MysqlMigrationBuilder file - $desiredFromDb = $this->tmpSaveNewCol($desired); + $desiredFromDb = $this->tmpSaveNewCol($tableAlias, $desired); $this->modifyDesired($desiredFromDb); $this->modifyDesiredInContextOfCurrent($current, $desiredFromDb); @@ -139,11 +140,12 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): protected function createEnumMigrations():void { + $tableAlias = $this->model->getTableAlias(); $enums = $this->model->getEnumAttributes(); foreach ($enums as $attr) { $this->migration - ->addUpCode($this->recordBuilder->createEnum($attr->columnName, $attr->enumValues), true) - ->addDownCode($this->recordBuilder->dropEnum($attr->columnName), true); + ->addUpCode($this->recordBuilder->createEnum($tableAlias, $attr->columnName, $attr->enumValues), true) + ->addDownCode($this->recordBuilder->dropEnum($tableAlias, $attr->columnName), true); } } diff --git a/tests/migrations/m100000_000000_pgsql.php b/tests/migrations/m100000_000000_pgsql.php index 0a42c8e9..bf076255 100644 --- a/tests/migrations/m100000_000000_pgsql.php +++ b/tests/migrations/m100000_000000_pgsql.php @@ -91,7 +91,9 @@ public function safeUp() 'str_country' => $this->text()->null()->defaultValue(null), ]); - $this->execute('CREATE TYPE enum_status AS ENUM(\'active\', \'draft\')'); + $rawTableName = $this->db->schema->getRawTableName('{{%v3_pgcustom}}'); + $enumTypeName = 'enum_'.$rawTableName.'_status'; + $this->execute('CREATE TYPE "'.$enumTypeName.'" AS ENUM(\'active\', \'draft\')'); $this->createTable('{{%v3_pgcustom}}', [ 'id' => $this->bigPrimaryKey(), @@ -100,7 +102,7 @@ public function safeUp() 'json2' => $this->json()->null()->defaultValue(null), 'json3' => $this->json()->defaultValue(Json::encode(['foo' => 'bar', 'bar' => 'baz'])), 'json4' => "json DEFAULT '" . new Expression(Json::encode(['ffo' => 'bar'])) . "'", - 'status' => 'enum_status', + 'status' => $enumTypeName, 'search' => 'tsvector' ]); $columns = [ @@ -138,7 +140,8 @@ public function safeDown() $this->dropTable('{{%v2_users}}'); $this->dropTable('{{%v2_categories}}'); $this->dropTable('{{%v3_pgcustom}}'); - $this->execute('DROP TYPE enum_status'); + $rawTableName = $this->db->schema->getRawTableName('{{%v3_pgcustom}}'); + $this->execute('DROP TYPE "enum_'.$rawTableName.'_status"'); $this->dropTable('{{%default_sizes}}'); } } From d4f6f2ddd4de86845dc4cf1d1ae664ceaf96d2f0 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 4 Jan 2023 16:37:11 +0530 Subject: [PATCH 61/62] Adjust tests for enum with table name --- .../m200000_000000_change_table_v2_posts.php | 6 +++--- .../m200000_000001_create_table_v2_tags.php | 6 +++--- .../m200000_000004_change_table_v2_users.php | 6 +++--- ...m200000_000000_change_table_editcolumns.php | 12 ++++++------ .../m200000_000001_create_table_newcolumns.php | 6 +++--- .../m200000_000002_create_table_pristines.php | 6 +++--- ...m200000_000000_create_table_editcolumns.php | 6 +++--- .../m200000_000001_create_table_newcolumns.php | 6 +++--- .../m200000_000002_create_table_pristines.php | 6 +++--- ...m200000_000000_create_table_editcolumns.php | 6 +++--- .../m200000_000001_change_table_newcolumns.php | 12 ++++++------ .../m200000_000002_create_table_pristines.php | 6 +++--- tests/unit/EnumTest.php | 18 +++++++++--------- 13 files changed, 51 insertions(+), 51 deletions(-) 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 df70f5d2..612cc2b1 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 @@ -8,8 +8,8 @@ class m200000_000000_change_table_v2_posts extends \yii\db\Migration public function safeUp() { $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); - $this->execute('CREATE TYPE "enum_lang" AS ENUM(\'ru\', \'eng\')'); - $this->addColumn('{{%v2_posts}}', 'lang', 'enum_lang NULL DEFAULT \'ru\''); + $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->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT"); $this->alterColumn('{{%v2_posts}}', 'category_id', 'bigint NOT NULL USING "category_id"::bigint'); @@ -25,7 +25,7 @@ public function safeDown() $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull()); $this->dropColumn('{{%v2_posts}}', 'lang'); $this->dropColumn('{{%v2_posts}}', 'id'); - $this->execute('DROP TYPE "enum_lang"'); + $this->execute('DROP TYPE "enum_itt_v2_posts_lang"'); $this->alterColumn('{{%v2_posts}}', 'active', "SET DEFAULT 'f'"); } } diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php index a912555d..4eb8889a 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php @@ -7,11 +7,11 @@ class m200000_000001_create_table_v2_tags extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_lang" AS ENUM(\'ru\', \'eng\')'); + $this->execute('CREATE TYPE "enum_itt_v2_tags_lang" AS ENUM(\'ru\', \'eng\')'); $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), 0 => '"name" varchar(100) NOT NULL', - 'lang' => 'enum_lang NOT NULL', + 'lang' => 'enum_itt_v2_tags_lang NOT NULL', ]); $this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true); } @@ -20,6 +20,6 @@ public function safeDown() { $this->dropIndex('v2_tags_name_key', '{{%v2_tags}}'); $this->dropTable('{{%v2_tags}}'); - $this->execute('DROP TYPE "enum_lang"'); + $this->execute('DROP TYPE "enum_itt_v2_tags_lang"'); } } 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 16e8663c..f5f7f351 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 @@ -7,12 +7,12 @@ class m200000_000004_change_table_v2_users extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_role" AS ENUM(\'admin\', \'editor\', \'reader\')'); + $this->execute('CREATE TYPE "enum_itt_v2_users_role" AS ENUM(\'admin\', \'editor\', \'reader\')'); $this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull()); $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT"); $this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN "email" SET DATA TYPE varchar(255)')->execute(); - $this->alterColumn('{{%v2_users}}', 'role', 'enum_role USING "role"::"enum_role"'); + $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->dropIndex('v2_users_username_key', '{{%v2_users}}'); $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); @@ -30,6 +30,6 @@ public function safeDown() $this->dropColumn('{{%v2_users}}', 'login'); $this->alterColumn('{{%v2_users}}', 'created_at', "SET DEFAULT CURRENT_TIMESTAMP"); $this->alterColumn('{{%v2_users}}', 'role', "SET DEFAULT 'reader'"); - $this->execute('DROP TYPE "enum_role"'); + $this->execute('DROP TYPE "enum_itt_v2_users_role"'); } } 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 c4fa2ed7..798b2af1 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 @@ -7,24 +7,24 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); - $this->alterColumn('{{%editcolumns}}', 'connection', 'enum_connection USING "connection"::"enum_connection"'); + $this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); + $this->alterColumn('{{%editcolumns}}', 'connection', 'enum_itt_editcolumns_connection USING "connection"::"enum_itt_editcolumns_connection"'); $this->alterColumn('{{%editcolumns}}', 'connection', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'connection', "SET DEFAULT 'WIRED'"); $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"'); + $this->execute('DROP TYPE "enum_itt_editcolumns_device"'); } 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->execute('CREATE TYPE "enum_itt_editcolumns_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->alterColumn('{{%editcolumns}}', 'device', 'enum_itt_editcolumns_device USING "device"::"enum_itt_editcolumns_device"'); $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"'); + $this->execute('DROP TYPE "enum_itt_editcolumns_connection"'); $this->alterColumn('{{%editcolumns}}', 'device', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'device', "SET DEFAULT 'TV'"); } diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php index 0178c600..60354e12 100644 --- a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -7,16 +7,16 @@ class m200000_000001_create_table_newcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), - 'new_column' => 'enum_new_column NOT NULL DEFAULT \'ONE\'', + 'new_column' => 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\'', ]); } public function safeDown() { $this->dropTable('{{%newcolumns}}'); - $this->execute('DROP TYPE "enum_new_column"'); + $this->execute('DROP TYPE "enum_itt_newcolumns_new_column"'); } } diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index af2bb1ec..644fd17e 100644 --- a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -7,16 +7,16 @@ class m200000_000002_create_table_pristines extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), - 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + 'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'', ]); } public function safeDown() { $this->dropTable('{{%pristines}}'); - $this->execute('DROP TYPE "enum_device"'); + $this->execute('DROP TYPE "enum_itt_pristines_device"'); } } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php index 46efe201..18e1e3be 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -7,17 +7,17 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); + $this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), - 'connection' => 'enum_connection NOT NULL DEFAULT \'WIRED\'', + 'connection' => 'enum_itt_editcolumns_connection NOT NULL DEFAULT \'WIRED\'', ]); } public function safeDown() { $this->dropTable('{{%editcolumns}}'); - $this->execute('DROP TYPE "enum_connection"'); + $this->execute('DROP TYPE "enum_itt_editcolumns_connection"'); } } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php index 0178c600..60354e12 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -7,16 +7,16 @@ class m200000_000001_create_table_newcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), - 'new_column' => 'enum_new_column NOT NULL DEFAULT \'ONE\'', + 'new_column' => 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\'', ]); } public function safeDown() { $this->dropTable('{{%newcolumns}}'); - $this->execute('DROP TYPE "enum_new_column"'); + $this->execute('DROP TYPE "enum_itt_newcolumns_new_column"'); } } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index af2bb1ec..644fd17e 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -7,16 +7,16 @@ class m200000_000002_create_table_pristines extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), - 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + 'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'', ]); } public function safeDown() { $this->dropTable('{{%pristines}}'); - $this->execute('DROP TYPE "enum_device"'); + $this->execute('DROP TYPE "enum_itt_pristines_device"'); } } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php index 46efe201..18e1e3be 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -7,17 +7,17 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); + $this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), - 'connection' => 'enum_connection NOT NULL DEFAULT \'WIRED\'', + 'connection' => 'enum_itt_editcolumns_connection NOT NULL DEFAULT \'WIRED\'', ]); } public function safeDown() { $this->dropTable('{{%editcolumns}}'); - $this->execute('DROP TYPE "enum_connection"'); + $this->execute('DROP TYPE "enum_itt_editcolumns_connection"'); } } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php index 800d637a..312b7045 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php @@ -7,17 +7,17 @@ class m200000_000001_change_table_newcolumns extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); - $this->addColumn('{{%newcolumns}}', 'new_column', 'enum_new_column NOT NULL DEFAULT \'ONE\''); + $this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->addColumn('{{%newcolumns}}', 'new_column', 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\''); $this->dropColumn('{{%newcolumns}}', 'delete_col'); - $this->execute('DROP TYPE "enum_delete_col"'); + $this->execute('DROP TYPE "enum_itt_newcolumns_delete_col"'); } public function safeDown() { - $this->execute('CREATE TYPE "enum_delete_col" AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')'); - $this->addColumn('{{%newcolumns}}', 'delete_col', 'enum_delete_col NULL DEFAULT NULL'); + $this->execute('CREATE TYPE "enum_itt_newcolumns_delete_col" AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')'); + $this->addColumn('{{%newcolumns}}', 'delete_col', 'enum_itt_newcolumns_delete_col NULL DEFAULT NULL'); $this->dropColumn('{{%newcolumns}}', 'new_column'); - $this->execute('DROP TYPE "enum_new_column"'); + $this->execute('DROP TYPE "enum_itt_newcolumns_new_column"'); } } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index af2bb1ec..644fd17e 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -7,16 +7,16 @@ class m200000_000002_create_table_pristines extends \yii\db\Migration { public function safeUp() { - $this->execute('CREATE TYPE "enum_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); + $this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), - 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + 'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'', ]); } public function safeDown() { $this->dropTable('{{%pristines}}'); - $this->execute('DROP TYPE "enum_device"'); + $this->execute('DROP TYPE "enum_itt_pristines_device"'); } } diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 82546392..d2b208b5 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -202,11 +202,11 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa private function deleteTables() { if (ApiGenerator::isPostgres()) { - Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_device CASCADE')->execute(); - Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_connection CASCADE')->execute(); - Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_new_column CASCADE')->execute(); - Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_delete_col CASCADE')->execute(); - Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_add_one_mood_at_last CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_pristines_device CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_editcolumns_device CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_editcolumns_connection CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_newcolumns_new_column CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_newcolumns_delete_col CASCADE')->execute(); } Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute(); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%newcolumns}}')->execute(); @@ -218,10 +218,10 @@ private function deleteTables() private function createTableForEditEnumToString() // and vice versa { if (ApiGenerator::isPostgres()) { - Yii::$app->db->createCommand('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')')->execute(); + Yii::$app->db->createCommand('CREATE TYPE enum_itt_editcolumns_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')')->execute(); Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ 'id' => 'pk', - 'device' => 'enum_device NOT NULL DEFAULT \'TV\'', + 'device' => 'enum_itt_editcolumns_device NOT NULL DEFAULT \'TV\'', 'connection' => 'string' ])->execute(); return; @@ -236,10 +236,10 @@ private function createTableForEditEnumToString() // and vice versa private function createTableForNewEnumColumn() { if (ApiGenerator::isPostgres()) { - Yii::$app->db->createCommand('CREATE TYPE enum_delete_col AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')')->execute(); + Yii::$app->db->createCommand('CREATE TYPE enum_itt_newcolumns_delete_col AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')')->execute(); Yii::$app->db->createCommand()->createTable('{{%newcolumns}}', [ 'id' => 'pk', - 'delete_col' => 'enum_delete_col' + 'delete_col' => 'enum_itt_newcolumns_delete_col' ])->execute(); return; } From 8250c095c31022e95e6e7400a5c1836cab682154 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 4 Jan 2023 17:29:09 +0530 Subject: [PATCH 62/62] Fix errors in https://github.com/SOHELAHMED7/yii2-openapi/pull/10 --- src/lib/ColumnToCode.php | 4 ++-- src/lib/migrations/BaseMigrationBuilder.php | 2 +- tests/migrations/m100000_000000_pgsql.php | 2 +- .../m200000_000000_change_table_v2_posts.php | 2 +- .../m200000_000001_create_table_v2_tags.php | 2 +- .../m200000_000004_change_table_v2_users.php | 2 +- .../m200000_000000_change_table_editcolumns.php | 2 ++ .../m200000_000000_change_table_editcolumns.php | 2 ++ .../m200000_000000_change_table_editcolumns.php | 12 ++++++++++-- .../m200000_000001_create_table_newcolumns.php | 2 +- .../m200000_000002_create_table_pristines.php | 2 +- .../m200000_000000_create_table_editcolumns.php | 1 + .../m200000_000000_create_table_editcolumns.php | 1 + tests/specs/enum/fresh/mysql/enum.yaml | 9 +++++++++ .../m200000_000000_create_table_editcolumns.php | 5 ++++- .../m200000_000001_create_table_newcolumns.php | 2 +- .../m200000_000002_create_table_pristines.php | 2 +- .../m200000_000000_create_table_editcolumns.php | 1 + .../m200000_000000_create_table_editcolumns.php | 1 + .../m200000_000000_create_table_editcolumns.php | 5 ++++- .../m200000_000001_change_table_newcolumns.php | 4 ++-- .../m200000_000002_create_table_pristines.php | 2 +- tests/unit/EnumTest.php | 7 +++++-- 23 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 094c3a32..b5001d98 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -170,7 +170,7 @@ public function getAlterExpression(bool $addUsingExpression = false):string if ($this->isEnum() && ApiGenerator::isPostgres()) { $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias); $enumTypeName = 'enum_'.$rawTableName.'_'.$this->column->name; - return "'" . sprintf($enumTypeName.' USING "%1$s"::"'.$enumTypeName.'"', $this->column->name) . "'"; + return "'" . sprintf('"'.$enumTypeName.'" USING "%1$s"::"'.$enumTypeName.'"', $this->column->name) . "'"; } if ($this->column->dbType === 'tsvector') { return "'" . $this->rawParts['type'] . "'"; @@ -379,7 +379,7 @@ private function resolveEnumType():void { if (ApiGenerator::isPostgres()) { $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias); - $this->rawParts['type'] = 'enum_'.$rawTableName.'_' . $this->column->name; + $this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"'; return; } $this->rawParts['type'] = 'enum(' . self::mysqlEnumToString($this->column->enumValues) . ')'; diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 3a0cfaad..a2088c77 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -416,7 +416,7 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch return '"tmp_enum_'.$columnName.'_"'; }; $rawTableName = $this->db->schema->getRawTableName($tableAlias); - $innerEnumTypeName = "enum_{$tmpTableName}_$columnSchema->name"; + $innerEnumTypeName = "\"enum_{$tmpTableName}_{$columnSchema->name}\""; Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tmpTableName)->execute(); diff --git a/tests/migrations/m100000_000000_pgsql.php b/tests/migrations/m100000_000000_pgsql.php index bf076255..a200fbef 100644 --- a/tests/migrations/m100000_000000_pgsql.php +++ b/tests/migrations/m100000_000000_pgsql.php @@ -102,7 +102,7 @@ public function safeUp() 'json2' => $this->json()->null()->defaultValue(null), 'json3' => $this->json()->defaultValue(Json::encode(['foo' => 'bar', 'bar' => 'baz'])), 'json4' => "json DEFAULT '" . new Expression(Json::encode(['ffo' => 'bar'])) . "'", - 'status' => $enumTypeName, + 'status' => '"'.$enumTypeName.'"', 'search' => 'tsvector' ]); $columns = [ 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 612cc2b1..e98488cc 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 @@ -9,7 +9,7 @@ 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->addColumn('{{%v2_posts}}', 'lang', '"enum_itt_v2_posts_lang" NULL DEFAULT \'ru\''); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT"); $this->alterColumn('{{%v2_posts}}', 'category_id', 'bigint NOT NULL USING "category_id"::bigint'); diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php index 4eb8889a..2466cff0 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000001_create_table_v2_tags.php @@ -11,7 +11,7 @@ public function safeUp() $this->createTable('{{%v2_tags}}', [ 'id' => $this->bigPrimaryKey(), 0 => '"name" varchar(100) NOT NULL', - 'lang' => 'enum_itt_v2_tags_lang NOT NULL', + 'lang' => '"enum_itt_v2_tags_lang" NOT NULL', ]); $this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true); } 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 f5f7f351..96459926 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 @@ -12,7 +12,7 @@ public function safeUp() $this->dropColumn('{{%v2_users}}', 'username'); $this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT"); $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', '"enum_itt_v2_users_role" USING "role"::"enum_itt_v2_users_role"'); $this->alterColumn('{{%v2_users}}', 'role', "DROP DEFAULT"); $this->dropIndex('v2_users_username_key', '{{%v2_users}}'); $this->createIndex('v2_users_login_key', '{{%v2_users}}', 'login', true); diff --git a/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php index 9d0b16ab..36a599c6 100644 --- a/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php +++ b/tests/specs/enum/change/maria/app/migrations_maria_db/m200000_000000_change_table_editcolumns.php @@ -7,6 +7,7 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration { public function up() { + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\''); $this->alterColumn('{{%editcolumns}}', 'connection', 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\''); $this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null()->defaultValue(null)); } @@ -15,5 +16,6 @@ public function down() { $this->alterColumn('{{%editcolumns}}', 'device', 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\''); $this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null()->defaultValue(null)); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', $this->string(255)->null()->defaultValue(null)); } } diff --git a/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php index 7a79e1fc..3ad3b894 100644 --- a/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php +++ b/tests/specs/enum/change/mysql/app/migrations_mysql_db/m200000_000000_change_table_editcolumns.php @@ -7,6 +7,7 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration { public function up() { + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\''); $this->alterColumn('{{%editcolumns}}', 'connection', 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\''); $this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null()); } @@ -15,5 +16,6 @@ public function down() { $this->alterColumn('{{%editcolumns}}', 'device', 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\''); $this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null()->defaultValue(null)); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', $this->string(255)->null()->defaultValue(null)); } } 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 798b2af1..fee56988 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 @@ -8,7 +8,11 @@ class m200000_000000_change_table_editcolumns extends \yii\db\Migration public function safeUp() { $this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); - $this->alterColumn('{{%editcolumns}}', 'connection', 'enum_itt_editcolumns_connection USING "connection"::"enum_itt_editcolumns_connection"'); + $this->execute('CREATE TYPE "enum_itt_editcolumns_camelCaseCol" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', '"enum_itt_editcolumns_camelCaseCol" USING "camelCaseCol"::"enum_itt_editcolumns_camelCaseCol"'); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "SET NOT NULL"); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "SET DEFAULT 'TWO'"); + $this->alterColumn('{{%editcolumns}}', 'connection', '"enum_itt_editcolumns_connection" USING "connection"::"enum_itt_editcolumns_connection"'); $this->alterColumn('{{%editcolumns}}', 'connection', "SET NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'connection', "SET DEFAULT 'WIRED'"); $this->alterColumn('{{%editcolumns}}', 'device', 'text NULL USING "device"::text'); @@ -20,8 +24,12 @@ public function safeUp() public function safeDown() { $this->execute('CREATE TYPE "enum_itt_editcolumns_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); - $this->alterColumn('{{%editcolumns}}', 'device', 'enum_itt_editcolumns_device USING "device"::"enum_itt_editcolumns_device"'); + $this->alterColumn('{{%editcolumns}}', 'device', '"enum_itt_editcolumns_device" USING "device"::"enum_itt_editcolumns_device"'); $this->alterColumn('{{%editcolumns}}', 'connection', 'varchar(255) NULL USING "connection"::varchar'); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', 'varchar(255) NULL USING "camelCaseCol"::varchar'); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "DROP NOT NULL"); + $this->alterColumn('{{%editcolumns}}', 'camelCaseCol', "DROP DEFAULT"); + $this->execute('DROP TYPE "enum_itt_editcolumns_camelCaseCol"'); $this->alterColumn('{{%editcolumns}}', 'connection', "DROP NOT NULL"); $this->alterColumn('{{%editcolumns}}', 'connection', "DROP DEFAULT"); $this->execute('DROP TYPE "enum_itt_editcolumns_connection"'); diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php index 60354e12..dc4901ee 100644 --- a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -10,7 +10,7 @@ public function safeUp() $this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), - 'new_column' => 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\'', + 'new_column' => '"enum_itt_newcolumns_new_column" NOT NULL DEFAULT \'ONE\'', ]); } diff --git a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index 644fd17e..45b63115 100644 --- a/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -10,7 +10,7 @@ public function safeUp() $this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), - 'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'', + 'device' => '"enum_itt_pristines_device" NOT NULL DEFAULT \'TV\'', ]); } diff --git a/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php index 1778bc15..1d2581bf 100644 --- a/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/fresh/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php @@ -11,6 +11,7 @@ public function up() 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + 'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'', ]); } diff --git a/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php index 78e27f4f..3aeb5b25 100644 --- a/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/fresh/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php @@ -11,6 +11,7 @@ public function up() 'id' => $this->primaryKey(), 'device' => $this->text()->null(), 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + 'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'', ]); } diff --git a/tests/specs/enum/fresh/mysql/enum.yaml b/tests/specs/enum/fresh/mysql/enum.yaml index 3017f013..2dcc0a94 100644 --- a/tests/specs/enum/fresh/mysql/enum.yaml +++ b/tests/specs/enum/fresh/mysql/enum.yaml @@ -78,6 +78,15 @@ components: default: WIRED nullable: false + camelCaseCol: + type: string + enum: + - ONE + - TWO + - THREE + default: + TWO + nullable: false # Enumvaluechange: # type: object diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php index 18e1e3be..5180bf54 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -7,11 +7,13 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration { public function safeUp() { + $this->execute('CREATE TYPE "enum_itt_editcolumns_camelCaseCol" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), - 'connection' => 'enum_itt_editcolumns_connection NOT NULL DEFAULT \'WIRED\'', + 'connection' => '"enum_itt_editcolumns_connection" NOT NULL DEFAULT \'WIRED\'', + 'camelCaseCol' => '"enum_itt_editcolumns_camelCaseCol" NOT NULL DEFAULT \'TWO\'', ]); } @@ -19,5 +21,6 @@ public function safeDown() { $this->dropTable('{{%editcolumns}}'); $this->execute('DROP TYPE "enum_itt_editcolumns_connection"'); + $this->execute('DROP TYPE "enum_itt_editcolumns_camelCaseCol"'); } } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php index 60354e12..dc4901ee 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000001_create_table_newcolumns.php @@ -10,7 +10,7 @@ public function safeUp() $this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->createTable('{{%newcolumns}}', [ 'id' => $this->primaryKey(), - 'new_column' => 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\'', + 'new_column' => '"enum_itt_newcolumns_new_column" NOT NULL DEFAULT \'ONE\'', ]); } diff --git a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index 644fd17e..45b63115 100644 --- a/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/fresh/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -10,7 +10,7 @@ public function safeUp() $this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), - 'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'', + 'device' => '"enum_itt_pristines_device" NOT NULL DEFAULT \'TV\'', ]); } diff --git a/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php index 1778bc15..1d2581bf 100644 --- a/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000000_create_table_editcolumns.php @@ -11,6 +11,7 @@ public function up() 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + 'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'', ]); } diff --git a/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php index 78e27f4f..3aeb5b25 100644 --- a/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000000_create_table_editcolumns.php @@ -11,6 +11,7 @@ public function up() 'id' => $this->primaryKey(), 'device' => $this->text()->null(), 'connection' => 'enum("WIRED", "WIRELESS") NOT NULL DEFAULT \'WIRED\'', + 'camelCaseCol' => 'enum("ONE", "TWO", "THREE") NOT NULL DEFAULT \'TWO\'', ]); } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php index 18e1e3be..5180bf54 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000000_create_table_editcolumns.php @@ -7,11 +7,13 @@ class m200000_000000_create_table_editcolumns extends \yii\db\Migration { public function safeUp() { + $this->execute('CREATE TYPE "enum_itt_editcolumns_camelCaseCol" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); $this->execute('CREATE TYPE "enum_itt_editcolumns_connection" AS ENUM(\'WIRED\', \'WIRELESS\')'); $this->createTable('{{%editcolumns}}', [ 'id' => $this->primaryKey(), 'device' => $this->text()->null()->defaultValue(null), - 'connection' => 'enum_itt_editcolumns_connection NOT NULL DEFAULT \'WIRED\'', + 'connection' => '"enum_itt_editcolumns_connection" NOT NULL DEFAULT \'WIRED\'', + 'camelCaseCol' => '"enum_itt_editcolumns_camelCaseCol" NOT NULL DEFAULT \'TWO\'', ]); } @@ -19,5 +21,6 @@ public function safeDown() { $this->dropTable('{{%editcolumns}}'); $this->execute('DROP TYPE "enum_itt_editcolumns_connection"'); + $this->execute('DROP TYPE "enum_itt_editcolumns_camelCaseCol"'); } } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php index 312b7045..a1d79e66 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000001_change_table_newcolumns.php @@ -8,7 +8,7 @@ class m200000_000001_change_table_newcolumns extends \yii\db\Migration public function safeUp() { $this->execute('CREATE TYPE "enum_itt_newcolumns_new_column" AS ENUM(\'ONE\', \'TWO\', \'THREE\')'); - $this->addColumn('{{%newcolumns}}', 'new_column', 'enum_itt_newcolumns_new_column NOT NULL DEFAULT \'ONE\''); + $this->addColumn('{{%newcolumns}}', 'new_column', '"enum_itt_newcolumns_new_column" NOT NULL DEFAULT \'ONE\''); $this->dropColumn('{{%newcolumns}}', 'delete_col'); $this->execute('DROP TYPE "enum_itt_newcolumns_delete_col"'); } @@ -16,7 +16,7 @@ public function safeUp() public function safeDown() { $this->execute('CREATE TYPE "enum_itt_newcolumns_delete_col" AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')'); - $this->addColumn('{{%newcolumns}}', 'delete_col', 'enum_itt_newcolumns_delete_col NULL DEFAULT NULL'); + $this->addColumn('{{%newcolumns}}', 'delete_col', '"enum_itt_newcolumns_delete_col" NULL DEFAULT NULL'); $this->dropColumn('{{%newcolumns}}', 'new_column'); $this->execute('DROP TYPE "enum_itt_newcolumns_new_column"'); } diff --git a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php index 644fd17e..45b63115 100644 --- a/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php +++ b/tests/specs/enum/new_column/pgsql/app/migrations_pgsql_db/m200000_000002_create_table_pristines.php @@ -10,7 +10,7 @@ public function safeUp() $this->execute('CREATE TYPE "enum_itt_pristines_device" AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')'); $this->createTable('{{%pristines}}', [ 'id' => $this->primaryKey(), - 'device' => 'enum_itt_pristines_device NOT NULL DEFAULT \'TV\'', + 'device' => '"enum_itt_pristines_device" NOT NULL DEFAULT \'TV\'', ]); } diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index d2b208b5..2568b7f4 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -205,6 +205,7 @@ private function deleteTables() Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_pristines_device CASCADE')->execute(); Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_editcolumns_device CASCADE')->execute(); Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_editcolumns_connection CASCADE')->execute(); + Yii::$app->db->createCommand('DROP TYPE IF EXISTS "enum_itt_editcolumns_camelCaseCol" CASCADE')->execute(); Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_newcolumns_new_column CASCADE')->execute(); Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_itt_newcolumns_delete_col CASCADE')->execute(); } @@ -222,14 +223,16 @@ private function createTableForEditEnumToString() // and vice versa Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ 'id' => 'pk', 'device' => 'enum_itt_editcolumns_device NOT NULL DEFAULT \'TV\'', - 'connection' => 'string' + 'connection' => 'string', + 'camelCaseCol' => 'string', ])->execute(); return; } Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [ 'id' => 'pk', 'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'', - 'connection' => 'string' + 'connection' => 'string', + 'camelCaseCol' => 'string', ])->execute(); }