From 7320ce446dd1a9c3c7e82e647442e722b1eb174e Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 19 May 2013 22:41:34 -0400 Subject: [PATCH] Add index/constraint support to PostgresSchema. --- lib/Cake/Database/Schema/PostgresSchema.php | 37 ++++++++++++++----- .../Database/Schema/PostgresSchemaTest.php | 22 ++++++++--- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Database/Schema/PostgresSchema.php b/lib/Cake/Database/Schema/PostgresSchema.php index e20cf024a17..7f583706f2a 100644 --- a/lib/Cake/Database/Schema/PostgresSchema.php +++ b/lib/Cake/Database/Schema/PostgresSchema.php @@ -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']] ]); } @@ -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) + ); } /** @@ -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( @@ -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; } } diff --git a/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php index aa1afdbf524..ea895a2bbb7 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php @@ -444,7 +444,7 @@ public function testColumnSqlPrimaryKey() { 'type' => 'integer', 'null' => false ]) - ->addIndex('primary', [ + ->addConstraint('primary', [ 'type' => 'primary', 'columns' => ['id'] ]); @@ -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)); } @@ -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 = <<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] + ); } /**