Skip to content

Commit

Permalink
Fixing issue where DboPostgres used the wrong type for boolean column…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
markstory committed Jan 21, 2011
1 parent 9f58309 commit ed7f8d1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
3 changes: 3 additions & 0 deletions cake/libs/model/datasources/dbo/dbo_postgres.php
Expand Up @@ -265,6 +265,9 @@ function &describe(&$model) {
$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);
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/model/model.php
Expand Up @@ -1086,11 +1086,11 @@ function create($data = array(), $filterKey = false) {

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) {
Expand Down
45 changes: 24 additions & 21 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php
Expand Up @@ -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
Expand Down Expand Up @@ -437,6 +438,17 @@ function testBooleanNormalization() {
$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
*
Expand All @@ -446,23 +458,15 @@ function testBooleanNormalization() {
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);
}

/**
Expand Down Expand Up @@ -582,39 +586,38 @@ function testCakeSchema() {
$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,
"timestamp" timestamp without time zone,
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'));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion cake/tests/fixtures/datatype_fixture.php
Expand Up @@ -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),
);

/**
Expand All @@ -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),
);
}

0 comments on commit ed7f8d1

Please sign in to comment.