Skip to content

Commit

Permalink
Merge pull request #7861 from cakephp/issue-7853
Browse files Browse the repository at this point in the history
Exclude NULLS clauses in schema reflection.
  • Loading branch information
markstory committed Dec 18, 2015
2 parents 88f28f8 + 2005e1d commit 4dcc6da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Database/Schema/PostgresSchema.php
Expand Up @@ -229,7 +229,7 @@ public function convertIndexDescription(Table $table, $row)
if ($row['indisunique'] && $type === Table::INDEX_INDEX) {
$type = Table::CONSTRAINT_UNIQUE;
}
preg_match('/\(([^\)]+)\)/', $row['statement'], $matches);
preg_match('/\(([^\)]+?)\s*(?:ASC|DESC)?(?:NULLS FIRST|LAST)?\)/', $row['statement'], $matches);
$columns = $this->_convertColumnList($matches[1]);
if ($type === Table::CONSTRAINT_PRIMARY || $type === Table::CONSTRAINT_UNIQUE) {
$table->addConstraint($name, [
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -539,6 +539,50 @@ public function testDescribeTableIndexes()
$this->assertEquals($expected, $result->index('author_idx'));
}

/**
* Test describing a table with indexes with nulls first
*
* @return void
*/
public function testDescribeTableIndexesNullsFirst()
{
$this->_needsConnection();
$connection = ConnectionManager::get('test');
$connection->execute('DROP TABLE IF EXISTS schema_index');

$table = <<<SQL
CREATE TABLE schema_index (
id serial NOT NULL,
user_id integer NOT NULL,
group_id integer NOT NULL,
grade double precision
)
WITH (
OIDS=FALSE
)
SQL;
$connection->execute($table);

$index = <<<SQL
CREATE INDEX schema_index_nulls
ON schema_index
USING btree
(group_id, grade DESC NULLS FIRST);
SQL;
$connection->execute($index);
$schema = new SchemaCollection($connection);

$result = $schema->describe('schema_index');
$this->assertCount(1, $result->indexes());
$expected = [
'type' => 'index',
'columns' => ['group_id', 'grade'],
'length' => []
];
$this->assertEquals($expected, $result->index('schema_index_nulls'));
$connection->execute('DROP TABLE schema_index');
}

/**
* Column provider for creating column sql
*
Expand Down

0 comments on commit 4dcc6da

Please sign in to comment.