Skip to content

Commit

Permalink
Add index/constraint support to PostgresSchema.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 20, 2013
1 parent 989903a commit 7320ce4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
37 changes: 27 additions & 10 deletions lib/Cake/Database/Schema/PostgresSchema.php
Expand Up @@ -177,8 +177,8 @@ public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
}
$table->addColumn($row['name'], $field);
if (!empty($row['pk'])) {
$table->addIndex('primary', [
'type' => Table::INDEX_PRIMARY,
$table->addConstraint('primary', [
'type' => Table::CONSTRAINT_PRIMARY,
'columns' => [$row['name']]
]);
}
Expand Down Expand Up @@ -266,7 +266,16 @@ public function columnSql(Table $table, $name) {
* @return string SQL fragment.
*/
public function indexSql(Table $table, $name) {
return '';
$data = $table->index($name);
$columns = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['columns']
);
return sprintf('CREATE INDEX %s ON %s (%s)',
$this->_driver->quoteIdentifier($name),
$this->_driver->quoteIdentifier($table->name()),
implode(', ', $columns)
);
}

/**
Expand All @@ -277,12 +286,12 @@ public function indexSql(Table $table, $name) {
* @return string SQL fragment.
*/
public function constraintSql(Table $table, $name) {
$data = $table->index($name);
$data = $table->constraint($name);
$out = 'CONSTRAINT ' . $this->_driver->quoteIdentifier($name);
if ($data['type'] === Table::INDEX_PRIMARY) {
if ($data['type'] === Table::CONSTRAINT_PRIMARY) {
$out = 'PRIMARY KEY ';
}
if ($data['type'] === Table::INDEX_UNIQUE) {
if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
$out .= ' UNIQUE ';
}
$columns = array_map(
Expand All @@ -296,12 +305,20 @@ public function constraintSql(Table $table, $name) {
* Generate the SQL to create a table.
*
* @param string $table The name of the table.
* @param array $lines The lines (columns + indexes) to go inside the table.
* @param array $columns The columns to go inside the table.
* @param array $constraints The constraints for the table.
* @param array $indexes The indexes for the table.
* @return string A complete CREATE TABLE statement
*/
public function createTableSql($table, $lines) {
$content = implode(",\n", array_filter($lines));
return sprintf("CREATE TABLE \"%s\" (\n%s\n);", $table, $content);
public function createTableSql($table, $columns, $constraints, $indexes) {
$content = array_merge($columns, $constraints);
$content = implode(",\n", array_filter($content));
$out = [];
$out[] = sprintf("CREATE TABLE \"%s\" (\n%s\n)", $table, $content);
foreach ($indexes as $index) {
$out[] = $index;
}
return $out;
}

}
22 changes: 16 additions & 6 deletions lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -444,7 +444,7 @@ public function testColumnSqlPrimaryKey() {
'type' => 'integer',
'null' => false
])
->addIndex('primary', [
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
]);
Expand Down Expand Up @@ -486,7 +486,7 @@ public function testConstraintSql($name, $data, $expected) {
'length' => 255
])->addColumn('author_id', [
'type' => 'integer',
])->addIndex($name, $data);
])->addConstraint($name, $data);

$this->assertEquals($expected, $schema->constraintSql($table, $name));
}
Expand All @@ -512,9 +512,13 @@ public function testCreateTableSql() {
])
->addColumn('body', ['type' => 'text'])
->addColumn('created', 'datetime')
->addIndex('primary', [
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
'columns' => ['id'],
])
->addIndex('title_idx', [
'type' => 'index',
'columns' => ['title'],
]);

$expected = <<<SQL
Expand All @@ -524,10 +528,16 @@ public function testCreateTableSql() {
"body" TEXT,
"created" TIMESTAMP,
PRIMARY KEY ("id")
);
)
SQL;
$result = $table->createTableSql($connection);
$this->assertEquals($expected, $result);

$this->assertCount(2, $result);
$this->assertEquals($expected, $result[0]);
$this->assertEquals(
'CREATE INDEX "title_idx" ON "atricles" ("title")',
$result[1]
);
}

/**
Expand Down

0 comments on commit 7320ce4

Please sign in to comment.