Fixing issue where DboPostgres used the wrong type for boolean columns with a default of false or true.

Added a test case.
Changing Model::create() so it doesn't wipe out default values === false.
Fixes #1460
This commit is contained in:
mark_story 2011-01-21 13:31:33 -05:00
parent 9f583097f0
commit ed7f8d1906
4 changed files with 31 additions and 24 deletions

View file

@ -265,6 +265,9 @@ class DboPostgres extends DboSource {
$this->_sequenceMap[$table][$c['name']] = $seq[1];
}
}
if ($fields[$c['name']]['type'] == 'boolean' && !empty($fields[$c['name']]['default'])) {
$fields[$c['name']]['default'] = constant($fields[$c['name']]['default']);
}
}
}
$this->__cacheDescription($table, $fields);

View file

@ -1086,11 +1086,11 @@ class Model extends Overloadable {
if ($data !== null && $data !== false) {
foreach ($this->schema() as $field => $properties) {
if ($this->primaryKey !== $field && isset($properties['default'])) {
if ($this->primaryKey !== $field && isset($properties['default']) && $properties['default'] !== '') {
$defaults[$field] = $properties['default'];
}
}
$this->set(Set::filter($defaults));
$this->set($defaults);
$this->set($data);
}
if ($filterKey) {

View file

@ -220,6 +220,7 @@ class DboPostgresTest extends CakeTestCase {
*/
var $fixtures = array('core.user', 'core.binary_test', 'core.comment', 'core.article',
'core.tag', 'core.articles_tag', 'core.attachment', 'core.person', 'core.post', 'core.author',
'core.datatype',
);
/**
* Actual DB connection used in testing
@ -437,6 +438,17 @@ class DboPostgresTest extends CakeTestCase {
$this->assertFalse($this->db2->boolean(''));
}
/**
* test that default -> false in schemas works correctly.
*
* @return void
*/
function testBooleanDefaultFalseInSchema() {
$model = new Model(array('name' => 'Datatype', 'table' => 'datatypes', 'ds' => 'test_suite'));
$model->create();
$this->assertIdentical(false, $model->data['Datatype']['bool']);
}
/**
* testLastInsertIdMultipleInsert method
*
@ -446,23 +458,15 @@ class DboPostgresTest extends CakeTestCase {
function testLastInsertIdMultipleInsert() {
$db1 = ConnectionManager::getDataSource('test_suite');
if (PHP5) {
$db2 = clone $db1;
} else {
$db2 = $db1;
}
$db2->connect();
$this->assertNotEqual($db1->connection, $db2->connection);
$table = $db1->fullTableName('users', false);
$password = '5f4dcc3b5aa765d61d8327deb882cf99';
$db1->execute(
"INSERT INTO {$table} (\"user\", password) VALUES ('mariano', '{$password}')"
);
$db2->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')");
$this->assertEqual($db1->lastInsertId($table), 1);
$this->assertEqual($db2->lastInsertId($table), 2);
$db1->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')");
$this->assertEqual($db1->lastInsertId($table), 2);
}
/**
@ -582,7 +586,7 @@ class DboPostgresTest extends CakeTestCase {
$db1 =& ConnectionManager::getDataSource('test_suite');
$db1->cacheSources = false;
$db1->reconnect(array('persistent' => false));
$db1->query('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' (
$db1->query('CREATE TABLE ' . $db1->fullTableName('datatype_tests') . ' (
id serial NOT NULL,
"varchar" character varying(40) NOT NULL,
"full_length" character varying NOT NULL,
@ -590,31 +594,30 @@ class DboPostgresTest extends CakeTestCase {
date date,
CONSTRAINT test_suite_data_types_pkey PRIMARY KEY (id)
)');
$model = new Model(array('name' => 'Datatype', 'ds' => 'test_suite'));
$model = new Model(array('name' => 'DatatypeTest', 'ds' => 'test_suite'));
$schema = new CakeSchema(array('connection' => 'test_suite'));
$result = $schema->read(array(
'connection' => 'test_suite',
'models' => array('Datatype')
));
$schema->tables = array('datatypes' => $result['tables']['datatypes']);
$result = $db1->createSchema($schema, 'datatypes');
$schema->tables = array('datatype_tests' => $result['tables']['missing']['datatype_tests']);
$result = $db1->createSchema($schema, 'datatype_tests');
$this->assertNoPattern('/timestamp DEFAULT/', $result);
$this->assertPattern('/\"full_length\"\s*text\s.*,/', $result);
$this->assertPattern('/timestamp\s*,/', $result);
$db1->query('DROP TABLE ' . $db1->fullTableName('datatypes'));
$db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
$db1->query($result);
$result2 = $schema->read(array(
'connection' => 'test_suite',
'models' => array('Datatype')
'models' => array('DatatypeTest')
));
$schema->tables = array('datatypes' => $result2['tables']['datatypes']);
$result2 = $db1->createSchema($schema, 'datatypes');
$schema->tables = array('datatype_tests' => $result2['tables']['missing']['datatype_tests']);
$result2 = $db1->createSchema($schema, 'datatype_tests');
$this->assertEqual($result, $result2);
$db1->query('DROP TABLE ' . $db1->fullTableName('datatypes'));
$db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
}
/**

View file

@ -43,6 +43,7 @@ class DatatypeFixture extends CakeTestFixture {
var $fields = array(
'id' => array('type' => 'integer', 'null'=> false, 'default'=> 0, 'key' => 'primary'),
'float_field' => array('type' => 'float', 'length' => '5,2', 'null' => false, 'default' => null),
'bool' => array('type' => 'boolean', 'null' => false, 'default' => false),
);
/**
@ -52,6 +53,6 @@ class DatatypeFixture extends CakeTestFixture {
* @access public
*/
var $records = array(
array('id' => 1, 'float_field' => 42.23),
array('id' => 1, 'float_field' => 42.23, 'bool' => false),
);
}