Skip to content
Permalink
Browse files

Exclude NULLS clauses in schema reflection.

Exclude `ASC|DESC NULLS FIRST|LAST` from column names as it not part of the
column name. Sadly we have to muck around with regex as I can't find
a way to get column metadata out of postgres.

Refs #7853
  • Loading branch information...
markstory committed Dec 17, 2015
1 parent 268dfa3 commit 2005e1d213eb8d439c670a8858bce17f2448543f
Showing with 45 additions and 1 deletion.
  1. +1 −1 src/Database/Schema/PostgresSchema.php
  2. +44 −0 tests/TestCase/Database/Schema/PostgresSchemaTest.php
@@ -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, [
@@ -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
*

0 comments on commit 2005e1d

Please sign in to comment.
You can’t perform that action at this time.