Commit 7c68e80f authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #516 from bartv2/sort-sqlite-pk-columns

The primary key columns don't have to be in the same order as the table columns
parents b6934334 454b8f40
...@@ -95,6 +95,14 @@ DateTimeTz ...@@ -95,6 +95,14 @@ DateTimeTz
Sqlite does not support saving timezones or offsets. The DateTimeTz Sqlite does not support saving timezones or offsets. The DateTimeTz
type therefore behave like the DateTime type. type therefore behave like the DateTime type.
Reverse engineering primary key order
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQLite versions < 3.7.16 only return that a column is part of the primary key,
but not the order. This is only a problem with tables where the order of the
columns in the table is not the same as the order in the primary key. Tables
created with Doctrine use the order of the columns as defined in the primary
key.
IBM DB2 IBM DB2
------- -------
......
...@@ -168,6 +168,12 @@ class SqliteSchemaManager extends AbstractSchemaManager ...@@ -168,6 +168,12 @@ class SqliteSchemaManager extends AbstractSchemaManager
// fetch primary // fetch primary
$stmt = $this->_conn->executeQuery("PRAGMA TABLE_INFO ('$tableName')"); $stmt = $this->_conn->executeQuery("PRAGMA TABLE_INFO ('$tableName')");
$indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC); $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
usort($indexArray, function($a, $b) {
if ($a['pk'] == $b['pk']) {
return $a['cid'] - $b['cid'];
}
return $a['pk'] - $b['pk'];
});
foreach ($indexArray as $indexColumnRow) { foreach ($indexArray as $indexColumnRow) {
if ($indexColumnRow['pk'] != "0") { if ($indexColumnRow['pk'] != "0") {
$indexBuffer[] = array( $indexBuffer[] = array(
......
...@@ -108,4 +108,27 @@ EOS ...@@ -108,4 +108,27 @@ EOS
$this->assertInstanceOf('Doctrine\DBAL\Types\BlobType', $table->getColumn('column_binary')->getType()); $this->assertInstanceOf('Doctrine\DBAL\Types\BlobType', $table->getColumn('column_binary')->getType());
$this->assertFalse($table->getColumn('column_binary')->getFixed()); $this->assertFalse($table->getColumn('column_binary')->getFixed());
} }
public function testNonDefaultPKOrder()
{
$version = \SQLite3::version();
if(version_compare($version['versionString'], '3.7.16', '<')) {
$this->markTestSkipped('This version of sqlite doesn\'t return the order of the Primary Key.');
}
$this->_conn->executeQuery(<<<EOS
CREATE TABLE non_default_pk_order (
id INTEGER,
other_id INTEGER,
PRIMARY KEY(other_id, id)
)
EOS
);
$tableIndexes = $this->_sm->listTableIndexes('non_default_pk_order');
$this->assertEquals(1, count($tableIndexes));
$this->assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
$this->assertEquals(array('other_id', 'id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment