From 9ec5b1b86673b328b5d3b3c6d9ef38779c922376 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Sep 2024 13:19:34 +0530 Subject: [PATCH 01/25] Create PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1cd2090b..3bb79307 100644 --- a/README.md +++ b/README.md @@ -568,3 +568,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 87b01da1ce48961e76ad452e5c40e4febb63d46b Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Sep 2024 13:30:40 +0530 Subject: [PATCH 02/25] Add failing test --- README.md | 1 - .../index.php | 14 ++++++++++ .../index.yml | 27 +++++++++++++++++++ tests/unit/IssueFixTest.php | 14 ++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml diff --git a/README.md b/README.md index 3bb79307..1cd2090b 100644 --- a/README.md +++ b/README.md @@ -568,4 +568,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/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php new file mode 100644 index 00000000..8a96371f --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php @@ -0,0 +1,14 @@ + '@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` +]; + diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml new file mode 100644 index 00000000..b1dc40f3 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -0,0 +1,27 @@ +openapi: 3.0.3 + +info: + title: 'Description of a property in spec must correspond to DB TABLE COLUMN COMMENT #60' + version: 1.0.0 + +components: + schemas: + Fruit: + type: object + properties: + id: + type: integer + name: + type: string + description: desc + # colour: + # type: string + description: + type: string + +paths: + '/': + get: + responses: + '200': + description: OK diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index b6c7abdb..6eed92e9 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -360,4 +360,18 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim() ]); $this->checkFiles($actualFiles, $expectedFiles); } + + // https://github.com/php-openapi/yii2-openapi/issues/60 + public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() + { + $testFile = Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php"); + $this->runGenerator($testFile); + // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + // 'recursive' => true, + // ]); + // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql"), [ + // 'recursive' => true, + // ]); + // $this->checkFiles($actualFiles, $expectedFiles); + } } From 1e408b7dde8a47169fe2edf9d87baa005688e45f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Sep 2024 18:50:34 +0530 Subject: [PATCH 03/25] Fix this issue --- src/lib/ColumnToCode.php | 14 +++++++++----- src/lib/items/Attribute.php | 1 + .../index.php | 1 - .../index.yml | 2 ++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index e9a570bc..5f152c89 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -84,9 +84,9 @@ class ColumnToCode */ private $isPk = false; - private $rawParts = ['type' => null, 'nullable' => null, 'default' => null, 'position' => null]; + private $rawParts = ['type' => null, 'nullable' => null, 'default' => null, 'position' => null, 'comment' => null]; - private $fluentParts = ['type' => null, 'nullable' => null, 'default' => null, 'position' => null]; + private $fluentParts = ['type' => null, 'nullable' => null, 'default' => null, 'position' => null, 'comment' => null]; /** * @var bool @@ -160,7 +160,8 @@ public function getCode(bool $quoted = false):string $this->fluentParts['type'], $this->fluentParts['nullable'], $this->fluentParts['default'], - $this->fluentParts['position'] + $this->fluentParts['position'], + $this->fluentParts['comment'], ]); array_unshift($parts, '$this'); return implode('->', array_filter(array_map('trim', $parts))); @@ -175,9 +176,12 @@ public function getCode(bool $quoted = false):string } $code = $this->rawParts['type'] . ' ' . $this->rawParts['nullable'] . $default; - if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb()) && $this->rawParts['position']) { - $code .= ' ' . $this->rawParts['position']; + + if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb())) { + $code .= $this->rawParts['position'] ? ' ' . $this->rawParts['position'] : ''; + $code .= $this->rawParts['comment'] ? ' '.$this->rawParts['comment'] : ''; } + if (ApiGenerator::isPostgres() && $this->alterByXDbType) { return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type']; } diff --git a/src/lib/items/Attribute.php b/src/lib/items/Attribute.php index 5b62e49d..b7a991bf 100644 --- a/src/lib/items/Attribute.php +++ b/src/lib/items/Attribute.php @@ -318,6 +318,7 @@ public function toColumnSchema():ColumnSchema 'allowNull' => $this->allowNull(), 'size' => $this->size > 0 ? $this->size : null, 'xDbType' => $this->xDbType, + 'comment' => $this->description, ]); $column->isPrimaryKey = $this->primary; $column->autoIncrement = $this->primary && $this->phpType === 'int'; diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php index 8a96371f..5642a696 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php @@ -11,4 +11,3 @@ 'generateMigrations' => true, 'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` ]; - diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index b1dc40f3..43e75c38 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -18,6 +18,8 @@ components: # type: string description: type: string + x-db-type: varchar + description: desc 2 paths: '/': From 2fb2d7b3ccb4282b6263455535a397a39cd9269a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Sep 2024 18:50:49 +0530 Subject: [PATCH 04/25] Move `resolve()` method --- src/lib/ColumnToCode.php | 104 ++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 50 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 5f152c89..73030ea5 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -150,6 +150,60 @@ public function __construct( $this->resolve(); } + private function resolve():void + { + $dbType = $this->typeWithoutSize(strtolower($this->column->dbType)); + $type = $this->column->type; + $this->resolvePosition(); + //Primary Keys + if (array_key_exists($type, self::PK_TYPE_MAP)) { + $this->rawParts['type'] = $type; + $this->fluentParts['type'] = self::PK_TYPE_MAP[$type]; + $this->isPk = true; + return; + } + if (array_key_exists($dbType, self::PK_TYPE_MAP)) { + $this->rawParts['type'] = $dbType; + $this->fluentParts['type'] = self::PK_TYPE_MAP[$dbType]; + $this->isPk = true; + return; + } + + if ($dbType === 'varchar') { + $type = $dbType = 'string'; + } + $fluentSize = $this->column->size ? '(' . $this->column->size . ')' : '()'; + $rawSize = $this->column->size ? '(' . $this->column->size . ')' : ''; + $this->rawParts['nullable'] = $this->column->allowNull ? 'NULL' : 'NOT NULL'; + $this->fluentParts['nullable'] = $this->column->allowNull === true ? 'null()' : 'notNull()'; + + $this->fluentParts['comment'] = $this->column->comment ? 'comment(\''.$this->column->comment.'\')' : $this->fluentParts['comment']; + $this->rawParts['comment'] = $this->column->comment ? 'COMMENT \''.$this->column->comment.'\'' : $this->fluentParts['comment']; + + if (array_key_exists($dbType, self::INT_TYPE_MAP)) { + $this->fluentParts['type'] = self::INT_TYPE_MAP[$dbType] . $fluentSize; + $this->rawParts['type'] = + $this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize); + } elseif (array_key_exists($type, self::INT_TYPE_MAP)) { + $this->fluentParts['type'] = self::INT_TYPE_MAP[$type] . $fluentSize; + $this->rawParts['type'] = + $this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize); + } elseif ($this->isEnum()) { + $this->resolveEnumType(); + } elseif ($this->isDecimal()) { + $this->fluentParts['type'] = $this->column->dbType; + $this->rawParts['type'] = $this->column->dbType; + } else { + $this->fluentParts['type'] = $type . $fluentSize; + $this->rawParts['type'] = + $this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize); + } + + $this->isBuiltinType = $this->raw ? false : $this->getIsBuiltinType($type, $dbType); + + $this->resolveDefaultValue(); + } + public function getCode(bool $quoted = false):string { if ($this->isPk) { @@ -324,56 +378,6 @@ private function defaultValueArray(array $value):string return "'{" . trim(Json::encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT), '[]') . "}'"; } - private function resolve():void - { - $dbType = $this->typeWithoutSize(strtolower($this->column->dbType)); - $type = $this->column->type; - $this->resolvePosition(); - //Primary Keys - if (array_key_exists($type, self::PK_TYPE_MAP)) { - $this->rawParts['type'] = $type; - $this->fluentParts['type'] = self::PK_TYPE_MAP[$type]; - $this->isPk = true; - return; - } - if (array_key_exists($dbType, self::PK_TYPE_MAP)) { - $this->rawParts['type'] = $dbType; - $this->fluentParts['type'] = self::PK_TYPE_MAP[$dbType]; - $this->isPk = true; - return; - } - - if ($dbType === 'varchar') { - $type = $dbType = 'string'; - } - $fluentSize = $this->column->size ? '(' . $this->column->size . ')' : '()'; - $rawSize = $this->column->size ? '(' . $this->column->size . ')' : ''; - $this->rawParts['nullable'] = $this->column->allowNull ? 'NULL' : 'NOT NULL'; - $this->fluentParts['nullable'] = $this->column->allowNull === true ? 'null()' : 'notNull()'; - if (array_key_exists($dbType, self::INT_TYPE_MAP)) { - $this->fluentParts['type'] = self::INT_TYPE_MAP[$dbType] . $fluentSize; - $this->rawParts['type'] = - $this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize); - } elseif (array_key_exists($type, self::INT_TYPE_MAP)) { - $this->fluentParts['type'] = self::INT_TYPE_MAP[$type] . $fluentSize; - $this->rawParts['type'] = - $this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize); - } elseif ($this->isEnum()) { - $this->resolveEnumType(); - } elseif ($this->isDecimal()) { - $this->fluentParts['type'] = $this->column->dbType; - $this->rawParts['type'] = $this->column->dbType; - } else { - $this->fluentParts['type'] = $type . $fluentSize; - $this->rawParts['type'] = - $this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize); - } - - $this->isBuiltinType = $this->raw ? false : $this->getIsBuiltinType($type, $dbType); - - $this->resolveDefaultValue(); - } - /** * @param $type * @param $dbType From f998279ad0f218ed2aec766cd9c26eecfb3dca08 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Sep 2024 19:06:04 +0530 Subject: [PATCH 05/25] Add test to ensure existing comments are preserved --- .../index.yml | 8 ++++++++ tests/unit/IssueFixTest.php | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index 43e75c38..83f3a948 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -20,6 +20,14 @@ components: type: string x-db-type: varchar description: desc 2 + Animal: + type: object + properties: + id: + type: integer + name: + type: integer + description: desc paths: '/': diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 6eed92e9..45ace04d 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -364,6 +364,8 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim() // https://github.com/php-openapi/yii2-openapi/issues/60 public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() { + $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + $this->createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); $testFile = Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php"); $this->runGenerator($testFile); // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ @@ -373,5 +375,20 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC // 'recursive' => true, // ]); // $this->checkFiles($actualFiles, $expectedFiles); + + $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + } + + private function createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() + { + Yii::$app->db->createCommand()->createTable('{{%animals}}', [ + 'id' => 'pk', + 'name' => 'text comment "the name"', + ])->execute(); + } + + private function deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() + { + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%animals}}')->execute(); } } From 4a117856be13e05234f6dfee717eb55dcfe19a93 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Sep 2024 19:24:35 +0530 Subject: [PATCH 06/25] Fix failing tests --- src/generator/default/dbmodel.php | 8 ++++---- .../migrations/m200000_000002_create_table_blog_posts.php | 4 ++-- .../m200000_000004_create_table_post_comments.php | 4 ++-- .../m200000_000002_create_table_blog_posts.php | 4 ++-- .../m200000_000004_create_table_post_comments.php | 4 ++-- .../m200000_000002_create_table_blog_posts.php | 4 ++-- .../m200000_000004_create_table_post_comments.php | 4 ++-- .../m200000_000002_create_table_blog_posts.php | 4 ++-- .../m200000_000004_create_table_post_comments.php | 4 ++-- .../m200000_000005_change_table_v2_comments.php | 2 +- .../m200000_000005_change_table_v2_comments.php | 2 +- .../m200000_000005_change_table_v2_comments.php | 2 +- .../m200000_000002_create_table_webhooks.php | 2 +- .../m200000_000002_create_table_webhooks.php | 2 +- .../m200000_000000_create_table_accounts.php | 2 +- .../m200000_000001_create_table_contacts.php | 2 +- .../m200000_000001_create_table_b123s.php | 2 +- .../m200000_000002_create_table_a123s.php | 2 +- .../m200000_000003_create_table_accounts.php | 2 +- .../m200000_000005_create_table_domains.php | 4 ++-- .../m200000_000006_create_table_e123s.php | 2 +- .../m200000_000007_create_table_routings.php | 6 +++--- .../m200000_000003_create_table_pristines.php | 2 +- .../m200000_000003_create_table_pristines.php | 2 +- .../m200000_000003_create_table_pristines.php | 2 +- .../m200000_000003_create_table_pristines.php | 2 +- .../m200000_000003_create_table_pristines.php | 2 +- .../m200000_000003_create_table_pristines.php | 2 +- .../m200000_000001_create_table_postxes.php | 8 ++++---- 29 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/generator/default/dbmodel.php b/src/generator/default/dbmodel.php index 17fa4a84..a3d269b6 100644 --- a/src/generator/default/dbmodel.php +++ b/src/generator/default/dbmodel.php @@ -49,8 +49,8 @@ */ abstract class getClassName() ?> extends \yii\db\ActiveRecord { -getScenarios()): -foreach($scenarios as $scenario): ?> +getScenarios()): +foreach ($scenarios as $scenario): ?> /** * @@ -76,7 +76,7 @@ public static function tableName() { return getTableAlias()) ?>; } - + /** * Automatically generated scenarios from the model 'x-scenarios'. @@ -92,7 +92,7 @@ public function scenarios() $default = parent::scenarios()[self::SCENARIO_DEFAULT]; return [ - + self:: => $default, /** diff --git a/tests/specs/blog/migrations/m200000_000002_create_table_blog_posts.php b/tests/specs/blog/migrations/m200000_000002_create_table_blog_posts.php index b0b8e371..c7cc90fb 100644 --- a/tests/specs/blog/migrations/m200000_000002_create_table_blog_posts.php +++ b/tests/specs/blog/migrations/m200000_000002_create_table_blog_posts.php @@ -11,10 +11,10 @@ public function up() 0 => 'uid varchar(128) NOT NULL', 'title' => $this->string(255)->notNull(), 'slug' => $this->string(200)->null()->defaultValue(null), - 'category_id' => $this->integer()->notNull(), + 'category_id' => $this->integer()->notNull()->comment('Category of posts'), 'active' => $this->boolean()->notNull()->defaultValue(false), 'created_at' => $this->date()->null()->defaultValue(null), - 'created_by_id' => $this->integer()->null()->defaultValue(null), + 'created_by_id' => $this->integer()->null()->defaultValue(null)->comment('The User'), ]); $this->addPrimaryKey('pk_blog_posts_uid', '{{%blog_posts}}', 'uid'); $this->createIndex('blog_posts_title_key', '{{%blog_posts}}', 'title', true); diff --git a/tests/specs/blog/migrations/m200000_000004_create_table_post_comments.php b/tests/specs/blog/migrations/m200000_000004_create_table_post_comments.php index c16766da..175a32b7 100644 --- a/tests/specs/blog/migrations/m200000_000004_create_table_post_comments.php +++ b/tests/specs/blog/migrations/m200000_000004_create_table_post_comments.php @@ -9,8 +9,8 @@ public function up() { $this->createTable('{{%post_comments}}', [ 'id' => $this->bigPrimaryKey(), - 'post_id' => $this->string(128)->notNull(), - 'author_id' => $this->integer()->notNull(), + 'post_id' => $this->string(128)->notNull()->comment('A blog post (uid used as pk for test purposes)'), + 'author_id' => $this->integer()->notNull()->comment('The User'), 0 => 'message json NOT NULL', 1 => 'meta_data json NOT NULL', 'created_at' => $this->integer()->notNull(), diff --git a/tests/specs/blog/migrations_maria_db/m200000_000002_create_table_blog_posts.php b/tests/specs/blog/migrations_maria_db/m200000_000002_create_table_blog_posts.php index b0b8e371..c7cc90fb 100644 --- a/tests/specs/blog/migrations_maria_db/m200000_000002_create_table_blog_posts.php +++ b/tests/specs/blog/migrations_maria_db/m200000_000002_create_table_blog_posts.php @@ -11,10 +11,10 @@ public function up() 0 => 'uid varchar(128) NOT NULL', 'title' => $this->string(255)->notNull(), 'slug' => $this->string(200)->null()->defaultValue(null), - 'category_id' => $this->integer()->notNull(), + 'category_id' => $this->integer()->notNull()->comment('Category of posts'), 'active' => $this->boolean()->notNull()->defaultValue(false), 'created_at' => $this->date()->null()->defaultValue(null), - 'created_by_id' => $this->integer()->null()->defaultValue(null), + 'created_by_id' => $this->integer()->null()->defaultValue(null)->comment('The User'), ]); $this->addPrimaryKey('pk_blog_posts_uid', '{{%blog_posts}}', 'uid'); $this->createIndex('blog_posts_title_key', '{{%blog_posts}}', 'title', true); diff --git a/tests/specs/blog/migrations_maria_db/m200000_000004_create_table_post_comments.php b/tests/specs/blog/migrations_maria_db/m200000_000004_create_table_post_comments.php index d4971f43..5883fa5b 100644 --- a/tests/specs/blog/migrations_maria_db/m200000_000004_create_table_post_comments.php +++ b/tests/specs/blog/migrations_maria_db/m200000_000004_create_table_post_comments.php @@ -9,8 +9,8 @@ public function up() { $this->createTable('{{%post_comments}}', [ 'id' => $this->bigPrimaryKey(), - 'post_id' => $this->string(128)->notNull(), - 'author_id' => $this->integer()->notNull(), + 'post_id' => $this->string(128)->notNull()->comment('A blog post (uid used as pk for test purposes)'), + 'author_id' => $this->integer()->notNull()->comment('The User'), 0 => 'message json NOT NULL DEFAULT \'[]\'', 1 => 'meta_data json NOT NULL DEFAULT \'[]\'', 'created_at' => $this->integer()->notNull(), diff --git a/tests/specs/blog/migrations_mysql_db/m200000_000002_create_table_blog_posts.php b/tests/specs/blog/migrations_mysql_db/m200000_000002_create_table_blog_posts.php index b0b8e371..c7cc90fb 100644 --- a/tests/specs/blog/migrations_mysql_db/m200000_000002_create_table_blog_posts.php +++ b/tests/specs/blog/migrations_mysql_db/m200000_000002_create_table_blog_posts.php @@ -11,10 +11,10 @@ public function up() 0 => 'uid varchar(128) NOT NULL', 'title' => $this->string(255)->notNull(), 'slug' => $this->string(200)->null()->defaultValue(null), - 'category_id' => $this->integer()->notNull(), + 'category_id' => $this->integer()->notNull()->comment('Category of posts'), 'active' => $this->boolean()->notNull()->defaultValue(false), 'created_at' => $this->date()->null()->defaultValue(null), - 'created_by_id' => $this->integer()->null()->defaultValue(null), + 'created_by_id' => $this->integer()->null()->defaultValue(null)->comment('The User'), ]); $this->addPrimaryKey('pk_blog_posts_uid', '{{%blog_posts}}', 'uid'); $this->createIndex('blog_posts_title_key', '{{%blog_posts}}', 'title', true); diff --git a/tests/specs/blog/migrations_mysql_db/m200000_000004_create_table_post_comments.php b/tests/specs/blog/migrations_mysql_db/m200000_000004_create_table_post_comments.php index c16766da..175a32b7 100644 --- a/tests/specs/blog/migrations_mysql_db/m200000_000004_create_table_post_comments.php +++ b/tests/specs/blog/migrations_mysql_db/m200000_000004_create_table_post_comments.php @@ -9,8 +9,8 @@ public function up() { $this->createTable('{{%post_comments}}', [ 'id' => $this->bigPrimaryKey(), - 'post_id' => $this->string(128)->notNull(), - 'author_id' => $this->integer()->notNull(), + 'post_id' => $this->string(128)->notNull()->comment('A blog post (uid used as pk for test purposes)'), + 'author_id' => $this->integer()->notNull()->comment('The User'), 0 => 'message json NOT NULL', 1 => 'meta_data json NOT NULL', 'created_at' => $this->integer()->notNull(), 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 8639040d..a7266997 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 @@ -11,10 +11,10 @@ public function safeUp() 0 => '"uid" varchar(128) NOT NULL', 'title' => $this->string(255)->notNull(), 'slug' => $this->string(200)->null()->defaultValue(null), - 'category_id' => $this->integer()->notNull(), + 'category_id' => $this->integer()->notNull()->comment('Category of posts'), 'active' => $this->boolean()->notNull()->defaultValue(false), 'created_at' => $this->date()->null()->defaultValue(null), - 'created_by_id' => $this->integer()->null()->defaultValue(null), + 'created_by_id' => $this->integer()->null()->defaultValue(null)->comment('The User'), ]); $this->addPrimaryKey('pk_blog_posts_uid', '{{%blog_posts}}', 'uid'); $this->createIndex('blog_posts_title_key', '{{%blog_posts}}', 'title', true); 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 2aa48549..1229677b 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 @@ -9,8 +9,8 @@ public function safeUp() { $this->createTable('{{%post_comments}}', [ 'id' => $this->bigPrimaryKey(), - 'post_id' => $this->string(128)->notNull(), - 'author_id' => $this->integer()->notNull(), + 'post_id' => $this->string(128)->notNull()->comment('A blog post (uid used as pk for test purposes)'), + 'author_id' => $this->integer()->notNull()->comment('The User'), 0 => '"message" json NOT NULL DEFAULT \'[]\'', 1 => '"meta_data" json NOT NULL DEFAULT \'[]\'', 'created_at' => $this->integer()->notNull(), diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php index 75553729..e2f7b4ad 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php @@ -9,7 +9,7 @@ public function up() { $this->dropForeignKey('fk_v2_comments_post_id_v2_posts_uid', '{{%v2_comments}}'); $this->dropForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}'); - $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')); + $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); $this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null()->defaultValue('')); diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php index 5861542c..130014e7 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php @@ -9,7 +9,7 @@ public function up() { $this->dropForeignKey('fk_v2_comments_post_id_v2_posts_uid', '{{%v2_comments}}'); $this->dropForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}'); - $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')); + $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); $this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null()->defaultValue('')); 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 99a896c6..452a4401 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 @@ -9,7 +9,7 @@ public function safeUp() { $this->dropForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}'); $this->dropForeignKey('fk_v2_comments_post_id_v2_posts_uid', '{{%v2_comments}}'); - $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)); + $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); $this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text'); $this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT"); diff --git a/tests/specs/fk_col_name/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php b/tests/specs/fk_col_name/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php index bb6aa050..5d3abaf3 100644 --- a/tests/specs/fk_col_name/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php +++ b/tests/specs/fk_col_name/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php @@ -10,7 +10,7 @@ public function up() $this->createTable('{{%webhooks}}', [ 'id' => $this->primaryKey(), 'name' => $this->text()->null(), - 'user_id' => $this->integer()->null()->defaultValue(null), + 'user_id' => $this->integer()->null()->defaultValue(null)->comment('Test model for model code generation that should not contain id column in rules'), 'redelivery_of' => $this->integer()->null()->defaultValue(null), ]); $this->addForeignKey('fk_webhooks_user_id_users_id', '{{%webhooks}}', 'user_id', '{{%users}}', 'id'); diff --git a/tests/specs/fk_col_name_index/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php b/tests/specs/fk_col_name_index/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php index 783d58b0..591905a0 100644 --- a/tests/specs/fk_col_name_index/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php +++ b/tests/specs/fk_col_name_index/app/migrations_mysql_db/m200000_000002_create_table_webhooks.php @@ -10,7 +10,7 @@ public function up() $this->createTable('{{%webhooks}}', [ 'id' => $this->primaryKey(), 'name' => $this->string(255)->null()->defaultValue(null), - 'user_id' => $this->integer()->null()->defaultValue(null), + 'user_id' => $this->integer()->null()->defaultValue(null)->comment('Test model for model code generation that should not contain id column in rules'), 'redelivery_of' => $this->integer()->null()->defaultValue(null), 'rd_abc_2' => $this->integer()->null()->defaultValue(null), ]); diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000000_create_table_accounts.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000000_create_table_accounts.php index 1606f2fc..ce7db4f9 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000000_create_table_accounts.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000000_create_table_accounts.php @@ -9,7 +9,7 @@ public function up() { $this->createTable('{{%accounts}}', [ 'id' => $this->primaryKey(), - 'name' => $this->string(128)->notNull(), + 'name' => $this->string(128)->notNull()->comment('account name'), 'paymentMethodName' => $this->text()->null(), ]); } diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000001_create_table_contacts.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000001_create_table_contacts.php index 5fcda9dc..a706a626 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000001_create_table_contacts.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/migrations_pgsql_db/m200000_000001_create_table_contacts.php @@ -9,7 +9,7 @@ public function up() { $this->createTable('{{%contacts}}', [ 'id' => $this->primaryKey(), - 'account_id' => $this->integer()->notNull(), + 'account_id' => $this->integer()->notNull()->comment('Account'), 'active' => $this->boolean()->null()->defaultValue(false), 'nickname' => $this->text()->null(), ]); diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php index 908bd998..7fe423f5 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php @@ -10,7 +10,7 @@ public function safeUp() $this->createTable('{{%b123s}}', [ 'id' => $this->primaryKey(), 'name' => $this->text()->null()->defaultValue(null), - 'c123_id' => $this->integer()->null()->defaultValue(null), + 'c123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), ]); $this->addForeignKey('fk_b123s_c123_id_c123s_id', '{{%b123s}}', 'c123_id', '{{%c123s}}', 'id'); } diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php index 73c70ae2..4b2aad61 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php @@ -10,7 +10,7 @@ public function safeUp() $this->createTable('{{%a123s}}', [ 'id' => $this->primaryKey(), 'name' => $this->text()->null()->defaultValue(null), - 'b123_id' => $this->integer()->null()->defaultValue(null), + 'b123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), ]); $this->addForeignKey('fk_a123s_b123_id_b123s_id', '{{%a123s}}', 'b123_id', '{{%b123s}}', 'id'); } diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php index 8eba95b2..843678d6 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php @@ -9,7 +9,7 @@ public function safeUp() { $this->createTable('{{%accounts}}', [ 'id' => $this->primaryKey(), - 'name' => $this->string(40)->notNull(), + 'name' => $this->string(40)->notNull()->comment('account name'), ]); } diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php index c830fe7e..871b473f 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php @@ -9,8 +9,8 @@ public function safeUp() { $this->createTable('{{%domains}}', [ 'id' => $this->primaryKey(), - 'name' => $this->string(128)->notNull(), - 'account_id' => $this->integer()->notNull(), + 'name' => $this->string(128)->notNull()->comment('domain or sub-domain name, in DNS syntax, IDN are converted'), + 'account_id' => $this->integer()->notNull()->comment('user account'), 0 => '"created_at" timestamp NOT NULL', ]); $this->addForeignKey('fk_domains_account_id_accounts_id', '{{%domains}}', 'account_id', '{{%accounts}}', 'id'); diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php index f8d58a41..59d643a4 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php @@ -10,7 +10,7 @@ public function safeUp() $this->createTable('{{%e123s}}', [ 'id' => $this->primaryKey(), 'name' => $this->text()->null()->defaultValue(null), - 'b123_id' => $this->integer()->null()->defaultValue(null), + 'b123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), ]); $this->addForeignKey('fk_e123s_b123_id_b123s_id', '{{%e123s}}', 'b123_id', '{{%b123s}}', 'id'); } diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php index 98ac2ad9..f880d830 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php @@ -9,14 +9,14 @@ public function safeUp() { $this->createTable('{{%routings}}', [ 'id' => $this->primaryKey(), - 'domain_id' => $this->integer()->notNull(), + 'domain_id' => $this->integer()->notNull()->comment('domain'), 'path' => $this->string(255)->null()->defaultValue(null), 'ssl' => $this->boolean()->null()->defaultValue(null), 'redirect_to_ssl' => $this->boolean()->null()->defaultValue(null), 'service' => $this->string(255)->null()->defaultValue(null), 0 => '"created_at" timestamp NULL DEFAULT NULL', - 'd123_id' => $this->integer()->null()->defaultValue(null), - 'a123_id' => $this->integer()->null()->defaultValue(null), + 'd123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), + 'a123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), ]); $this->addForeignKey('fk_routings_domain_id_domains_id', '{{%routings}}', 'domain_id', '{{%domains}}', 'id'); $this->addForeignKey('fk_routings_d123_id_d123s_id', '{{%routings}}', 'd123_id', '{{%d123s}}', 'id'); 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 1e27d8d6..7c0b936f 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,7 +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', + 10 => 'price decimal(10,2) NULL DEFAULT 0 COMMENT \'price in EUR\'', ]); } 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 3e9e8e02..50881e9d 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,7 +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', + 10 => 'price decimal(10,2) NULL DEFAULT 0 COMMENT \'price in EUR\'', ]); } 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 1e27d8d6..7c0b936f 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,7 +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', + 10 => 'price decimal(10,2) NULL DEFAULT 0 COMMENT \'price in EUR\'', ]); } 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 3e9e8e02..50881e9d 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,7 +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', + 10 => 'price decimal(10,2) NULL DEFAULT 0 COMMENT \'price in EUR\'', ]); } 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 1e27d8d6..7c0b936f 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,7 +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', + 10 => 'price decimal(10,2) NULL DEFAULT 0 COMMENT \'price in EUR\'', ]); } 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 3e9e8e02..50881e9d 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,7 +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', + 10 => 'price decimal(10,2) NULL DEFAULT 0 COMMENT \'price in EUR\'', ]); } diff --git a/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php b/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php index f0e8adc8..777920de 100644 --- a/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php +++ b/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php @@ -10,10 +10,10 @@ public function safeUp() $this->createTable('{{%postxes}}', [ 'id' => $this->primaryKey(), 'title' => $this->text()->null()->defaultValue(null), - 'user_id' => $this->integer()->null()->defaultValue(null), - 'user_2_id' => $this->integer()->null()->defaultValue(null), - 'user_3_id' => $this->integer()->null()->defaultValue(null), - 'user_4_id' => $this->integer()->null()->defaultValue(null), + 'user_id' => $this->integer()->null()->defaultValue(null)->comment('x on-x (update|delete) foreign key constraint'), + 'user_2_id' => $this->integer()->null()->defaultValue(null)->comment('x on-x (update|delete) foreign key constraint'), + 'user_3_id' => $this->integer()->null()->defaultValue(null)->comment('x on-x (update|delete) foreign key constraint'), + 'user_4_id' => $this->integer()->null()->defaultValue(null)->comment('x on-x (update|delete) foreign key constraint'), ]); $this->addForeignKey('fk_postxes_user_id_userxes_id', '{{%postxes}}', 'user_id', '{{%userxes}}', 'id', null, 'CASCADE'); $this->addForeignKey('fk_postxes_user_2_id_userxes_id', '{{%postxes}}', 'user_2_id', '{{%userxes}}', 'id', 'SET NULL', 'CASCADE'); From d4dd0740704c56f95043f43e49e36c39f66e8502 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Sep 2024 20:48:15 +0530 Subject: [PATCH 07/25] Add comments handling for PgSQL as it is different from MySQL - WIP --- src/lib/migrations/BaseMigrationBuilder.php | 3 +++ src/lib/migrations/MigrationRecordBuilder.php | 7 +++++++ .../migrations/PostgresMigrationBuilder.php | 14 +++++++++++--- .../index.yml | 13 +++++++++++-- tests/unit/IssueFixTest.php | 18 +++++++++++++++++- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index d4c49c21..8c9464f7 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -453,6 +453,9 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch } Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute(); + if (ApiGenerator::isPostgres()) { + Yii::$app->db->createCommand("comment on column $tmpTableName.$columnSchema->name is '$columnSchema->comment'")->execute(); + } $table = Yii::$app->db->getTableSchema($tmpTableName); diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index 98ee03b8..f70808b5 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -46,6 +46,8 @@ final class MigrationRecordBuilder public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN \"%s\" SET DATA TYPE %s')->execute();"; + public const PGSQL_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');"; + /** * @var \yii\db\Schema */ @@ -345,4 +347,9 @@ public static function makeString(array $codeColumns): string $codeColumns = '['.PHP_EOL.self::INDENT.' '.$codeColumns.PHP_EOL . self::INDENT.']'; return $codeColumns; } + + public function pgsqlCommentOnColumn($table, string $column, string $comment) + { + return sprintf(self::PGSQL_COMMENT_ON_COLUMN, $table, $column, $comment); + } } diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index b8c9324d..9b3cb225 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -9,7 +9,6 @@ use cebe\yii2openapi\lib\items\DbIndex; use yii\db\ColumnSchema; -use yii\helpers\VarDumper; use yii\helpers\ArrayHelper; final class PostgresMigrationBuilder extends BaseMigrationBuilder @@ -65,7 +64,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir if (!empty(array_intersect(['type', 'size' , 'dbType', 'phpType' - , 'precision', 'scale', 'unsigned' + , 'precision', 'scale', 'unsigned', 'comment' ], $changed))) { $addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType); $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired, $addUsing)); @@ -91,6 +90,15 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir $this->migration->addUpCode($upCode)->addDownCode($downCode, true); } } + + if (in_array('comment', $changed, true)) { +// if ($desired->comment) { + $this->migration->addUpCode($this->recordBuilder->pgsqlCommentOnColumn($tableName, $desired->name, $desired->comment)); +// } else { +// +// } + } + if ($isChangeToEnum) { $this->migration->addUpCode($this->recordBuilder->createEnum($tableName, $desired->name, $desired->enumValues), true); } @@ -127,7 +135,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues' , 'dbType', 'phpType' - , 'precision', 'scale', 'unsigned' + , 'precision', 'scale', 'unsigned', 'comment' ] as $attr) { if ($attr === 'defaultValue') { if ($this->isDefaultValueChanged($current, $desiredFromDb)) { diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index 83f3a948..a6178648 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -14,8 +14,10 @@ components: name: type: string description: desc - # colour: - # type: string + colour: + type: string + # maxLength: 255 + # x-db-type: varbinary # TODO description: type: string x-db-type: varchar @@ -28,6 +30,13 @@ components: name: type: integer description: desc + # description: + # type: string + # x-db-type: varchar + # description: desc 2 + g: + type: string + description: desc 3 paths: '/': diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 45ace04d..1022ab76 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -377,13 +377,29 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC // $this->checkFiles($actualFiles, $expectedFiles); $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + + + $this->changeDbToPgsql(); + $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + $this->createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + $this->runGenerator($testFile, 'pgsql'); + // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + // 'recursive' => true, + // ]); + // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql"), [ + // 'recursive' => true, + // ]); + // $this->checkFiles($actualFiles, $expectedFiles); + + $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); } private function createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() { Yii::$app->db->createCommand()->createTable('{{%animals}}', [ 'id' => 'pk', - 'name' => 'text comment "the name"', + 'name' => 'text ', # comment "the name" + 'g' => 'text' ])->execute(); } From b18c521a6fb0ee7564c1317ac6dd9b16bf938c3f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sun, 29 Sep 2024 12:44:38 +0530 Subject: [PATCH 08/25] Add comments handling for PgSQL as it is different from MySQL - 2 --- src/lib/migrations/BaseMigrationBuilder.php | 4 ++++ src/lib/migrations/MysqlMigrationBuilder.php | 6 ++++++ .../migrations/PostgresMigrationBuilder.php | 12 +++++++++++ .../index.yml | 8 ++++---- tests/unit/IssueFixTest.php | 20 +++++++++---------- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 8c9464f7..a413d3a7 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -161,6 +161,8 @@ public function buildFresh():MigrationModel } } + $this->addCommentsMigration(); + return $this->migration; } @@ -275,6 +277,8 @@ abstract public static function getColumnSchemaBuilderClass(): string; */ abstract protected function findTableIndexes():array; + abstract public function addCommentsMigration(); + protected function buildIndexChanges():void { $haveIndexes = $this->findTableIndexes(); diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 42354df3..b65d8719 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -159,4 +159,10 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch $desired->size = $current->size; } } + + public function addCommentsMigration() + { + // nothing to do here as comments can be defined in same statement as of alter/add column in MySQL + // this method is only for PgSQL + } } diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index 9b3cb225..ef81ad39 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -256,4 +256,16 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch $desired->size = $current->size; } } + + public function addCommentsMigration() + { + $tableAlias = $this->model->getTableAlias(); + foreach ($this->newColumns as $column) { + if($column->comment) { + $this->migration + ->addUpCode($this->recordBuilder->pgsqlCommentOnColumn($tableAlias, $column->name, $column->comment)) + ; + } + } + } } diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index a6178648..cf6728f9 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -14,13 +14,13 @@ components: name: type: string description: desc - colour: - type: string +# colour: +# type: string # maxLength: 255 # x-db-type: varbinary # TODO description: - type: string - x-db-type: varchar + type: number + x-db-type: double precision description: desc 2 Animal: type: object diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 1022ab76..01d751a9 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -364,8 +364,9 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim() // https://github.com/php-openapi/yii2-openapi/issues/60 public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() { - $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); - $this->createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + // MySQL + $this->deleteTableFor60DescriptionOfAProperty(); + $this->createTableFor60DescriptionOfAProperty(); $testFile = Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php"); $this->runGenerator($testFile); // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ @@ -375,13 +376,13 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC // 'recursive' => true, // ]); // $this->checkFiles($actualFiles, $expectedFiles); - - $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + $this->deleteTableFor60DescriptionOfAProperty(); + // PgSQL $this->changeDbToPgsql(); - $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); - $this->createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + $this->deleteTableFor60DescriptionOfAProperty(); + $this->createTableFor60DescriptionOfAProperty(); $this->runGenerator($testFile, 'pgsql'); // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ // 'recursive' => true, @@ -390,11 +391,10 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC // 'recursive' => true, // ]); // $this->checkFiles($actualFiles, $expectedFiles); - - $this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment(); + $this->deleteTableFor60DescriptionOfAProperty(); } - private function createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() + private function createTableFor60DescriptionOfAProperty() { Yii::$app->db->createCommand()->createTable('{{%animals}}', [ 'id' => 'pk', @@ -403,7 +403,7 @@ private function createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbT ])->execute(); } - private function deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment() + private function deleteTableFor60DescriptionOfAProperty() { Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%animals}}')->execute(); } From 72a7a6ae565a0b78a16d36a4b3e87be81529d279 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sun, 29 Sep 2024 19:49:32 +0530 Subject: [PATCH 09/25] Add comments handling for PgSQL as it is different from MySQL - done --- src/lib/migrations/BaseMigrationBuilder.php | 8 +++--- src/lib/migrations/MigrationRecordBuilder.php | 13 +++++++--- src/lib/migrations/MysqlMigrationBuilder.php | 6 ++--- .../migrations/PostgresMigrationBuilder.php | 25 +++++++++++-------- .../index.yml | 13 ++++++++-- tests/unit/IssueFixTest.php | 14 +++++++++-- 6 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index a413d3a7..ad37a942 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -161,7 +161,7 @@ public function buildFresh():MigrationModel } } - $this->addCommentsMigration(); + $this->handleCommentsMigration(); return $this->migration; } @@ -277,7 +277,7 @@ abstract public static function getColumnSchemaBuilderClass(): string; */ abstract protected function findTableIndexes():array; - abstract public function addCommentsMigration(); + abstract public function handleCommentsMigration(); protected function buildIndexChanges():void { @@ -457,8 +457,8 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch } Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute(); - if (ApiGenerator::isPostgres()) { - Yii::$app->db->createCommand("comment on column $tmpTableName.$columnSchema->name is '$columnSchema->comment'")->execute(); + if (ApiGenerator::isPostgres() && $columnSchema->comment) { + Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS '$columnSchema->comment'")->execute(); } $table = Yii::$app->db->getTableSchema($tmpTableName); diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index f70808b5..cbe0af72 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -46,7 +46,9 @@ final class MigrationRecordBuilder public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN \"%s\" SET DATA TYPE %s')->execute();"; - public const PGSQL_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');"; + public const ADD_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');"; + + public const DROP_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropCommentFromColumn('%s', '%s');"; /** * @var \yii\db\Schema @@ -348,8 +350,13 @@ public static function makeString(array $codeColumns): string return $codeColumns; } - public function pgsqlCommentOnColumn($table, string $column, string $comment) + public function addCommentOnColumn($table, string $column, string $comment) + { + return sprintf(self::ADD_COMMENT_ON_COLUMN, $table, $column, $comment); + } + + public function dropCommentOnColumn($table, string $column) { - return sprintf(self::PGSQL_COMMENT_ON_COLUMN, $table, $column, $comment); + return sprintf(self::DROP_COMMENT_ON_COLUMN, $table, $column); } } diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index b65d8719..881068b5 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -14,9 +14,7 @@ use yii\db\ColumnSchema; use yii\db\IndexConstraint; use yii\db\Schema; -use \Yii; use yii\helpers\ArrayHelper; -use yii\helpers\VarDumper; final class MysqlMigrationBuilder extends BaseMigrationBuilder { @@ -56,7 +54,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues' , 'dbType', 'phpType' - , 'precision', 'scale', 'unsigned' + , 'precision', 'scale', 'unsigned', 'comment' ] as $attr) { if ($attr === 'defaultValue') { if ($this->isDefaultValueChanged($current, $desiredFromDb)) { @@ -160,7 +158,7 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch } } - public function addCommentsMigration() + public function handleCommentsMigration() { // nothing to do here as comments can be defined in same statement as of alter/add column in MySQL // this method is only for PgSQL diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index ef81ad39..b072d8b1 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -64,7 +64,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir if (!empty(array_intersect(['type', 'size' , 'dbType', 'phpType' - , 'precision', 'scale', 'unsigned', 'comment' + , 'precision', 'scale', 'unsigned' ], $changed))) { $addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType); $this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired, $addUsing)); @@ -91,12 +91,14 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir } } - if (in_array('comment', $changed, true)) { -// if ($desired->comment) { - $this->migration->addUpCode($this->recordBuilder->pgsqlCommentOnColumn($tableName, $desired->name, $desired->comment)); -// } else { -// -// } + if (in_array('comment', $changed, true)) { // TODO + if ($desired->comment) { + $this->migration->addUpCode($this->recordBuilder->addCommentOnColumn($tableName, $desired->name, $desired->comment)); + $this->migration->addDownCode($this->recordBuilder->dropCommentOnColumn($tableName, $desired->name)); + } else { + $this->migration->addUpCode($this->recordBuilder->dropCommentOnColumn($tableName, $desired->name)); + $this->migration->addDownCode($this->recordBuilder->addCommentOnColumn($tableName, $desired->name, $current->comment)); + } } if ($isChangeToEnum) { @@ -257,13 +259,16 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch } } - public function addCommentsMigration() + public function handleCommentsMigration() { $tableAlias = $this->model->getTableAlias(); foreach ($this->newColumns as $column) { - if($column->comment) { + if ($column->comment) { + $this->migration + ->addUpCode($this->recordBuilder->addCommentOnColumn($tableAlias, $column->name, $column->comment)); + } else { $this->migration - ->addUpCode($this->recordBuilder->pgsqlCommentOnColumn($tableAlias, $column->name, $column->comment)) + ->addUpCode($this->recordBuilder->dropCommentOnColumn($tableAlias, $column->name)) ; } } diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index cf6728f9..f4637ada 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -29,14 +29,23 @@ components: type: integer name: type: integer - description: desc + # description: desc # description: # type: string # x-db-type: varchar # description: desc 2 g: type: string - description: desc 3 + description: desc for g + g2: + type: string + description: changed comment on g2 col + g3: + type: string + description: the comment on g3 col remains same + g4: + type: integer + description: data type changes but comment remains same paths: '/': diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 01d751a9..fc5271b4 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -369,6 +369,7 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC $this->createTableFor60DescriptionOfAProperty(); $testFile = Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php"); $this->runGenerator($testFile); + $this->runActualMigrations(); // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ // 'recursive' => true, // ]); @@ -384,6 +385,7 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC $this->deleteTableFor60DescriptionOfAProperty(); $this->createTableFor60DescriptionOfAProperty(); $this->runGenerator($testFile, 'pgsql'); + $this->runActualMigrations('pgsql'); // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ // 'recursive' => true, // ]); @@ -391,7 +393,7 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC // 'recursive' => true, // ]); // $this->checkFiles($actualFiles, $expectedFiles); - $this->deleteTableFor60DescriptionOfAProperty(); +// $this->deleteTableFor60DescriptionOfAProperty(); } private function createTableFor60DescriptionOfAProperty() @@ -399,8 +401,16 @@ private function createTableFor60DescriptionOfAProperty() Yii::$app->db->createCommand()->createTable('{{%animals}}', [ 'id' => 'pk', 'name' => 'text ', # comment "the name" - 'g' => 'text' + 'g' => 'text', + 'g2' => 'text', + 'g3' => 'text', + 'g4' => 'text', ])->execute(); + + Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'name', 'the comment on name col')->execute(); + Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'g2', 'the comment on g2 col')->execute(); + Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'g3', 'the comment on g3 col remains same')->execute(); + Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'g4', 'data type changes but comment remains same')->execute(); } private function deleteTableFor60DescriptionOfAProperty() From 7d557c49a65bb89871e2c4e82a8339987c66075e Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sun, 29 Sep 2024 20:19:43 +0530 Subject: [PATCH 10/25] Complete the test --- src/lib/migrations/BaseMigrationBuilder.php | 4 +- .../index.yml | 3 + .../m200000_000000_change_table_animals.php | 27 ++++ .../m200000_000001_create_table_fruits.php | 21 +++ .../mysql/models/Animal.php | 10 ++ .../mysql/models/AnimalFaker.php | 46 ++++++ .../mysql/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../mysql/models/Fruit.php | 10 ++ .../mysql/models/FruitFaker.php | 42 +++++ .../mysql/models/base/Animal.php | 40 +++++ .../mysql/models/base/Fruit.php | 32 ++++ .../m200000_000000_change_table_animals.php | 29 ++++ .../m200000_000001_create_table_fruits.php | 24 +++ .../pgsql/models/Animal.php | 10 ++ .../pgsql/models/AnimalFaker.php | 46 ++++++ .../pgsql/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../pgsql/models/Fruit.php | 10 ++ .../pgsql/models/FruitFaker.php | 42 +++++ .../pgsql/models/base/Animal.php | 40 +++++ .../pgsql/models/base/Fruit.php | 32 ++++ tests/unit/IssueFixTest.php | 32 ++-- 21 files changed, 771 insertions(+), 17 deletions(-) create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000000_change_table_animals.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/Animal.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/AnimalFaker.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/BaseModelFaker.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/Fruit.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/FruitFaker.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Animal.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000000_change_table_animals.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/Animal.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/AnimalFaker.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/BaseModelFaker.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/Fruit.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/FruitFaker.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Animal.php create mode 100644 tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index ad37a942..8a49de34 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -453,12 +453,12 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch }, $allEnumValues); Yii::$app->db->createCommand( 'CREATE TYPE '.$tmpEnumName($columnSchema->name).' AS ENUM('.implode(', ', $allEnumValues).')' - )->execute(); + )->execute(); // TODO quote value } Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute(); if (ApiGenerator::isPostgres() && $columnSchema->comment) { - Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS '$columnSchema->comment'")->execute(); + Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS '$columnSchema->comment'")->execute(); // TODO quote comment } $table = Yii::$app->db->getTableSchema($tmpTableName); diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index f4637ada..a034947e 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -46,6 +46,9 @@ components: g4: type: integer description: data type changes but comment remains same + new_col: + type: string + description: new col added paths: '/': diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000000_change_table_animals.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000000_change_table_animals.php new file mode 100644 index 00000000..bff6c78d --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000000_change_table_animals.php @@ -0,0 +1,27 @@ +addColumn('{{%animals}}', 'new_col', $this->text()->null()->comment('new col added')); + $this->dropColumn('{{%animals}}', 'drop_col'); + $this->alterColumn('{{%animals}}', 'name', $this->integer()->null()->defaultValue(null)); + $this->alterColumn('{{%animals}}', 'g', $this->text()->null()->comment('desc for g')); + $this->alterColumn('{{%animals}}', 'g2', $this->text()->null()->comment('changed comment on g2 col')); + $this->alterColumn('{{%animals}}', 'g4', $this->integer()->null()->defaultValue(null)->comment('data type changes but comment remains same')); + } + + public function down() + { + $this->alterColumn('{{%animals}}', 'g4', $this->text()->null()->comment('data type changes but comment remains same')); + $this->alterColumn('{{%animals}}', 'g2', $this->text()->null()->comment('the comment on g2 col')); + $this->alterColumn('{{%animals}}', 'g', $this->text()->null()); + $this->alterColumn('{{%animals}}', 'name', $this->text()->null()->comment('the comment on name col')); + $this->addColumn('{{%animals}}', 'drop_col', $this->text()->null()); + $this->dropColumn('{{%animals}}', 'new_col'); + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php new file mode 100644 index 00000000..4fa328d9 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php @@ -0,0 +1,21 @@ +createTable('{{%fruits}}', [ + 'id' => $this->primaryKey(), + 'name' => $this->text()->null()->comment('desc'), + 0 => 'description double precision NULL DEFAULT NULL COMMENT \'desc 2\'', + ]); + } + + public function down() + { + $this->dropTable('{{%fruits}}'); + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/Animal.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/Animal.php new file mode 100644 index 00000000..92caf2f3 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/Animal.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 Animal(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->numberBetween(0, 1000000); + $model->g = $faker->sentence; + $model->g2 = $faker->sentence; + $model->g3 = $faker->sentence; + $model->g4 = $faker->numberBetween(0, 1000000); + $model->new_col = $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/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/BaseModelFaker.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/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/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/Fruit.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/Fruit.php new file mode 100644 index 00000000..c74c53d9 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/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; + $model->description = $faker->randomFloat(); + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Animal.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Animal.php new file mode 100644 index 00000000..5fd077a3 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Animal.php @@ -0,0 +1,40 @@ + [['g', 'g2', 'g3', 'new_col'], 'trim'], + 'name_integer' => [['name'], 'integer'], + 'g_string' => [['g'], 'string'], + 'g2_string' => [['g2'], 'string'], + 'g3_string' => [['g3'], 'string'], + 'g4_integer' => [['g4'], 'integer'], + 'new_col_string' => [['new_col'], 'string'], + ]; + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php new file mode 100644 index 00000000..da1edf2f --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php @@ -0,0 +1,32 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + 'description_double' => [['description'], 'double'], + ]; + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000000_change_table_animals.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000000_change_table_animals.php new file mode 100644 index 00000000..ede74a69 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000000_change_table_animals.php @@ -0,0 +1,29 @@ +addColumn('{{%animals}}', 'new_col', $this->text()->null()->defaultValue(null)->comment('new col added')); + $this->dropColumn('{{%animals}}', 'drop_col'); + $this->alterColumn('{{%animals}}', 'name', 'int4 NULL USING "name"::int4'); + $this->dropCommentFromColumn('{{%animals}}', 'name'); + $this->addCommentOnColumn('{{%animals}}', 'g', 'desc for g'); + $this->addCommentOnColumn('{{%animals}}', 'g2', 'changed comment on g2 col'); + $this->alterColumn('{{%animals}}', 'g4', 'int4 NULL USING "g4"::int4'); + } + + public function safeDown() + { + $this->alterColumn('{{%animals}}', 'g4', 'text NULL USING "g4"::text'); + $this->dropCommentFromColumn('{{%animals}}', 'g2'); + $this->dropCommentFromColumn('{{%animals}}', 'g'); + $this->addCommentOnColumn('{{%animals}}', 'name', 'the comment on name col'); + $this->alterColumn('{{%animals}}', 'name', 'text NULL USING "name"::text'); + $this->addColumn('{{%animals}}', 'drop_col', $this->text()->null()->defaultValue(null)); + $this->dropColumn('{{%animals}}', 'new_col'); + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php new file mode 100644 index 00000000..de417197 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php @@ -0,0 +1,24 @@ +createTable('{{%fruits}}', [ + 'id' => $this->primaryKey(), + 'name' => $this->text()->null()->defaultValue(null)->comment('desc'), + 0 => '"description" double precision NULL DEFAULT NULL', + ]); + $this->dropCommentFromColumn('{{%fruits}}', 'id'); + $this->addCommentOnColumn('{{%fruits}}', 'name', 'desc'); + $this->addCommentOnColumn('{{%fruits}}', 'description', 'desc 2'); + } + + public function safeDown() + { + $this->dropTable('{{%fruits}}'); + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/Animal.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/Animal.php new file mode 100644 index 00000000..92caf2f3 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/Animal.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 Animal(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->numberBetween(0, 1000000); + $model->g = $faker->sentence; + $model->g2 = $faker->sentence; + $model->g3 = $faker->sentence; + $model->g4 = $faker->numberBetween(0, 1000000); + $model->new_col = $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/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/BaseModelFaker.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/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/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/Fruit.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/Fruit.php new file mode 100644 index 00000000..c74c53d9 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/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; + $model->description = $faker->randomFloat(); + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Animal.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Animal.php new file mode 100644 index 00000000..5fd077a3 --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Animal.php @@ -0,0 +1,40 @@ + [['g', 'g2', 'g3', 'new_col'], 'trim'], + 'name_integer' => [['name'], 'integer'], + 'g_string' => [['g'], 'string'], + 'g2_string' => [['g2'], 'string'], + 'g3_string' => [['g3'], 'string'], + 'g4_integer' => [['g4'], 'integer'], + 'new_col_string' => [['new_col'], 'string'], + ]; + } +} diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php new file mode 100644 index 00000000..da1edf2f --- /dev/null +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php @@ -0,0 +1,32 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + 'description_double' => [['description'], 'double'], + ]; + } +} diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index fc5271b4..2287b762 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -370,13 +370,13 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC $testFile = Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php"); $this->runGenerator($testFile); $this->runActualMigrations(); - // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ - // 'recursive' => true, - // ]); - // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql"), [ - // 'recursive' => true, - // ]); - // $this->checkFiles($actualFiles, $expectedFiles); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); $this->deleteTableFor60DescriptionOfAProperty(); @@ -386,14 +386,15 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC $this->createTableFor60DescriptionOfAProperty(); $this->runGenerator($testFile, 'pgsql'); $this->runActualMigrations('pgsql'); - // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ - // 'recursive' => true, - // ]); - // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql"), [ - // 'recursive' => true, - // ]); - // $this->checkFiles($actualFiles, $expectedFiles); -// $this->deleteTableFor60DescriptionOfAProperty(); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + 'except' => ['migrations_mysql_db'] + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->deleteTableFor60DescriptionOfAProperty(); } private function createTableFor60DescriptionOfAProperty() @@ -405,6 +406,7 @@ private function createTableFor60DescriptionOfAProperty() 'g2' => 'text', 'g3' => 'text', 'g4' => 'text', + 'drop_col' => 'text', ])->execute(); Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'name', 'the comment on name col')->execute(); From d0415fbe03c636880d8a68a40ee48098f2e4248a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 30 Sep 2024 18:53:35 +0530 Subject: [PATCH 11/25] Fix bug and failing tests --- src/lib/migrations/PostgresMigrationBuilder.php | 4 ---- .../m200000_000002_create_table_blog_posts.php | 2 ++ .../m200000_000004_create_table_post_comments.php | 2 ++ .../m200000_000000_change_table_v2_posts.php | 4 ++-- .../m200000_000005_change_table_v2_comments.php | 2 ++ .../m200000_000000_change_table_v2_posts.php | 4 ++-- .../m200000_000005_change_table_v2_comments.php | 2 ++ .../m200000_000000_change_table_v2_posts.php | 4 ++++ .../m200000_000005_change_table_v2_comments.php | 2 ++ .../m200000_000001_create_table_fruits.php | 1 - .../migrations_pgsql_db/m200000_000001_create_table_b123s.php | 1 + .../migrations_pgsql_db/m200000_000002_create_table_a123s.php | 1 + .../m200000_000003_create_table_accounts.php | 1 + .../m200000_000005_create_table_domains.php | 2 ++ .../migrations_pgsql_db/m200000_000006_create_table_e123s.php | 1 + .../m200000_000007_create_table_routings.php | 3 +++ .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000003_create_table_pristines.php | 1 + .../m200000_000001_create_table_postxes.php | 4 ++++ 20 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index b072d8b1..cc205584 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -266,10 +266,6 @@ public function handleCommentsMigration() if ($column->comment) { $this->migration ->addUpCode($this->recordBuilder->addCommentOnColumn($tableAlias, $column->name, $column->comment)); - } else { - $this->migration - ->addUpCode($this->recordBuilder->dropCommentOnColumn($tableAlias, $column->name)) - ; } } } 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 a7266997..9dbe55cf 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 @@ -21,6 +21,8 @@ public function safeUp() $this->createIndex('blog_posts_slug_key', '{{%blog_posts}}', 'slug', true); $this->addForeignKey('fk_blog_posts_category_id_categories_id', '{{%blog_posts}}', 'category_id', '{{%categories}}', 'id'); $this->addForeignKey('fk_blog_posts_created_by_id_users_id', '{{%blog_posts}}', 'created_by_id', '{{%users}}', 'id'); + $this->addCommentOnColumn('{{%blog_posts}}', 'category_id', 'Category of posts'); + $this->addCommentOnColumn('{{%blog_posts}}', 'created_by_id', 'The User'); } public function safeDown() 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 1229677b..2d924e52 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 @@ -17,6 +17,8 @@ public function safeUp() ]); $this->addForeignKey('fk_post_comments_post_id_blog_posts_uid', '{{%post_comments}}', 'post_id', '{{%blog_posts}}', 'uid'); $this->addForeignKey('fk_post_comments_author_id_users_id', '{{%post_comments}}', 'author_id', '{{%users}}', 'id'); + $this->addCommentOnColumn('{{%post_comments}}', 'post_id', 'A blog post (uid used as pk for test purposes)'); + $this->addCommentOnColumn('{{%post_comments}}', 'author_id', 'The User'); } public function safeDown() diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php index 5c43dabf..33bdf987 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 @@ -10,9 +10,9 @@ public function up() $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\' AFTER slug'); $this->dropColumn('{{%v2_posts}}', 'uid'); - $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); + $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()->comment('Category of posts')); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); - $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)); + $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)->comment('The User')); $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); } diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php index e2f7b4ad..3494daeb 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php @@ -11,6 +11,7 @@ public function up() $this->dropForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}'); $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); + $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger(20)->notNull()->comment('A blog post (uid used as pk for test purposes)')); $this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null()->defaultValue('')); $this->alterColumn('{{%v2_comments}}', 'created_at', $this->timestamp()->notNull()); @@ -25,6 +26,7 @@ public function down() $this->alterColumn('{{%v2_comments}}', 'created_at', $this->integer(11)->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'json NOT NULL DEFAULT \'[]\''); $this->alterColumn('{{%v2_comments}}', 'message', 'json NOT NULL DEFAULT \'[]\''); + $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger(20)->notNull()); $this->addColumn('{{%v2_comments}}', 'author_id', $this->integer(11)->notNull()); $this->dropColumn('{{%v2_comments}}', 'user_id'); $this->addForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}', 'id', 'v2_users', 'author_id'); diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php index be309829..eec5ee6b 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 @@ -10,9 +10,9 @@ public function up() $this->addColumn('{{%v2_posts}}', 'id', $this->bigPrimaryKey()); $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\' AFTER slug'); $this->dropColumn('{{%v2_posts}}', 'uid'); - $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); + $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()->comment('Category of posts')); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); - $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)); + $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)->comment('The User')); $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); } diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php index 130014e7..0fd92aa1 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php @@ -11,6 +11,7 @@ public function up() $this->dropForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}'); $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); + $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger()->notNull()->comment('A blog post (uid used as pk for test purposes)')); $this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null()->defaultValue('')); $this->alterColumn('{{%v2_comments}}', 'created_at', $this->timestamp()->notNull()); @@ -25,6 +26,7 @@ public function down() $this->alterColumn('{{%v2_comments}}', 'created_at', $this->integer()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'json NOT NULL'); $this->alterColumn('{{%v2_comments}}', 'message', 'json NOT NULL'); + $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger()->notNull()); $this->addColumn('{{%v2_comments}}', 'author_id', $this->integer()->notNull()); $this->dropColumn('{{%v2_comments}}', 'user_id'); $this->addForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}', 'id', 'v2_users', 'author_id'); diff --git a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php index 5188983b..56755b58 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,15 +12,19 @@ public function safeUp() $this->addColumn('{{%v2_posts}}', 'lang', '"enum_itt_v2_posts_lang" NULL DEFAULT \'ru\''); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'category_id', 'int8 NOT NULL USING "category_id"::int8'); + $this->addCommentOnColumn('{{%v2_posts}}', 'category_id', 'Category of posts'); $this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT"); $this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int8 NULL USING "created_by_id"::int8'); + $this->addCommentOnColumn('{{%v2_posts}}', 'created_by_id', 'The User'); $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); } public function safeDown() { $this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); + $this->dropCommentFromColumn('{{%v2_posts}}', 'created_by_id'); $this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int4 NULL USING "created_by_id"::int4'); + $this->dropCommentFromColumn('{{%v2_posts}}', 'category_id'); $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'); 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 452a4401..a77155bd 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,6 +11,7 @@ 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)->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); + $this->addCommentOnColumn('{{%v2_comments}}', 'post_id', 'A blog post (uid used as pk for test purposes)'); $this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text'); $this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT"); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'varchar(300) NULL USING "meta_data"::varchar'); @@ -28,6 +29,7 @@ public function safeDown() $this->alterColumn('{{%v2_comments}}', 'created_at', 'int4 NOT NULL USING "created_at"::int4'); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'jsonb NOT NULL USING "meta_data"::jsonb'); $this->alterColumn('{{%v2_comments}}', 'message', 'jsonb NOT NULL USING "message"::jsonb'); + $this->dropCommentFromColumn('{{%v2_comments}}', 'post_id'); $this->addColumn('{{%v2_comments}}', 'author_id', $this->integer()->notNull()); $this->dropColumn('{{%v2_comments}}', 'user_id'); $this->alterColumn('{{%v2_comments}}', 'message', "SET DEFAULT '[]'"); diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php index de417197..b42b2822 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php @@ -12,7 +12,6 @@ public function safeUp() 'name' => $this->text()->null()->defaultValue(null)->comment('desc'), 0 => '"description" double precision NULL DEFAULT NULL', ]); - $this->dropCommentFromColumn('{{%fruits}}', 'id'); $this->addCommentOnColumn('{{%fruits}}', 'name', 'desc'); $this->addCommentOnColumn('{{%fruits}}', 'description', 'desc 2'); } diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php index 7fe423f5..688d4e58 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000001_create_table_b123s.php @@ -13,6 +13,7 @@ public function safeUp() 'c123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), ]); $this->addForeignKey('fk_b123s_c123_id_c123s_id', '{{%b123s}}', 'c123_id', '{{%c123s}}', 'id'); + $this->addCommentOnColumn('{{%b123s}}', 'c123_id', 'desc'); } public function safeDown() diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php index 4b2aad61..f93343c6 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000002_create_table_a123s.php @@ -13,6 +13,7 @@ public function safeUp() 'b123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), ]); $this->addForeignKey('fk_a123s_b123_id_b123s_id', '{{%a123s}}', 'b123_id', '{{%b123s}}', 'id'); + $this->addCommentOnColumn('{{%a123s}}', 'b123_id', 'desc'); } public function safeDown() diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php index 843678d6..271f8a7b 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000003_create_table_accounts.php @@ -11,6 +11,7 @@ public function safeUp() 'id' => $this->primaryKey(), 'name' => $this->string(40)->notNull()->comment('account name'), ]); + $this->addCommentOnColumn('{{%accounts}}', 'name', 'account name'); } public function safeDown() diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php index 871b473f..c5a0e7f2 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000005_create_table_domains.php @@ -14,6 +14,8 @@ public function safeUp() 0 => '"created_at" timestamp NOT NULL', ]); $this->addForeignKey('fk_domains_account_id_accounts_id', '{{%domains}}', 'account_id', '{{%accounts}}', 'id'); + $this->addCommentOnColumn('{{%domains}}', 'name', 'domain or sub-domain name, in DNS syntax, IDN are converted'); + $this->addCommentOnColumn('{{%domains}}', 'account_id', 'user account'); } public function safeDown() diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php index 59d643a4..55cb62af 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000006_create_table_e123s.php @@ -13,6 +13,7 @@ public function safeUp() 'b123_id' => $this->integer()->null()->defaultValue(null)->comment('desc'), ]); $this->addForeignKey('fk_e123s_b123_id_b123s_id', '{{%e123s}}', 'b123_id', '{{%b123s}}', 'id'); + $this->addCommentOnColumn('{{%e123s}}', 'b123_id', 'desc'); } public function safeDown() diff --git a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php index f880d830..bd09ae48 100644 --- a/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php +++ b/tests/specs/relations_in_faker/app/migrations_pgsql_db/m200000_000007_create_table_routings.php @@ -21,6 +21,9 @@ public function safeUp() $this->addForeignKey('fk_routings_domain_id_domains_id', '{{%routings}}', 'domain_id', '{{%domains}}', 'id'); $this->addForeignKey('fk_routings_d123_id_d123s_id', '{{%routings}}', 'd123_id', '{{%d123s}}', 'id'); $this->addForeignKey('fk_routings_a123_id_a123s_id', '{{%routings}}', 'a123_id', '{{%a123s}}', 'id'); + $this->addCommentOnColumn('{{%routings}}', 'domain_id', 'domain'); + $this->addCommentOnColumn('{{%routings}}', 'd123_id', 'desc'); + $this->addCommentOnColumn('{{%routings}}', 'a123_id', 'desc'); } public function safeDown() 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 aab6bb88..7fa8a01f 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 @@ -21,6 +21,7 @@ public function safeUp() 9 => '"col_11" text NULL DEFAULT NULL', 10 => '"price" decimal(10,2) NULL DEFAULT 0', ]); + $this->addCommentOnColumn('{{%pristines}}', 'price', 'price in EUR'); } public function safeDown() 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 aab6bb88..7fa8a01f 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 @@ -21,6 +21,7 @@ public function safeUp() 9 => '"col_11" text NULL DEFAULT NULL', 10 => '"price" decimal(10,2) NULL DEFAULT 0', ]); + $this->addCommentOnColumn('{{%pristines}}', 'price', 'price in EUR'); } 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 aab6bb88..7fa8a01f 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 @@ -21,6 +21,7 @@ public function safeUp() 9 => '"col_11" text NULL DEFAULT NULL', 10 => '"price" decimal(10,2) NULL DEFAULT 0', ]); + $this->addCommentOnColumn('{{%pristines}}', 'price', 'price in EUR'); } public function safeDown() diff --git a/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php b/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php index 777920de..5593db95 100644 --- a/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php +++ b/tests/specs/x_on_x_fk_constraint/app/migrations_pgsql_db/m200000_000001_create_table_postxes.php @@ -19,6 +19,10 @@ public function safeUp() $this->addForeignKey('fk_postxes_user_2_id_userxes_id', '{{%postxes}}', 'user_2_id', '{{%userxes}}', 'id', 'SET NULL', 'CASCADE'); $this->addForeignKey('fk_postxes_user_3_id_userxes_id', '{{%postxes}}', 'user_3_id', '{{%userxes}}', 'id', 'SET NULL'); $this->addForeignKey('fk_postxes_user_4_id_userxes_id', '{{%postxes}}', 'user_4_id', '{{%userxes}}', 'id'); + $this->addCommentOnColumn('{{%postxes}}', 'user_id', 'x on-x (update|delete) foreign key constraint'); + $this->addCommentOnColumn('{{%postxes}}', 'user_2_id', 'x on-x (update|delete) foreign key constraint'); + $this->addCommentOnColumn('{{%postxes}}', 'user_3_id', 'x on-x (update|delete) foreign key constraint'); + $this->addCommentOnColumn('{{%postxes}}', 'user_4_id', 'x on-x (update|delete) foreign key constraint'); } public function safeDown() From fa47121e01bfdab94ce6b1bddc80e2edfbcd6141 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 30 Sep 2024 19:27:59 +0530 Subject: [PATCH 12/25] Rename function and constant --- src/lib/migrations/MigrationRecordBuilder.php | 8 ++++---- src/lib/migrations/PostgresMigrationBuilder.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index cbe0af72..32cc8885 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -48,7 +48,7 @@ final class MigrationRecordBuilder public const ADD_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');"; - public const DROP_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropCommentFromColumn('%s', '%s');"; + public const DROP_COMMENT_FROM_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropCommentFromColumn('%s', '%s');"; /** * @var \yii\db\Schema @@ -350,13 +350,13 @@ public static function makeString(array $codeColumns): string return $codeColumns; } - public function addCommentOnColumn($table, string $column, string $comment) + public function addCommentOnColumn($table, string $column, string $comment): string { return sprintf(self::ADD_COMMENT_ON_COLUMN, $table, $column, $comment); } - public function dropCommentOnColumn($table, string $column) + public function dropCommentFromColumn($table, string $column): string { - return sprintf(self::DROP_COMMENT_ON_COLUMN, $table, $column); + return sprintf(self::DROP_COMMENT_FROM_COLUMN, $table, $column); } } diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index cc205584..7f5d8a11 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -94,9 +94,9 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir if (in_array('comment', $changed, true)) { // TODO if ($desired->comment) { $this->migration->addUpCode($this->recordBuilder->addCommentOnColumn($tableName, $desired->name, $desired->comment)); - $this->migration->addDownCode($this->recordBuilder->dropCommentOnColumn($tableName, $desired->name)); + $this->migration->addDownCode($this->recordBuilder->dropCommentFromColumn($tableName, $desired->name)); } else { - $this->migration->addUpCode($this->recordBuilder->dropCommentOnColumn($tableName, $desired->name)); + $this->migration->addUpCode($this->recordBuilder->dropCommentFromColumn($tableName, $desired->name)); $this->migration->addDownCode($this->recordBuilder->addCommentOnColumn($tableName, $desired->name, $current->comment)); } } From 3fbf3c7bec2f5204a32181e27057b5cc3312ae53 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 1 Oct 2024 19:50:05 +0530 Subject: [PATCH 13/25] Handle quoting of values --- src/lib/ColumnToCode.php | 4 ++-- src/lib/migrations/BaseMigrationBuilder.php | 6 +++--- src/lib/migrations/MigrationRecordBuilder.php | 4 ++-- .../index.yml | 4 ++-- .../m200000_000001_create_table_fruits.php | 4 ++-- .../mysql/models/base/Fruit.php | 4 ++-- .../m200000_000001_create_table_fruits.php | 6 +++--- .../pgsql/models/base/Fruit.php | 4 ++-- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 73030ea5..a192fb41 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -177,8 +177,8 @@ private function resolve():void $this->rawParts['nullable'] = $this->column->allowNull ? 'NULL' : 'NOT NULL'; $this->fluentParts['nullable'] = $this->column->allowNull === true ? 'null()' : 'notNull()'; - $this->fluentParts['comment'] = $this->column->comment ? 'comment(\''.$this->column->comment.'\')' : $this->fluentParts['comment']; - $this->rawParts['comment'] = $this->column->comment ? 'COMMENT \''.$this->column->comment.'\'' : $this->fluentParts['comment']; + $this->fluentParts['comment'] = $this->column->comment ? 'comment('.var_export($this->column->comment, true).')' : $this->fluentParts['comment']; + $this->rawParts['comment'] = $this->column->comment ? 'COMMENT '.var_export($this->column->comment, true) : $this->rawParts['comment']; if (array_key_exists($dbType, self::INT_TYPE_MAP)) { $this->fluentParts['type'] = self::INT_TYPE_MAP[$dbType] . $fluentSize; diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 8a49de34..a9da6374 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -449,16 +449,16 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) { $allEnumValues = $columnSchema->enumValues; $allEnumValues = array_map(function ($aValue) { - return "'$aValue'"; + return $this->db->quoteValue($aValue); }, $allEnumValues); Yii::$app->db->createCommand( 'CREATE TYPE '.$tmpEnumName($columnSchema->name).' AS ENUM('.implode(', ', $allEnumValues).')' - )->execute(); // TODO quote value + )->execute(); } Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute(); if (ApiGenerator::isPostgres() && $columnSchema->comment) { - Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS '$columnSchema->comment'")->execute(); // TODO quote comment + Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS {$this->db->quoteValue($columnSchema->comment)}")->execute(); } $table = Yii::$app->db->getTableSchema($tmpTableName); diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index 32cc8885..9adfc6af 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -46,7 +46,7 @@ final class MigrationRecordBuilder public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN \"%s\" SET DATA TYPE %s')->execute();"; - public const ADD_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');"; + public const ADD_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', %s);"; public const DROP_COMMENT_FROM_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropCommentFromColumn('%s', '%s');"; @@ -352,7 +352,7 @@ public static function makeString(array $codeColumns): string public function addCommentOnColumn($table, string $column, string $comment): string { - return sprintf(self::ADD_COMMENT_ON_COLUMN, $table, $column, $comment); + return sprintf(self::ADD_COMMENT_ON_COLUMN, $table, $column, var_export($comment, true)); } public function dropCommentFromColumn($table, string $column): string diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index a034947e..d8e131d6 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -13,7 +13,7 @@ components: type: integer name: type: string - description: desc + description: desc with ' quote # colour: # type: string # maxLength: 255 @@ -21,7 +21,7 @@ components: description: type: number x-db-type: double precision - description: desc 2 + description: desc ' 2 Animal: type: object properties: diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php index 4fa328d9..c4271f02 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php @@ -9,8 +9,8 @@ public function up() { $this->createTable('{{%fruits}}', [ 'id' => $this->primaryKey(), - 'name' => $this->text()->null()->comment('desc'), - 0 => 'description double precision NULL DEFAULT NULL COMMENT \'desc 2\'', + 'name' => $this->text()->null()->comment('desc with \' quote'), + 0 => 'description double precision NULL DEFAULT NULL COMMENT \'desc \\\' 2\'', ]); } diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php index da1edf2f..177133f5 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php @@ -10,8 +10,8 @@ * This is the model class for table "fruits". * * @property int $id - * @property string $name desc - * @property double $description desc 2 + * @property string $name desc with ' quote + * @property double $description desc ' 2 * */ abstract class Fruit extends \yii\db\ActiveRecord diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php index b42b2822..21467feb 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php @@ -9,11 +9,11 @@ public function safeUp() { $this->createTable('{{%fruits}}', [ 'id' => $this->primaryKey(), - 'name' => $this->text()->null()->defaultValue(null)->comment('desc'), + 'name' => $this->text()->null()->defaultValue(null)->comment('desc with \' quote'), 0 => '"description" double precision NULL DEFAULT NULL', ]); - $this->addCommentOnColumn('{{%fruits}}', 'name', 'desc'); - $this->addCommentOnColumn('{{%fruits}}', 'description', 'desc 2'); + $this->addCommentOnColumn('{{%fruits}}', 'name', 'desc with \' quote'); + $this->addCommentOnColumn('{{%fruits}}', 'description', 'desc \' 2'); } public function safeDown() diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php index da1edf2f..177133f5 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php @@ -10,8 +10,8 @@ * This is the model class for table "fruits". * * @property int $id - * @property string $name desc - * @property double $description desc 2 + * @property string $name desc with ' quote + * @property double $description desc ' 2 * */ abstract class Fruit extends \yii\db\ActiveRecord From 6ea5f1d273ea407014306520a84e47f803fc7ad1 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 1 Oct 2024 19:55:52 +0530 Subject: [PATCH 14/25] Resolve TODOs --- src/lib/migrations/PostgresMigrationBuilder.php | 2 +- .../index.yml | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index 7f5d8a11..cc0618c0 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -91,7 +91,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir } } - if (in_array('comment', $changed, true)) { // TODO + if (in_array('comment', $changed, true)) { if ($desired->comment) { $this->migration->addUpCode($this->recordBuilder->addCommentOnColumn($tableName, $desired->name, $desired->comment)); $this->migration->addDownCode($this->recordBuilder->dropCommentFromColumn($tableName, $desired->name)); diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index d8e131d6..cfefea9b 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -14,10 +14,6 @@ components: name: type: string description: desc with ' quote -# colour: -# type: string - # maxLength: 255 - # x-db-type: varbinary # TODO description: type: number x-db-type: double precision From 7fa9cde711f453ad21a9371aaca1317c838988a4 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 15 Nov 2024 18:13:50 +0530 Subject: [PATCH 15/25] Fix failing test --- .../m200000_000000_change_table_v2_posts.php | 1 - .../m200000_000001_create_table_accounts.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) 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 d0ee3b6a..5d9f6aa4 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 @@ -14,7 +14,6 @@ public function up() $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()->comment('Category of posts')); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)->comment('The User')); - $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); } public function down() diff --git a/tests/specs/issue_fix/25_generate_inverse_relations/mysql/migrations_mysql_db/m200000_000001_create_table_accounts.php b/tests/specs/issue_fix/25_generate_inverse_relations/mysql/migrations_mysql_db/m200000_000001_create_table_accounts.php index f2bc7b90..1bc93a24 100644 --- a/tests/specs/issue_fix/25_generate_inverse_relations/mysql/migrations_mysql_db/m200000_000001_create_table_accounts.php +++ b/tests/specs/issue_fix/25_generate_inverse_relations/mysql/migrations_mysql_db/m200000_000001_create_table_accounts.php @@ -9,7 +9,7 @@ public function up() { $this->createTable('{{%accounts}}', [ 'id' => $this->primaryKey(), - 'name' => $this->string(128)->notNull(), + 'name' => $this->string(128)->notNull()->comment('account name'), 'paymentMethodName' => $this->text()->null(), 'user_id' => $this->integer()->null()->defaultValue(null), 'user2_id' => $this->integer()->null()->defaultValue(null), From af961aa83c39b285764701ba3ab7dded642fa3cc Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 15 Nov 2024 19:48:56 +0530 Subject: [PATCH 16/25] Make this feature optional - WIP - https://github.com/php-openapi/yii2-openapi/pull/61#issuecomment-2478774878 --- src/lib/CustomSpecAttr.php | 4 ++++ src/lib/generators/MigrationsGenerator.php | 2 +- src/lib/migrations/BaseMigrationBuilder.php | 6 +++++- src/lib/migrations/MysqlMigrationBuilder.php | 13 +++++++++---- .../index.yml | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib/CustomSpecAttr.php b/src/lib/CustomSpecAttr.php index e4638746..72b88dc3 100644 --- a/src/lib/CustomSpecAttr.php +++ b/src/lib/CustomSpecAttr.php @@ -56,4 +56,8 @@ class CustomSpecAttr * Custom route (controller ID/action ID) instead of auto-generated. See README for usage docs. https://github.com/cebe/yii2-openapi/issues/144 */ public const ROUTE = 'x-route'; + + + // TODO docs here + in README.md + public const DESC_IS_COMMENT = 'x-description-is-comment'; } diff --git a/src/lib/generators/MigrationsGenerator.php b/src/lib/generators/MigrationsGenerator.php index 1c0dfb51..b14fbfc0 100644 --- a/src/lib/generators/MigrationsGenerator.php +++ b/src/lib/generators/MigrationsGenerator.php @@ -142,7 +142,7 @@ protected function createBuilder(DbModel $model):BaseMigrationBuilder if ($this->db->getDriverName() === 'pgsql') { return Yii::createObject(PostgresMigrationBuilder::class, [$this->db, $model]); } - return Yii::createObject(MysqlMigrationBuilder::class, [$this->db, $model]); + return Yii::createObject(MysqlMigrationBuilder::class, [$this->db, $model, $this->config]); } /** diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 644da945..cff13460 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -9,6 +9,7 @@ use cebe\yii2openapi\generator\ApiGenerator; use cebe\yii2openapi\lib\ColumnToCode; +use cebe\yii2openapi\lib\Config; use cebe\yii2openapi\lib\items\DbModel; use cebe\yii2openapi\lib\items\ManyToManyRelation; use cebe\yii2openapi\lib\items\MigrationModel; @@ -53,6 +54,8 @@ abstract class BaseMigrationBuilder */ protected $recordBuilder; + public ?Config $config = null; + /** * MigrationBuilder constructor. * @param \yii\db\Connection $db @@ -60,12 +63,13 @@ abstract class BaseMigrationBuilder * @throws \yii\base\InvalidConfigException * @throws \yii\base\NotSupportedException */ - public function __construct(Connection $db, DbModel $model) + public function __construct(Connection $db, DbModel $model, ?Config $config = null) { $this->db = $db; $this->model = $model; $this->tableSchema = $db->getTableSchema($model->getTableAlias(), true); $this->recordBuilder = Yii::createObject(MigrationRecordBuilder::class, [$db->getSchema()]); + $this->config = $config; } /** diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index e9058a5c..b31a7619 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -9,6 +9,7 @@ use cebe\yii2openapi\generator\ApiGenerator; use cebe\yii2openapi\lib\ColumnToCode; +use cebe\yii2openapi\lib\CustomSpecAttr; use cebe\yii2openapi\lib\items\DbIndex; use yii\base\NotSupportedException; use yii\db\ColumnSchema; @@ -59,10 +60,14 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): $this->modifyDesiredInContextOfCurrent($current, $desiredFromDb); $this->modifyDesiredFromDbInContextOfDesired($desired, $desiredFromDb); - foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues' - , 'dbType', 'phpType' - , 'precision', 'scale', 'unsigned', 'comment' - ] as $attr) { + $properties = ['type', 'size', 'allowNull', 'defaultValue', 'enumValues' + , 'dbType', 'phpType' + , 'precision', 'scale', 'unsigned'#, 'comment' + ]; + if (!empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { + $properties[] = 'comment'; + } + foreach ($properties as $attr) { if ($attr === 'defaultValue') { if ($this->isDefaultValueChanged($current, $desiredFromDb)) { $changedAttributes[] = $attr; diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index cfefea9b..aad9014f 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -1,5 +1,5 @@ openapi: 3.0.3 - +x-description-is-comment: true info: title: 'Description of a property in spec must correspond to DB TABLE COLUMN COMMENT #60' version: 1.0.0 From d549831883c8f58b52f390bbf4e905825d26fdd6 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 18 Nov 2024 19:16:38 +0530 Subject: [PATCH 17/25] Implement for table/compo schema - WIP --- src/lib/AttributeResolver.php | 1 + src/lib/items/DbModel.php | 2 ++ src/lib/migrations/MysqlMigrationBuilder.php | 2 ++ 3 files changed, 5 insertions(+) diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index 92014c6e..736844cb 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -117,6 +117,7 @@ public function resolve(): DbModel //For valid primary keys for junction tables 'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [], 'isNotDb' => $this->componentSchema->isNonDb(), + 'descriptionIsComment' => $this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT}, ], ]); } diff --git a/src/lib/items/DbModel.php b/src/lib/items/DbModel.php index 1deb9fbc..08135d71 100644 --- a/src/lib/items/DbModel.php +++ b/src/lib/items/DbModel.php @@ -88,6 +88,8 @@ class DbModel extends BaseObject */ public $scenarioDefaultDescription = "Scenario {scenarioName}"; + public bool $descriptionIsComment = false; + /** * @var array Automatically generated scenarios from the model 'x-scenarios'. */ diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index b31a7619..50947555 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -66,6 +66,8 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): ]; if (!empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { $properties[] = 'comment'; + } elseif ($this->model->descriptionIsComment) { // TODO + $properties[] = 'comment'; } foreach ($properties as $attr) { if ($attr === 'defaultValue') { From 53416d2533b9d5541a82361233e5df0f185e62c6 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 19 Nov 2024 19:47:50 +0530 Subject: [PATCH 18/25] WIP --- src/lib/AttributeResolver.php | 2 +- src/lib/migrations/BaseMigrationBuilder.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index 736844cb..fd9beae1 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -117,7 +117,7 @@ public function resolve(): DbModel //For valid primary keys for junction tables 'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [], 'isNotDb' => $this->componentSchema->isNonDb(), - 'descriptionIsComment' => $this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT}, + 'descriptionIsComment' => !empty(($this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT})) ? $this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT} : false, ], ]); } diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index cff13460..c483351e 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -507,7 +507,8 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch public function newColStr(string $tableAlias, \cebe\yii2openapi\db\ColumnSchema $columnSchema): string { $ctc = new ColumnToCode(\Yii::$app->db->schema, $tableAlias, $columnSchema, false, false, true); - return ColumnToCode::undoEscapeQuotes($ctc->getCode()); +// return ColumnToCode::undoEscapeQuotes($ctc->getCode()); + return $ctc->getCode(); } public static function isEnum(\yii\db\ColumnSchema $columnSchema): bool From 3815cb3a64e65f0a9ebdfe78efb7d4b4e86a1628 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 20 Nov 2024 21:04:57 +0530 Subject: [PATCH 19/25] Implement for property level extension --- src/lib/AttributeResolver.php | 3 ++- src/lib/items/Attribute.php | 25 +++++++++++++------ src/lib/migrations/MysqlMigrationBuilder.php | 14 ++++++++--- .../index.yml | 17 ++++++++----- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index fd9beae1..a43aa8ba 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -117,7 +117,7 @@ public function resolve(): DbModel //For valid primary keys for junction tables 'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [], 'isNotDb' => $this->componentSchema->isNonDb(), - 'descriptionIsComment' => !empty(($this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT})) ? $this->componentSchema->{CustomSpecAttr::DESC_IS_COMMENT} : false, + 'descriptionIsComment' => !empty(($this->componentSchema->getSchema()->{CustomSpecAttr::DESC_IS_COMMENT})) ], ]); } @@ -226,6 +226,7 @@ protected function resolveProperty( ->setDefault($property->guessDefault()) ->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE)) ->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION)) + ->setXDescriptionIsComment($property->getAttr(CustomSpecAttr::DESC_IS_COMMENT)) ->setNullable($nullableValue) ->setIsPrimary($property->isPrimaryKey()) ->setForeignKeyColumnName($property->fkColName) diff --git a/src/lib/items/Attribute.php b/src/lib/items/Attribute.php index ffbbcba2..c079d06b 100644 --- a/src/lib/items/Attribute.php +++ b/src/lib/items/Attribute.php @@ -61,13 +61,6 @@ class Attribute extends BaseObject */ public $dbType = 'string'; - /** - * Custom db type - * string | null | false - * if `false` then this attribute is virtual - */ - public $xDbType; - /** * nullable * bool | null @@ -128,6 +121,18 @@ class Attribute extends BaseObject **/ public $isVirtual = false; + /** + * Custom db type + * string | null | false + * if `false` then this attribute is virtual + */ + public $xDbType; + + /** + * @see \cebe\yii2openapi\lib\CustomSpecAttr::DESC_IS_COMMENT + */ + public ?bool $xDescriptionIsComment = false; + public function __construct(string $propertyName, array $config = []) { $this->propertyName = $propertyName; @@ -397,4 +402,10 @@ public function handleDecimal(ColumnSchema $columnSchema): void $columnSchema->dbType = $decimalAttributes['dbType']; } } + + public function setXDescriptionIsComment($xDescriptionIsComment): Attribute + { + $this->xDescriptionIsComment = $xDescriptionIsComment; + return $this; + } } diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 50947555..a860ac6d 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -64,9 +64,17 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): , 'dbType', 'phpType' , 'precision', 'scale', 'unsigned'#, 'comment' ]; - if (!empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { - $properties[] = 'comment'; - } elseif ($this->model->descriptionIsComment) { // TODO + $comment = false; + if ($this->model->attributes[$desired->name]->xDescriptionIsComment) { + $comment = true; + } + if ($this->model->descriptionIsComment) { + $comment = true; + } + if ($this->config && !empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { + $comment = true; + } + if ($comment) { $properties[] = 'comment'; } foreach ($properties as $attr) { diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index aad9014f..d9272c32 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -1,5 +1,5 @@ openapi: 3.0.3 -x-description-is-comment: true +#x-description-is-comment: true info: title: 'Description of a property in spec must correspond to DB TABLE COLUMN COMMENT #60' version: 1.0.0 @@ -8,43 +8,48 @@ components: schemas: Fruit: type: object + # x-description-is-comment: true properties: id: type: integer name: type: string description: desc with ' quote + x-description-is-comment: true description: type: number x-db-type: double precision description: desc ' 2 + x-description-is-comment: true Animal: type: object + # x-description-is-comment: true properties: id: type: integer name: type: integer - # description: desc - # description: - # type: string - # x-db-type: varchar - # description: desc 2 + x-description-is-comment: true g: type: string description: desc for g + x-description-is-comment: true g2: type: string description: changed comment on g2 col + x-description-is-comment: true g3: type: string description: the comment on g3 col remains same + x-description-is-comment: true g4: type: integer description: data type changes but comment remains same + x-description-is-comment: true new_col: type: string description: new col added + x-description-is-comment: true paths: '/': From 8fd5b8b14e69ff338f117e94231a264a72419563 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 21 Nov 2024 12:24:12 +0530 Subject: [PATCH 20/25] Add more tests --- .../index.yml | 10 -- tests/unit/IssueFixTest.php | 132 ++++++++++++++++++ 2 files changed, 132 insertions(+), 10 deletions(-) diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index d9272c32..1b874edc 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -8,48 +8,38 @@ components: schemas: Fruit: type: object - # x-description-is-comment: true properties: id: type: integer name: type: string description: desc with ' quote - x-description-is-comment: true description: type: number x-db-type: double precision description: desc ' 2 - x-description-is-comment: true Animal: type: object - # x-description-is-comment: true properties: id: type: integer name: type: integer - x-description-is-comment: true g: type: string description: desc for g - x-description-is-comment: true g2: type: string description: changed comment on g2 col - x-description-is-comment: true g3: type: string description: the comment on g3 col remains same - x-description-is-comment: true g4: type: integer description: data type changes but comment remains same - x-description-is-comment: true new_col: type: string description: new col added - x-description-is-comment: true paths: '/': diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index e7fdf5f1..86a41d4c 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -580,6 +580,138 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC $this->deleteTableFor60DescriptionOfAProperty(); } + // https://github.com/php-openapi/yii2-openapi/issues/60 + public function test60ComponentSchemaLevelExtension() + { + + + $schema = <<alterColumn('{{%fruits}}', 'name', $this->text()->notNull()->comment('Hi there')); + } + + public function down() + { + $this->alterColumn('{{%fruits}}', 'name', $this->text()->null()); + } +} + +PHP; + + $this->for60($schema, $expected); + } + + // https://github.com/php-openapi/yii2-openapi/issues/60 + public function test60PropertyLevelExtension() + { + $schema = <<alterColumn('{{%fruits}}', 'name', $this->text()->notNull()->comment('Hi there')); + } + + public function down() + { + $this->alterColumn('{{%fruits}}', 'name', $this->text()->null()); + } +} + +PHP; + + $this->for60($schema, $expected); + } + + private function for60($spec, $expected) + { + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute(); + Yii::$app->db->createCommand()->createTable('{{%fruits}}', [ + 'id' => 'pk', + 'name' => 'text', + ])->execute(); + $config = [ + 'openApiPath' => 'data://text/plain;base64,' . base64_encode($spec), + 'generateUrls' => false, + 'generateModels' => false, + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => false, + ]; + $tmpConfigFile = Yii::getAlias("@runtime") . "/tmp-config.php"; + file_put_contents($tmpConfigFile, 'runGenerator($tmpConfigFile); + $actual = file_get_contents(Yii::getAlias('@app') . '/migrations_mysql_db/m200000_000000_change_table_fruits.php'); + $this->assertSame($expected, $actual); + $this->runActualMigrations('mysql', 1); + Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute(); + } + private function createTableFor60DescriptionOfAProperty() { Yii::$app->db->createCommand()->createTable('{{%animals}}', [ From 051900a800306660c39b9cecfd42e47aa034e4c8 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 21 Nov 2024 12:39:08 +0530 Subject: [PATCH 21/25] Fix failing tests --- src/lib/migrations/MysqlMigrationBuilder.php | 2 +- .../m200000_000000_change_table_v2_posts.php | 4 ++-- .../m200000_000005_change_table_v2_comments.php | 2 -- .../m200000_000000_change_table_v2_posts.php | 4 ++-- .../m200000_000005_change_table_v2_comments.php | 2 -- .../index.yml | 2 +- 6 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index a860ac6d..1cc1dadc 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -65,7 +65,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): , 'precision', 'scale', 'unsigned'#, 'comment' ]; $comment = false; - if ($this->model->attributes[$desired->name]->xDescriptionIsComment) { + if (isset($this->model->attributes[$desired->name]) && $this->model->attributes[$desired->name]->xDescriptionIsComment) { $comment = true; } if ($this->model->descriptionIsComment) { 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 5d9f6aa4..5f50d0d7 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000000_change_table_v2_posts.php @@ -11,9 +11,9 @@ public function up() $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\' AFTER slug'); $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); $this->dropColumn('{{%v2_posts}}', 'uid'); - $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()->comment('Category of posts')); + $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); - $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)->comment('The User')); + $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)); } public function down() diff --git a/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php b/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php index 4ded7be3..b34a7810 100644 --- a/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php +++ b/tests/specs/blog_v2/migrations_maria_db/m200000_000005_change_table_v2_comments.php @@ -11,7 +11,6 @@ public function up() $this->dropForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}'); $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); - $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger(20)->notNull()->comment('A blog post (uid used as pk for test purposes)')); $this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null()->defaultValue('')); $this->alterColumn('{{%v2_comments}}', 'created_at', $this->timestamp()->notNull()); @@ -26,7 +25,6 @@ public function down() $this->alterColumn('{{%v2_comments}}', 'created_at', $this->integer(11)->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'json NOT NULL DEFAULT \'[]\''); $this->alterColumn('{{%v2_comments}}', 'message', 'json NOT NULL DEFAULT \'[]\''); - $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger(20)->notNull()); $this->addColumn('{{%v2_comments}}', 'author_id', $this->integer(11)->notNull()->after('post_id')); $this->dropColumn('{{%v2_comments}}', 'user_id'); $this->addForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}', 'author_id', 'itt_v2_users', 'id'); 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 83ab62a1..eea03cca 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000000_change_table_v2_posts.php @@ -11,9 +11,9 @@ public function up() $this->addColumn('{{%v2_posts}}', 'lang', 'enum("ru", "eng") NULL DEFAULT \'ru\' AFTER slug'); $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); $this->dropColumn('{{%v2_posts}}', 'uid'); - $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()->comment('Category of posts')); + $this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull()); $this->alterColumn('{{%v2_posts}}', 'active', $this->tinyInteger(1)->notNull()); - $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)->comment('The User')); + $this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null()->defaultValue(null)); } public function down() diff --git a/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php b/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php index 3fa92fa8..8ac91b71 100644 --- a/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php +++ b/tests/specs/blog_v2/migrations_mysql_db/m200000_000005_change_table_v2_comments.php @@ -11,7 +11,6 @@ public function up() $this->dropForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}'); $this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null)->after('post_id')->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); - $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger()->notNull()->comment('A blog post (uid used as pk for test purposes)')); $this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null()->defaultValue('')); $this->alterColumn('{{%v2_comments}}', 'created_at', $this->timestamp()->notNull()); @@ -26,7 +25,6 @@ public function down() $this->alterColumn('{{%v2_comments}}', 'created_at', $this->integer()->notNull()); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'json NOT NULL'); $this->alterColumn('{{%v2_comments}}', 'message', 'json NOT NULL'); - $this->alterColumn('{{%v2_comments}}', 'post_id', $this->bigInteger()->notNull()); $this->addColumn('{{%v2_comments}}', 'author_id', $this->integer()->notNull()->after('post_id')); $this->dropColumn('{{%v2_comments}}', 'user_id'); $this->addForeignKey('fk_v2_comments_author_id_v2_users_id', '{{%v2_comments}}', 'author_id', 'itt_v2_users', 'id'); diff --git a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml index 1b874edc..bafe0cf8 100644 --- a/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml +++ b/tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml @@ -1,5 +1,5 @@ openapi: 3.0.3 -#x-description-is-comment: true +x-description-is-comment: true info: title: 'Description of a property in spec must correspond to DB TABLE COLUMN COMMENT #60' version: 1.0.0 From 9d4dc38ae6c644007a7f1dbad84a928f64a3fec0 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 21 Nov 2024 17:36:46 +0530 Subject: [PATCH 22/25] Add doc --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/README.md b/README.md index aae945c5..f68df469 100644 --- a/README.md +++ b/README.md @@ -465,6 +465,60 @@ Generated URL rules config for above is (in `urls.rest.php` or pertinent file): ``` `x-route` does not support [Yii Modules](https://www.yiiframework.com/doc/guide/2.0/en/structure-modules). +### `x-description-is-comment` + + boolean; default: false + +When a new database table is created from new OpenAPI component schema, description of a property will be used as +comment of column (of database table). + +This extension is used when a description is edited for existing property, and you want to generate migration for its +corresponding column comment changes. + +This extension can be used at 3 place: + +**1. root level (highest priority)** + +```yaml +openapi: 3.0.3 +x-description-is-comment: true +info: + title: Description +``` + +This will create migration of any changed description of component schema property present throughout the spec. + +**2. component schema level** + +```yaml +components: + schemas: + Fruit: + type: object + x-description-is-comment: true +``` + +This will create migration of changed description of only properties of component schema which have this extension. + +**3. property level (lowest priority)** + +```yaml +components: + schemas: + Fruit: + type: object + properties: + id: + type: integer + name: + type: string + nullable: false + x-description-is-comment: true + description: Hi there +``` + +Migrations will be only generated for changed description of properties having this extension. + ## Many-to-Many relation definition There are two ways for define many-to-many relations: From 20fad45320bd0a4f0ea7a4aa2bb4964325997cff Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 21 Nov 2024 17:40:05 +0530 Subject: [PATCH 23/25] Resolve TODO --- src/lib/CustomSpecAttr.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/CustomSpecAttr.php b/src/lib/CustomSpecAttr.php index 72b88dc3..6234f7a9 100644 --- a/src/lib/CustomSpecAttr.php +++ b/src/lib/CustomSpecAttr.php @@ -58,6 +58,8 @@ class CustomSpecAttr public const ROUTE = 'x-route'; - // TODO docs here + in README.md + /** + * Generate migrations for changed description of property. More docs is present in README.md file + */ public const DESC_IS_COMMENT = 'x-description-is-comment'; } From 692c4aa1e3d40210153e6aa1256910cf928822f5 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 21 Nov 2024 18:34:31 +0530 Subject: [PATCH 24/25] Implement for PgSQL --- src/lib/generators/MigrationsGenerator.php | 3 +-- src/lib/migrations/BaseMigrationBuilder.php | 19 ++++++++++++++++++- src/lib/migrations/MysqlMigrationBuilder.php | 15 ++------------- .../migrations/PostgresMigrationBuilder.php | 11 ++++++++--- .../m200000_000000_change_table_v2_posts.php | 4 ---- ...200000_000005_change_table_v2_comments.php | 2 -- 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/lib/generators/MigrationsGenerator.php b/src/lib/generators/MigrationsGenerator.php index b14fbfc0..95426c13 100644 --- a/src/lib/generators/MigrationsGenerator.php +++ b/src/lib/generators/MigrationsGenerator.php @@ -12,7 +12,6 @@ use cebe\yii2openapi\lib\items\DbModel; use cebe\yii2openapi\lib\items\MigrationModel; use cebe\yii2openapi\lib\migrations\BaseMigrationBuilder; -use cebe\yii2openapi\lib\migrations\MigrationRecordBuilder; use cebe\yii2openapi\lib\migrations\MysqlMigrationBuilder; use cebe\yii2openapi\lib\migrations\PostgresMigrationBuilder; use Exception; @@ -140,7 +139,7 @@ public function buildMigrations():array protected function createBuilder(DbModel $model):BaseMigrationBuilder { if ($this->db->getDriverName() === 'pgsql') { - return Yii::createObject(PostgresMigrationBuilder::class, [$this->db, $model]); + return Yii::createObject(PostgresMigrationBuilder::class, [$this->db, $model, $this->config]); } return Yii::createObject(MysqlMigrationBuilder::class, [$this->db, $model, $this->config]); } diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index c483351e..2cf1702a 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -10,6 +10,7 @@ use cebe\yii2openapi\generator\ApiGenerator; use cebe\yii2openapi\lib\ColumnToCode; use cebe\yii2openapi\lib\Config; +use cebe\yii2openapi\lib\CustomSpecAttr; use cebe\yii2openapi\lib\items\DbModel; use cebe\yii2openapi\lib\items\ManyToManyRelation; use cebe\yii2openapi\lib\items\MigrationModel; @@ -54,7 +55,7 @@ abstract class BaseMigrationBuilder */ protected $recordBuilder; - public ?Config $config = null; + protected ?Config $config = null; /** * MigrationBuilder constructor. @@ -597,4 +598,20 @@ public function buildTablesDrop(): void abstract public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $forAlter = false): ?string; abstract public function setColumnsPositions(); + + protected function shouldCompareComment(ColumnSchema $desired): bool + { + $comment = false; + if (isset($this->model->attributes[$desired->name]) && $this->model->attributes[$desired->name]->xDescriptionIsComment) { + $comment = true; + } + if ($this->model->descriptionIsComment) { + $comment = true; + } + if ($this->config !== null && + !empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { + $comment = true; + } + return $comment; + } } diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 1cc1dadc..da3d967d 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -9,7 +9,6 @@ use cebe\yii2openapi\generator\ApiGenerator; use cebe\yii2openapi\lib\ColumnToCode; -use cebe\yii2openapi\lib\CustomSpecAttr; use cebe\yii2openapi\lib\items\DbIndex; use yii\base\NotSupportedException; use yii\db\ColumnSchema; @@ -62,19 +61,9 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): $properties = ['type', 'size', 'allowNull', 'defaultValue', 'enumValues' , 'dbType', 'phpType' - , 'precision', 'scale', 'unsigned'#, 'comment' + , 'precision', 'scale', 'unsigned' ]; - $comment = false; - if (isset($this->model->attributes[$desired->name]) && $this->model->attributes[$desired->name]->xDescriptionIsComment) { - $comment = true; - } - if ($this->model->descriptionIsComment) { - $comment = true; - } - if ($this->config && !empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) { - $comment = true; - } - if ($comment) { + if ($this->shouldCompareComment($desired)) { $properties[] = 'comment'; } foreach ($properties as $attr) { diff --git a/src/lib/migrations/PostgresMigrationBuilder.php b/src/lib/migrations/PostgresMigrationBuilder.php index 0be4298b..4f2379cf 100644 --- a/src/lib/migrations/PostgresMigrationBuilder.php +++ b/src/lib/migrations/PostgresMigrationBuilder.php @@ -141,10 +141,15 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired): $this->modifyDesiredInContextOfCurrent($current, $desiredFromDb); $this->modifyDesiredFromDbInContextOfDesired($desired, $desiredFromDb); - foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues' + $properties = ['type', 'size', 'allowNull', 'defaultValue', 'enumValues' , 'dbType', 'phpType' - , 'precision', 'scale', 'unsigned', 'comment' - ] as $attr) { + , 'precision', 'scale', 'unsigned' + ]; + if ($this->shouldCompareComment($desired)) { + $properties[] = 'comment'; + } + + foreach ($properties as $attr) { if ($attr === 'defaultValue') { if ($this->isDefaultValueChanged($current, $desiredFromDb)) { $changedAttributes[] = $attr; 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 a9e52fbc..9444aef9 100644 --- a/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php +++ b/tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php @@ -13,17 +13,13 @@ public function safeUp() $this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}'); $this->dropColumn('{{%v2_posts}}', 'uid'); $this->alterColumn('{{%v2_posts}}', 'category_id', 'int8 NOT NULL USING "category_id"::int8'); - $this->addCommentOnColumn('{{%v2_posts}}', 'category_id', 'Category of posts'); $this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT"); $this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int8 NULL USING "created_by_id"::int8'); - $this->addCommentOnColumn('{{%v2_posts}}', 'created_by_id', 'The User'); } public function safeDown() { - $this->dropCommentFromColumn('{{%v2_posts}}', 'created_by_id'); $this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int4 NULL USING "created_by_id"::int4'); - $this->dropCommentFromColumn('{{%v2_posts}}', 'category_id'); $this->alterColumn('{{%v2_posts}}', 'category_id', 'int4 NOT NULL USING "category_id"::int4'); $this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull()); $this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true); 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 8a12066d..e9ad9ff5 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,7 +11,6 @@ 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)->comment('The User')); $this->dropColumn('{{%v2_comments}}', 'author_id'); - $this->addCommentOnColumn('{{%v2_comments}}', 'post_id', 'A blog post (uid used as pk for test purposes)'); $this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text'); $this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT"); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'varchar(300) NULL USING "meta_data"::varchar'); @@ -29,7 +28,6 @@ public function safeDown() $this->alterColumn('{{%v2_comments}}', 'created_at', 'int4 NOT NULL USING "created_at"::int4'); $this->alterColumn('{{%v2_comments}}', 'meta_data', 'jsonb NOT NULL USING "meta_data"::jsonb'); $this->alterColumn('{{%v2_comments}}', 'message', 'jsonb NOT NULL USING "message"::jsonb'); - $this->dropCommentFromColumn('{{%v2_comments}}', 'post_id'); $this->addColumn('{{%v2_comments}}', 'author_id', $this->integer()->notNull()); $this->dropColumn('{{%v2_comments}}', 'user_id'); $this->alterColumn('{{%v2_comments}}', 'message', "SET DEFAULT '[]'"); From d5be8ce78162f5c65847b269567f7e498b16498f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 21 Nov 2024 18:46:17 +0530 Subject: [PATCH 25/25] Refactor --- src/lib/generators/MigrationsGenerator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/generators/MigrationsGenerator.php b/src/lib/generators/MigrationsGenerator.php index 95426c13..211e4639 100644 --- a/src/lib/generators/MigrationsGenerator.php +++ b/src/lib/generators/MigrationsGenerator.php @@ -138,10 +138,11 @@ public function buildMigrations():array */ protected function createBuilder(DbModel $model):BaseMigrationBuilder { + $params = [$this->db, $model, $this->config]; if ($this->db->getDriverName() === 'pgsql') { - return Yii::createObject(PostgresMigrationBuilder::class, [$this->db, $model, $this->config]); + return Yii::createObject(PostgresMigrationBuilder::class, $params); } - return Yii::createObject(MysqlMigrationBuilder::class, [$this->db, $model, $this->config]); + return Yii::createObject(MysqlMigrationBuilder::class, $params); } /**