From 4da3de55b4bab6090045624e9c7d16d144c26053 Mon Sep 17 00:00:00 2001 From: Bill Schaller Date: Tue, 31 Mar 2015 15:08:05 -0400 Subject: [PATCH 1/3] fix incorrect ordering of columns in clustered indexes on sql server when hydrated from schema info --- lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php index 8e9dbecbdd1..a554baa0090 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php @@ -893,7 +893,7 @@ public function getListTableIndexesSQL($table, $currentDatabase = null) JOIN sys.index_columns AS idxcol ON idx.object_id = idxcol.object_id AND idx.index_id = idxcol.index_id JOIN sys.columns AS col ON idxcol.object_id = col.object_id AND idxcol.column_id = col.column_id WHERE " . $this->getTableWhereClause($table, 'scm.name', 'tbl.name') . " - ORDER BY idx.index_id ASC, idxcol.index_column_id ASC"; + ORDER BY idx.index_id ASC, idxcol.key_ordinal ASC"; } /** From bc5fe05a87537a5d37d4d0527223a42cec3e2eb0 Mon Sep 17 00:00:00 2001 From: Bill Schaller Date: Thu, 2 Apr 2015 15:00:56 -0400 Subject: [PATCH 2/3] Add test for correct index column ordering on clustered indexes --- .../Schema/SQLServerSchemaManagerTest.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php index 948d0c0c642..429de39f01d 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php @@ -329,4 +329,30 @@ public function testColumnComments() $this->assertNull($columns['added_commented_type']->getComment()); $this->assertEquals('666', $columns['added_commented_type_with_comment']->getComment()); } + + public function testPkOrdering() + { + // SQL Server stores index column information in a system table with two + // columns that almost always have the same value: index_column_id and key_ordinal. + // The only situation when the two values doesn't match up is when a clustered index + // is declared that references columns in a different order from which they are + // declared in the table. In that case, key_ordinal != index_column_id. + // key_ordinal holds the index ordering. index_column_id is just a unique identifier + // for index columns within the given index. + $table = new Table('sqlsrv_pk_ordering'); + $table->addColumn('colA', 'integer', array('notnull' => true)); + $table->addColumn('colB', 'integer', array('notnull' => true)); + $table->setPrimaryKey(['colB', 'colA']); + $this->_sm->createTable($table); + + $indexes = $this->_sm->listTableIndexes('sqlsrv_pk_ordering'); + + $this->assertCount(1, $indexes); + + $firstIndex = current($indexes); + $columns = $firstIndex->getColumns(); + $this->assertCount(2, $columns); + $this->assertEquals('colB', $columns[0]); + $this->assertEquals('colA', $columns[1]); + } } From 52adcde96d1b230a0900c9cfa731317273931998 Mon Sep 17 00:00:00 2001 From: Bill Schaller Date: Wed, 8 Apr 2015 09:33:13 -0400 Subject: [PATCH 3/3] Change short array syntax to full array syntax Can't use short array in php < 5.4 --- .../Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php index 429de39f01d..da51840c73a 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php @@ -342,7 +342,7 @@ public function testPkOrdering() $table = new Table('sqlsrv_pk_ordering'); $table->addColumn('colA', 'integer', array('notnull' => true)); $table->addColumn('colB', 'integer', array('notnull' => true)); - $table->setPrimaryKey(['colB', 'colA']); + $table->setPrimaryKey(array('colB', 'colA')); $this->_sm->createTable($table); $indexes = $this->_sm->listTableIndexes('sqlsrv_pk_ordering');