Skip to content

Commit f531e7f

Browse files
committed
Fix UUID issue in SQLite
Only varchar(36) was interpreted as a uuid. char(36) should also be treated this way. Most documentation refers to this type. Also char(x) fields should be treated as strings, not text. Fixes #2184
1 parent 550076d commit f531e7f

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/Cake/Model/Datasource/Database/Sqlite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function column($real) {
250250
if (in_array($col, array('text', 'integer', 'float', 'boolean', 'timestamp', 'date', 'datetime', 'time'))) {
251251
return $col;
252252
}
253-
if (strpos($col, 'varchar') !== false) {
253+
if (strpos($col, 'varchar') !== false || strpos($col, 'char') !== false) {
254254
return 'string';
255255
}
256256
if (in_array($col, array('blob', 'clob'))) {

lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class SqliteTest extends CakeTestCase {
7676
*
7777
* @var object
7878
*/
79-
public $fixtures = array('core.user');
79+
public $fixtures = array('core.user', 'core.uuid');
8080

8181
/**
8282
* Actual DB connection used in testing
@@ -321,6 +321,20 @@ public function testDescribeWithUuidPrimaryKey() {
321321
);
322322
$this->assertEqual($result['id'], $expected);
323323
$this->Dbo->query('DROP TABLE ' . $tableName);
324+
325+
$tableName = 'uuid_tests';
326+
$this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
327+
$Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
328+
$result = $this->Dbo->describe($Model);
329+
$expected = array(
330+
'type' => 'string',
331+
'length' => 36,
332+
'null' => false,
333+
'default' => null,
334+
'key' => 'primary',
335+
);
336+
$this->assertEqual($result['id'], $expected);
337+
$this->Dbo->query('DROP TABLE ' . $tableName);
324338
}
325339

326340
/**
@@ -338,4 +352,27 @@ public function testVirtualFieldWithFunction() {
338352
));
339353
$this->assertEquals('ett', $result['User']['name']);
340354
}
355+
356+
/**
357+
* Test that records can be inserted with uuid primary keys, and
358+
* that the primary key is not blank
359+
*
360+
* @return void
361+
*/
362+
public function testUuidPrimaryKeyInsertion() {
363+
$this->loadFixtures('Uuid');
364+
$Model = ClassRegistry::init('Uuid');
365+
366+
$data = array(
367+
'title' => 'A uuid should work',
368+
'count' => 10
369+
);
370+
$Model->create($data);
371+
$this->assertTrue((bool)$Model->save());
372+
$result = $Model->read();
373+
374+
$this->assertEquals($data['title'], $result['Uuid']['title']);
375+
$this->assertTrue(Validation::uuid($result['Uuid']['id']), 'Not a uuid');
376+
}
377+
341378
}

0 commit comments

Comments
 (0)