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
This commit is contained in:
mark_story 2011-10-31 22:51:37 -04:00
parent 550076d75e
commit f531e7f24b
2 changed files with 39 additions and 2 deletions

View file

@ -250,7 +250,7 @@ class Sqlite extends DboSource {
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) {
return 'string';
}
if (in_array($col, array('blob', 'clob'))) {

View file

@ -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
@ -321,6 +321,20 @@ class SqliteTest extends CakeTestCase {
);
$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);
}
/**
@ -338,4 +352,27 @@ class SqliteTest extends CakeTestCase {
));
$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');
}
}