Skip to content

Fix #62: Unnecessary SQL statement for comment on column in PgSQL #104

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/lib/migrations/MigrationRecordBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public function __construct(Schema $dbSchema)

/**
* @param string $tableAlias
* @param array|\yii\db\ColumnSchema $columns
* @param array|ColumnSchema $columns
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function createTable(string $tableAlias, array $columns):string
{
$codeColumns = [];
foreach ($columns as $columnName => $cebeDbColumnSchema) {
if (!empty($cebeDbColumnSchema->xDbType) && is_string($cebeDbColumnSchema->xDbType)) {
if (static::isXDbTypePresent($cebeDbColumnSchema)) {
$name = static::quote($columnName);
$codeColumns[] = $name.' '.$this->columnToCode($tableAlias, $cebeDbColumnSchema, false)->getCode();
} else {
Expand Down Expand Up @@ -393,4 +393,9 @@ public function renameColumn(string $table, string $fromColumn, string $toColumn
{
return sprintf(self::RENAME_COLUMN, $table, $fromColumn, $toColumn);
}

public static function isXDbTypePresent(ColumnSchema $columnSchema): bool
{
return (!empty($columnSchema->xDbType) && is_string($columnSchema->xDbType));
}
}
7 changes: 5 additions & 2 deletions src/lib/migrations/PostgresMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,12 @@ public function handleCommentsMigration()
{
$tableAlias = $this->model->getTableAlias();
foreach ($this->newColumns as $column) {
/** @var ColumnSchema $column */
if ($column->comment) {
$this->migration
->addUpCode($this->recordBuilder->addCommentOnColumn($tableAlias, $column->name, $column->comment));
if (MigrationRecordBuilder::isXDbTypePresent($column)) {
$this->migration
->addUpCode($this->recordBuilder->addCommentOnColumn($tableAlias, $column->name, $column->comment));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function safeUp()
'name' => $this->text()->null()->defaultValue(null)->comment('desc with \' quote'),
0 => '"description" double precision NULL DEFAULT NULL',
]);
$this->addCommentOnColumn('{{%fruits}}', 'name', 'desc with \' quote');
$this->addCommentOnColumn('{{%fruits}}', 'description', 'desc \' 2');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/62_unnecessary_sql_statement_for_comment_on_column_in_pgsql/index.yml',
'generateUrls' => false,
'generateModels' => false,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => true,
'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
];

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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

components:
schemas:
Fruit:
type: object
properties:
id:
type: integer
name:
type: string
description: desc with ' quote
description:
type: number
x-db-type: double precision
description: desc ' 2

paths:
'/':
get:
responses:
'200':
description: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Table for Fruit
*/
class m200000_000000_create_table_fruits extends \yii\db\Migration
{
public function safeUp()
{
$this->createTable('{{%fruits}}', [
'id' => $this->primaryKey(),
'name' => $this->text()->null()->defaultValue(null)->comment('desc with \' quote'),
0 => '"description" double precision NULL DEFAULT NULL',
]);
$this->addCommentOnColumn('{{%fruits}}', 'description', 'desc \' 2');
}

public function safeDown()
{
$this->dropTable('{{%fruits}}');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function safeUp()
'id' => $this->primaryKey(),
'name' => $this->string(40)->notNull()->comment('account name'),
]);
$this->addCommentOnColumn('{{%accounts}}', 'name', 'account name');
}

public function safeDown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ 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()
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/issues/Issue62Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace tests\unit\issues;

use tests\DbTestCase;
use Yii;
use yii\helpers\FileHelper;

# https://github.com/php-openapi/yii2-openapi/issues/62
class Issue62Test extends DbTestCase
{
public function testIndex()
{
$this->changeDbToPgsql();

$testFile = Yii::getAlias("@specs/issue_fix/62_unnecessary_sql_statement_for_comment_on_column_in_pgsql/index.php");
$this->runGenerator($testFile, 'pgsql');
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
'recursive' => true,
]);
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/62_unnecessary_sql_statement_for_comment_on_column_in_pgsql/pgsql"), [
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
}
}