Skip to content

Commit 72a7a6a

Browse files
committed
Add comments handling for PgSQL as it is different from MySQL - done
1 parent b18c521 commit 72a7a6a

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function buildFresh():MigrationModel
161161
}
162162
}
163163

164-
$this->addCommentsMigration();
164+
$this->handleCommentsMigration();
165165

166166
return $this->migration;
167167
}
@@ -277,7 +277,7 @@ abstract public static function getColumnSchemaBuilderClass(): string;
277277
*/
278278
abstract protected function findTableIndexes():array;
279279

280-
abstract public function addCommentsMigration();
280+
abstract public function handleCommentsMigration();
281281

282282
protected function buildIndexChanges():void
283283
{
@@ -457,8 +457,8 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch
457457
}
458458

459459
Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute();
460-
if (ApiGenerator::isPostgres()) {
461-
Yii::$app->db->createCommand("comment on column $tmpTableName.$columnSchema->name is '$columnSchema->comment'")->execute();
460+
if (ApiGenerator::isPostgres() && $columnSchema->comment) {
461+
Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS '$columnSchema->comment'")->execute();
462462
}
463463

464464
$table = Yii::$app->db->getTableSchema($tmpTableName);

src/lib/migrations/MigrationRecordBuilder.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ final class MigrationRecordBuilder
4646

4747
public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN \"%s\" SET DATA TYPE %s')->execute();";
4848

49-
public const PGSQL_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');";
49+
public const ADD_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');";
50+
51+
public const DROP_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropCommentFromColumn('%s', '%s');";
5052

5153
/**
5254
* @var \yii\db\Schema
@@ -348,8 +350,13 @@ public static function makeString(array $codeColumns): string
348350
return $codeColumns;
349351
}
350352

351-
public function pgsqlCommentOnColumn($table, string $column, string $comment)
353+
public function addCommentOnColumn($table, string $column, string $comment)
354+
{
355+
return sprintf(self::ADD_COMMENT_ON_COLUMN, $table, $column, $comment);
356+
}
357+
358+
public function dropCommentOnColumn($table, string $column)
352359
{
353-
return sprintf(self::PGSQL_COMMENT_ON_COLUMN, $table, $column, $comment);
360+
return sprintf(self::DROP_COMMENT_ON_COLUMN, $table, $column);
354361
}
355362
}

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use yii\db\ColumnSchema;
1515
use yii\db\IndexConstraint;
1616
use yii\db\Schema;
17-
use \Yii;
1817
use yii\helpers\ArrayHelper;
19-
use yii\helpers\VarDumper;
2018

2119
final class MysqlMigrationBuilder extends BaseMigrationBuilder
2220
{
@@ -56,7 +54,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
5654

5755
foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues'
5856
, 'dbType', 'phpType'
59-
, 'precision', 'scale', 'unsigned'
57+
, 'precision', 'scale', 'unsigned', 'comment'
6058
] as $attr) {
6159
if ($attr === 'defaultValue') {
6260
if ($this->isDefaultValueChanged($current, $desiredFromDb)) {
@@ -160,7 +158,7 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
160158
}
161159
}
162160

163-
public function addCommentsMigration()
161+
public function handleCommentsMigration()
164162
{
165163
// nothing to do here as comments can be defined in same statement as of alter/add column in MySQL
166164
// this method is only for PgSQL

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
6464

6565
if (!empty(array_intersect(['type', 'size'
6666
, 'dbType', 'phpType'
67-
, 'precision', 'scale', 'unsigned', 'comment'
67+
, 'precision', 'scale', 'unsigned'
6868
], $changed))) {
6969
$addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType);
7070
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired, $addUsing));
@@ -91,12 +91,14 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
9191
}
9292
}
9393

94-
if (in_array('comment', $changed, true)) {
95-
// if ($desired->comment) {
96-
$this->migration->addUpCode($this->recordBuilder->pgsqlCommentOnColumn($tableName, $desired->name, $desired->comment));
97-
// } else {
98-
//
99-
// }
94+
if (in_array('comment', $changed, true)) { // TODO
95+
if ($desired->comment) {
96+
$this->migration->addUpCode($this->recordBuilder->addCommentOnColumn($tableName, $desired->name, $desired->comment));
97+
$this->migration->addDownCode($this->recordBuilder->dropCommentOnColumn($tableName, $desired->name));
98+
} else {
99+
$this->migration->addUpCode($this->recordBuilder->dropCommentOnColumn($tableName, $desired->name));
100+
$this->migration->addDownCode($this->recordBuilder->addCommentOnColumn($tableName, $desired->name, $current->comment));
101+
}
100102
}
101103

102104
if ($isChangeToEnum) {
@@ -257,13 +259,16 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
257259
}
258260
}
259261

260-
public function addCommentsMigration()
262+
public function handleCommentsMigration()
261263
{
262264
$tableAlias = $this->model->getTableAlias();
263265
foreach ($this->newColumns as $column) {
264-
if($column->comment) {
266+
if ($column->comment) {
267+
$this->migration
268+
->addUpCode($this->recordBuilder->addCommentOnColumn($tableAlias, $column->name, $column->comment));
269+
} else {
265270
$this->migration
266-
->addUpCode($this->recordBuilder->pgsqlCommentOnColumn($tableAlias, $column->name, $column->comment))
271+
->addUpCode($this->recordBuilder->dropCommentOnColumn($tableAlias, $column->name))
267272
;
268273
}
269274
}

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,23 @@ components:
2929
type: integer
3030
name:
3131
type: integer
32-
description: desc
32+
# description: desc
3333
# description:
3434
# type: string
3535
# x-db-type: varchar
3636
# description: desc 2
3737
g:
3838
type: string
39-
description: desc 3
39+
description: desc for g
40+
g2:
41+
type: string
42+
description: changed comment on g2 col
43+
g3:
44+
type: string
45+
description: the comment on g3 col remains same
46+
g4:
47+
type: integer
48+
description: data type changes but comment remains same
4049

4150
paths:
4251
'/':

tests/unit/IssueFixTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC
369369
$this->createTableFor60DescriptionOfAProperty();
370370
$testFile = Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php");
371371
$this->runGenerator($testFile);
372+
$this->runActualMigrations();
372373
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
373374
// 'recursive' => true,
374375
// ]);
@@ -384,23 +385,32 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC
384385
$this->deleteTableFor60DescriptionOfAProperty();
385386
$this->createTableFor60DescriptionOfAProperty();
386387
$this->runGenerator($testFile, 'pgsql');
388+
$this->runActualMigrations('pgsql');
387389
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
388390
// 'recursive' => true,
389391
// ]);
390392
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql"), [
391393
// 'recursive' => true,
392394
// ]);
393395
// $this->checkFiles($actualFiles, $expectedFiles);
394-
$this->deleteTableFor60DescriptionOfAProperty();
396+
// $this->deleteTableFor60DescriptionOfAProperty();
395397
}
396398

397399
private function createTableFor60DescriptionOfAProperty()
398400
{
399401
Yii::$app->db->createCommand()->createTable('{{%animals}}', [
400402
'id' => 'pk',
401403
'name' => 'text ', # comment "the name"
402-
'g' => 'text'
404+
'g' => 'text',
405+
'g2' => 'text',
406+
'g3' => 'text',
407+
'g4' => 'text',
403408
])->execute();
409+
410+
Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'name', 'the comment on name col')->execute();
411+
Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'g2', 'the comment on g2 col')->execute();
412+
Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'g3', 'the comment on g3 col remains same')->execute();
413+
Yii::$app->db->createCommand()->addCommentOnColumn('{{%animals}}', 'g4', 'data type changes but comment remains same')->execute();
404414
}
405415

406416
private function deleteTableFor60DescriptionOfAProperty()

0 commit comments

Comments
 (0)