Skip to content

Commit

Permalink
Add column comment support to postgres.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 20, 2013
1 parent 7aea620 commit c5e1165
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/Cake/Database/Schema/PostgresSchema.php
Expand Up @@ -313,11 +313,22 @@ public function constraintSql(Table $table, $name) {
public function createTableSql($table, $columns, $constraints, $indexes) {
$content = array_merge($columns, $constraints);
$content = implode(",\n", array_filter($content));
$tableName = $this->_driver->quoteIdentifier($table->name());
$out = [];
$out[] = sprintf("CREATE TABLE \"%s\" (\n%s\n)", $table, $content);
$out[] = sprintf("CREATE TABLE %s (\n%s\n)", $tableName, $content);
foreach ($indexes as $index) {
$out[] = $index;
}
foreach ($table->columns() as $column) {
$columnData = $table->column($column);
if (isset($columnData['comment'])) {
$out[] = sprintf('COMMENT ON COLUMN %s.%s IS %s',
$tableName,
$this->_driver->quoteIdentifier($column),
$this->_driver->schemaValue($columnData['comment'])
);
}
}
return $out;
}

Expand Down
9 changes: 7 additions & 2 deletions lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -509,6 +509,7 @@ public function testCreateTableSql() {
->addColumn('title', [
'type' => 'string',
'null' => false,
'comment' => 'This is the title',
])
->addColumn('body', ['type' => 'text'])
->addColumn('created', 'datetime')
Expand All @@ -532,12 +533,16 @@ public function testCreateTableSql() {
SQL;
$result = $table->createTableSql($connection);

$this->assertCount(2, $result);
$this->assertCount(3, $result);
$this->assertEquals($expected, $result[0]);
$this->assertEquals(
'CREATE INDEX "title_idx" ON "atricles" ("title")',
'CREATE INDEX "title_idx" ON "articles" ("title")',
$result[1]
);
$this->assertEquals(
'COMMENT ON COLUMN "articles"."title" IS "This is the title"',
$result[2]
);
}

/**
Expand Down

0 comments on commit c5e1165

Please sign in to comment.