Skip to content

Commit ed7f8d1

Browse files
committed
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
1 parent 9f58309 commit ed7f8d1

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

cake/libs/model/datasources/dbo/dbo_postgres.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ function &describe(&$model) {
265265
$this->_sequenceMap[$table][$c['name']] = $seq[1];
266266
}
267267
}
268+
if ($fields[$c['name']]['type'] == 'boolean' && !empty($fields[$c['name']]['default'])) {
269+
$fields[$c['name']]['default'] = constant($fields[$c['name']]['default']);
270+
}
268271
}
269272
}
270273
$this->__cacheDescription($table, $fields);

cake/libs/model/model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,11 +1086,11 @@ function create($data = array(), $filterKey = false) {
10861086

10871087
if ($data !== null && $data !== false) {
10881088
foreach ($this->schema() as $field => $properties) {
1089-
if ($this->primaryKey !== $field && isset($properties['default'])) {
1089+
if ($this->primaryKey !== $field && isset($properties['default']) && $properties['default'] !== '') {
10901090
$defaults[$field] = $properties['default'];
10911091
}
10921092
}
1093-
$this->set(Set::filter($defaults));
1093+
$this->set($defaults);
10941094
$this->set($data);
10951095
}
10961096
if ($filterKey) {

cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class DboPostgresTest extends CakeTestCase {
220220
*/
221221
var $fixtures = array('core.user', 'core.binary_test', 'core.comment', 'core.article',
222222
'core.tag', 'core.articles_tag', 'core.attachment', 'core.person', 'core.post', 'core.author',
223+
'core.datatype',
223224
);
224225
/**
225226
* Actual DB connection used in testing
@@ -437,6 +438,17 @@ function testBooleanNormalization() {
437438
$this->assertFalse($this->db2->boolean(''));
438439
}
439440

441+
/**
442+
* test that default -> false in schemas works correctly.
443+
*
444+
* @return void
445+
*/
446+
function testBooleanDefaultFalseInSchema() {
447+
$model = new Model(array('name' => 'Datatype', 'table' => 'datatypes', 'ds' => 'test_suite'));
448+
$model->create();
449+
$this->assertIdentical(false, $model->data['Datatype']['bool']);
450+
}
451+
440452
/**
441453
* testLastInsertIdMultipleInsert method
442454
*
@@ -446,23 +458,15 @@ function testBooleanNormalization() {
446458
function testLastInsertIdMultipleInsert() {
447459
$db1 = ConnectionManager::getDataSource('test_suite');
448460

449-
if (PHP5) {
450-
$db2 = clone $db1;
451-
} else {
452-
$db2 = $db1;
453-
}
454-
455-
$db2->connect();
456-
$this->assertNotEqual($db1->connection, $db2->connection);
457-
458461
$table = $db1->fullTableName('users', false);
459462
$password = '5f4dcc3b5aa765d61d8327deb882cf99';
460463
$db1->execute(
461464
"INSERT INTO {$table} (\"user\", password) VALUES ('mariano', '{$password}')"
462465
);
463-
$db2->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')");
464466
$this->assertEqual($db1->lastInsertId($table), 1);
465-
$this->assertEqual($db2->lastInsertId($table), 2);
467+
468+
$db1->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')");
469+
$this->assertEqual($db1->lastInsertId($table), 2);
466470
}
467471

468472
/**
@@ -582,39 +586,38 @@ function testCakeSchema() {
582586
$db1 =& ConnectionManager::getDataSource('test_suite');
583587
$db1->cacheSources = false;
584588
$db1->reconnect(array('persistent' => false));
585-
$db1->query('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' (
589+
$db1->query('CREATE TABLE ' . $db1->fullTableName('datatype_tests') . ' (
586590
id serial NOT NULL,
587591
"varchar" character varying(40) NOT NULL,
588592
"full_length" character varying NOT NULL,
589593
"timestamp" timestamp without time zone,
590594
date date,
591595
CONSTRAINT test_suite_data_types_pkey PRIMARY KEY (id)
592596
)');
593-
$model = new Model(array('name' => 'Datatype', 'ds' => 'test_suite'));
597+
$model = new Model(array('name' => 'DatatypeTest', 'ds' => 'test_suite'));
594598
$schema = new CakeSchema(array('connection' => 'test_suite'));
595599
$result = $schema->read(array(
596600
'connection' => 'test_suite',
597-
'models' => array('Datatype')
598601
));
599-
$schema->tables = array('datatypes' => $result['tables']['datatypes']);
600-
$result = $db1->createSchema($schema, 'datatypes');
602+
$schema->tables = array('datatype_tests' => $result['tables']['missing']['datatype_tests']);
603+
$result = $db1->createSchema($schema, 'datatype_tests');
601604

602605
$this->assertNoPattern('/timestamp DEFAULT/', $result);
603606
$this->assertPattern('/\"full_length\"\s*text\s.*,/', $result);
604607
$this->assertPattern('/timestamp\s*,/', $result);
605608

606-
$db1->query('DROP TABLE ' . $db1->fullTableName('datatypes'));
609+
$db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
607610

608611
$db1->query($result);
609612
$result2 = $schema->read(array(
610613
'connection' => 'test_suite',
611-
'models' => array('Datatype')
614+
'models' => array('DatatypeTest')
612615
));
613-
$schema->tables = array('datatypes' => $result2['tables']['datatypes']);
614-
$result2 = $db1->createSchema($schema, 'datatypes');
616+
$schema->tables = array('datatype_tests' => $result2['tables']['missing']['datatype_tests']);
617+
$result2 = $db1->createSchema($schema, 'datatype_tests');
615618
$this->assertEqual($result, $result2);
616619

617-
$db1->query('DROP TABLE ' . $db1->fullTableName('datatypes'));
620+
$db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
618621
}
619622

620623
/**

cake/tests/fixtures/datatype_fixture.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class DatatypeFixture extends CakeTestFixture {
4343
var $fields = array(
4444
'id' => array('type' => 'integer', 'null'=> false, 'default'=> 0, 'key' => 'primary'),
4545
'float_field' => array('type' => 'float', 'length' => '5,2', 'null' => false, 'default' => null),
46+
'bool' => array('type' => 'boolean', 'null' => false, 'default' => false),
4647
);
4748

4849
/**
@@ -52,6 +53,6 @@ class DatatypeFixture extends CakeTestFixture {
5253
* @access public
5354
*/
5455
var $records = array(
55-
array('id' => 1, 'float_field' => 42.23),
56+
array('id' => 1, 'float_field' => 42.23, 'bool' => false),
5657
);
5758
}

0 commit comments

Comments
 (0)