Skip to content

Commit

Permalink
Fix UUID issue in SQLite
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed Nov 1, 2011
1 parent 550076d commit f531e7f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -250,7 +250,7 @@ public function column($real) {
if (in_array($col, array('text', 'integer', 'float', 'boolean', 'timestamp', 'date', 'datetime', 'time'))) {
return $col;
}
if (strpos($col, 'varchar') !== false) {
if (strpos($col, 'varchar') !== false || strpos($col, 'char') !== false) {

This comment has been minimized.

Copy link
@mdunham

mdunham Nov 3, 2011

Why not just:

if (strpos($col, 'char') !== false) {

This comment has been minimized.

Copy link
@markstory

markstory Nov 3, 2011

Author Member

Fair point.

This comment has been minimized.

Copy link
@markstory

markstory Nov 8, 2011

Author Member

Fixed in [2c91f11]

return 'string';
}
if (in_array($col, array('blob', 'clob'))) {
Expand Down
39 changes: 38 additions & 1 deletion lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php
Expand Up @@ -76,7 +76,7 @@ class SqliteTest extends CakeTestCase {
*
* @var object
*/
public $fixtures = array('core.user');
public $fixtures = array('core.user', 'core.uuid');

/**
* Actual DB connection used in testing
Expand Down Expand Up @@ -321,6 +321,20 @@ public function testDescribeWithUuidPrimaryKey() {
);
$this->assertEqual($result['id'], $expected);
$this->Dbo->query('DROP TABLE ' . $tableName);

$tableName = 'uuid_tests';
$this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
$Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
$result = $this->Dbo->describe($Model);
$expected = array(
'type' => 'string',
'length' => 36,
'null' => false,
'default' => null,
'key' => 'primary',
);
$this->assertEqual($result['id'], $expected);
$this->Dbo->query('DROP TABLE ' . $tableName);
}

/**
Expand All @@ -338,4 +352,27 @@ public function testVirtualFieldWithFunction() {
));
$this->assertEquals('ett', $result['User']['name']);
}

/**
* Test that records can be inserted with uuid primary keys, and
* that the primary key is not blank
*
* @return void
*/
public function testUuidPrimaryKeyInsertion() {
$this->loadFixtures('Uuid');
$Model = ClassRegistry::init('Uuid');

$data = array(
'title' => 'A uuid should work',
'count' => 10
);
$Model->create($data);
$this->assertTrue((bool)$Model->save());
$result = $Model->read();

$this->assertEquals($data['title'], $result['Uuid']['title']);
$this->assertTrue(Validation::uuid($result['Uuid']['id']), 'Not a uuid');
}

}

0 comments on commit f531e7f

Please sign in to comment.