diff --git a/lib/Cake/Database/Schema/PostgresSchema.php b/lib/Cake/Database/Schema/PostgresSchema.php index 7f583706f2a..8cfc3435da9 100644 --- a/lib/Cake/Database/Schema/PostgresSchema.php +++ b/lib/Cake/Database/Schema/PostgresSchema.php @@ -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; } diff --git a/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php index ea895a2bbb7..13d04c4fd30 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php @@ -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') @@ -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] + ); } /**