Skip to content

Commit d4dd074

Browse files
committed
Add comments handling for PgSQL as it is different from MySQL - WIP
1 parent 4a11785 commit d4dd074

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch
453453
}
454454

455455
Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute();
456+
if (ApiGenerator::isPostgres()) {
457+
Yii::$app->db->createCommand("comment on column $tmpTableName.$columnSchema->name is '$columnSchema->comment'")->execute();
458+
}
456459

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

src/lib/migrations/MigrationRecordBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ 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');";
50+
4951
/**
5052
* @var \yii\db\Schema
5153
*/
@@ -345,4 +347,9 @@ public static function makeString(array $codeColumns): string
345347
$codeColumns = '['.PHP_EOL.self::INDENT.' '.$codeColumns.PHP_EOL . self::INDENT.']';
346348
return $codeColumns;
347349
}
350+
351+
public function pgsqlCommentOnColumn($table, string $column, string $comment)
352+
{
353+
return sprintf(self::PGSQL_COMMENT_ON_COLUMN, $table, $column, $comment);
354+
}
348355
}

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use cebe\yii2openapi\lib\items\DbIndex;
1111
use yii\db\ColumnSchema;
12-
use yii\helpers\VarDumper;
1312
use yii\helpers\ArrayHelper;
1413

1514
final class PostgresMigrationBuilder extends BaseMigrationBuilder
@@ -65,7 +64,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
6564

6665
if (!empty(array_intersect(['type', 'size'
6766
, 'dbType', 'phpType'
68-
, 'precision', 'scale', 'unsigned'
67+
, 'precision', 'scale', 'unsigned', 'comment'
6968
], $changed))) {
7069
$addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType);
7170
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired, $addUsing));
@@ -91,6 +90,15 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
9190
$this->migration->addUpCode($upCode)->addDownCode($downCode, true);
9291
}
9392
}
93+
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+
// }
100+
}
101+
94102
if ($isChangeToEnum) {
95103
$this->migration->addUpCode($this->recordBuilder->createEnum($tableName, $desired->name, $desired->enumValues), true);
96104
}
@@ -127,7 +135,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
127135

128136
foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues'
129137
, 'dbType', 'phpType'
130-
, 'precision', 'scale', 'unsigned'
138+
, 'precision', 'scale', 'unsigned', 'comment'
131139
] as $attr) {
132140
if ($attr === 'defaultValue') {
133141
if ($this->isDefaultValueChanged($current, $desiredFromDb)) {

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
@@ -14,8 +14,10 @@ components:
1414
name:
1515
type: string
1616
description: desc
17-
# colour:
18-
# type: string
17+
colour:
18+
type: string
19+
# maxLength: 255
20+
# x-db-type: varbinary # TODO
1921
description:
2022
type: string
2123
x-db-type: varchar
@@ -28,6 +30,13 @@ components:
2830
name:
2931
type: integer
3032
description: desc
33+
# description:
34+
# type: string
35+
# x-db-type: varchar
36+
# description: desc 2
37+
g:
38+
type: string
39+
description: desc 3
3140

3241
paths:
3342
'/':

tests/unit/IssueFixTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,29 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC
377377
// $this->checkFiles($actualFiles, $expectedFiles);
378378

379379
$this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
380+
381+
382+
$this->changeDbToPgsql();
383+
$this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
384+
$this->createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
385+
$this->runGenerator($testFile, 'pgsql');
386+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
387+
// 'recursive' => true,
388+
// ]);
389+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql"), [
390+
// 'recursive' => true,
391+
// ]);
392+
// $this->checkFiles($actualFiles, $expectedFiles);
393+
394+
$this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
380395
}
381396

382397
private function createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment()
383398
{
384399
Yii::$app->db->createCommand()->createTable('{{%animals}}', [
385400
'id' => 'pk',
386-
'name' => 'text comment "the name"',
401+
'name' => 'text ', # comment "the name"
402+
'g' => 'text'
387403
])->execute();
388404
}
389405

0 commit comments

Comments
 (0)