From c1ae41da510befbc514a59ca486fb79d2a18f158 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 27 Aug 2013 18:11:04 -0400 Subject: [PATCH] Correctly generate bigint primary keys in sqlite. generate bigint primary keys correctly. Autoincrement cannot be set as it only works with INTEGER columns[1]. I decided to use some string manipulations as the entire SQL generation bits are a bit janky and I've already re-written them for 3.0. [1] https://www.sqlite.org/autoinc.html Closes #GH-1552 --- lib/Cake/Model/Datasource/Database/Sqlite.php | 13 +++++++++++-- .../Case/Model/Datasource/Database/SqliteTest.php | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Sqlite.php b/lib/Cake/Model/Datasource/Database/Sqlite.php index da61aa273..8251fc34b 100644 --- a/lib/Cake/Model/Datasource/Database/Sqlite.php +++ b/lib/Cake/Model/Datasource/Database/Sqlite.php @@ -407,10 +407,19 @@ class Sqlite extends DboSource { return null; } - if (isset($column['key']) && $column['key'] === 'primary' && $type === 'integer') { + $isPrimary = (isset($column['key']) && $column['key'] === 'primary'); + if ($isPrimary && $type === 'integer') { return $this->name($name) . ' ' . $this->columns['primary_key']['name']; } - return parent::buildColumn($column); + $out = parent::buildColumn($column); + if ($isPrimary && $type === 'biginteger') { + $replacement = 'PRIMARY KEY'; + if ($column['null'] === false) { + $replacement = 'NOT NULL ' . $replacement; + } + return str_replace($this->columns['primary_key']['name'], $replacement, $out); + } + return $out; } /** diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php index 564fe593e..a121da575 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php @@ -264,6 +264,17 @@ class SqliteTest extends CakeTestCase { $result = $this->Dbo->buildColumn($data); $expected = '"huge" bigint(20) NOT NULL'; $this->assertEquals($expected, $result); + + $data = array( + 'name' => 'id', + 'type' => 'biginteger', + 'length' => 20, + 'null' => false, + 'key' => 'primary', + ); + $result = $this->Dbo->buildColumn($data); + $expected = '"id" bigint(20) NOT NULL PRIMARY KEY'; + $this->assertEquals($expected, $result); } /**