Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix incorrect ordering of columns in clustered indexes on sql server #826

Merged
merged 3 commits into from
Apr 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(array('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]);
}
}