Skip to content

Commit b18c521

Browse files
committed
Add comments handling for PgSQL as it is different from MySQL - 2
1 parent d4dd074 commit b18c521

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

src/lib/migrations/BaseMigrationBuilder.php

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

164+
$this->addCommentsMigration();
165+
164166
return $this->migration;
165167
}
166168

@@ -275,6 +277,8 @@ abstract public static function getColumnSchemaBuilderClass(): string;
275277
*/
276278
abstract protected function findTableIndexes():array;
277279

280+
abstract public function addCommentsMigration();
281+
278282
protected function buildIndexChanges():void
279283
{
280284
$haveIndexes = $this->findTableIndexes();

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,10 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
159159
$desired->size = $current->size;
160160
}
161161
}
162+
163+
public function addCommentsMigration()
164+
{
165+
// nothing to do here as comments can be defined in same statement as of alter/add column in MySQL
166+
// this method is only for PgSQL
167+
}
162168
}

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,16 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
256256
$desired->size = $current->size;
257257
}
258258
}
259+
260+
public function addCommentsMigration()
261+
{
262+
$tableAlias = $this->model->getTableAlias();
263+
foreach ($this->newColumns as $column) {
264+
if($column->comment) {
265+
$this->migration
266+
->addUpCode($this->recordBuilder->pgsqlCommentOnColumn($tableAlias, $column->name, $column->comment))
267+
;
268+
}
269+
}
270+
}
259271
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ components:
1414
name:
1515
type: string
1616
description: desc
17-
colour:
18-
type: string
17+
# colour:
18+
# type: string
1919
# maxLength: 255
2020
# x-db-type: varbinary # TODO
2121
description:
22-
type: string
23-
x-db-type: varchar
22+
type: number
23+
x-db-type: double precision
2424
description: desc 2
2525
Animal:
2626
type: object

tests/unit/IssueFixTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,9 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim()
364364
// https://github.com/php-openapi/yii2-openapi/issues/60
365365
public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment()
366366
{
367-
$this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
368-
$this->createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
367+
// MySQL
368+
$this->deleteTableFor60DescriptionOfAProperty();
369+
$this->createTableFor60DescriptionOfAProperty();
369370
$testFile = Yii::getAlias("@specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.php");
370371
$this->runGenerator($testFile);
371372
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
@@ -375,13 +376,13 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC
375376
// 'recursive' => true,
376377
// ]);
377378
// $this->checkFiles($actualFiles, $expectedFiles);
378-
379-
$this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
379+
$this->deleteTableFor60DescriptionOfAProperty();
380380

381381

382+
// PgSQL
382383
$this->changeDbToPgsql();
383-
$this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
384-
$this->createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
384+
$this->deleteTableFor60DescriptionOfAProperty();
385+
$this->createTableFor60DescriptionOfAProperty();
385386
$this->runGenerator($testFile, 'pgsql');
386387
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
387388
// 'recursive' => true,
@@ -390,11 +391,10 @@ public function test60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnC
390391
// 'recursive' => true,
391392
// ]);
392393
// $this->checkFiles($actualFiles, $expectedFiles);
393-
394-
$this->deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment();
394+
$this->deleteTableFor60DescriptionOfAProperty();
395395
}
396396

397-
private function createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment()
397+
private function createTableFor60DescriptionOfAProperty()
398398
{
399399
Yii::$app->db->createCommand()->createTable('{{%animals}}', [
400400
'id' => 'pk',
@@ -403,7 +403,7 @@ private function createTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbT
403403
])->execute();
404404
}
405405

406-
private function deleteTableFor60DescriptionOfAPropertyInSpecMustCorrespondToDbTableColumnComment()
406+
private function deleteTableFor60DescriptionOfAProperty()
407407
{
408408
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%animals}}')->execute();
409409
}

0 commit comments

Comments
 (0)