Skip to content

Commit c5e1165

Browse files
committed
Add column comment support to postgres.
1 parent 7aea620 commit c5e1165

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Cake/Database/Schema/PostgresSchema.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,22 @@ public function constraintSql(Table $table, $name) {
313313
public function createTableSql($table, $columns, $constraints, $indexes) {
314314
$content = array_merge($columns, $constraints);
315315
$content = implode(",\n", array_filter($content));
316+
$tableName = $this->_driver->quoteIdentifier($table->name());
316317
$out = [];
317-
$out[] = sprintf("CREATE TABLE \"%s\" (\n%s\n)", $table, $content);
318+
$out[] = sprintf("CREATE TABLE %s (\n%s\n)", $tableName, $content);
318319
foreach ($indexes as $index) {
319320
$out[] = $index;
320321
}
322+
foreach ($table->columns() as $column) {
323+
$columnData = $table->column($column);
324+
if (isset($columnData['comment'])) {
325+
$out[] = sprintf('COMMENT ON COLUMN %s.%s IS %s',
326+
$tableName,
327+
$this->_driver->quoteIdentifier($column),
328+
$this->_driver->schemaValue($columnData['comment'])
329+
);
330+
}
331+
}
321332
return $out;
322333
}
323334

lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ public function testCreateTableSql() {
509509
->addColumn('title', [
510510
'type' => 'string',
511511
'null' => false,
512+
'comment' => 'This is the title',
512513
])
513514
->addColumn('body', ['type' => 'text'])
514515
->addColumn('created', 'datetime')
@@ -532,12 +533,16 @@ public function testCreateTableSql() {
532533
SQL;
533534
$result = $table->createTableSql($connection);
534535

535-
$this->assertCount(2, $result);
536+
$this->assertCount(3, $result);
536537
$this->assertEquals($expected, $result[0]);
537538
$this->assertEquals(
538-
'CREATE INDEX "title_idx" ON "atricles" ("title")',
539+
'CREATE INDEX "title_idx" ON "articles" ("title")',
539540
$result[1]
540541
);
542+
$this->assertEquals(
543+
'COMMENT ON COLUMN "articles"."title" IS "This is the title"',
544+
$result[2]
545+
);
541546
}
542547

543548
/**

0 commit comments

Comments
 (0)