Skip to content

Commit

Permalink
ensure postgres boolean values are handled correctly
Browse files Browse the repository at this point in the history
also fixed an issue with default value loading of bool columns.

fixes yiisoft#3489, fixes yiisoft#4085, fixes yiisoft#3920
related to yiisoft#4672
  • Loading branch information
cebe committed Aug 12, 2014
1 parent 92d65ab commit c6274ac
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Yii Framework 2 Change Log
- Bug #3863: Fixed incorrect js selector for `\yii\widgets\ActiveForm::errorSummaryCssClass` when it contains multiple classes (creocoder, umneeq)
- Bug #3893: Headers did not overwrite default setting by webserver (cebe)
- Bug #3909: `Html::to()` should not prefix base URL to URLs that already contain scheme (qiangxue)
- Bug #3920: Fixed issue with loading default values of PostgreSQL boolean columns (cebe)
- Bug #3934: yii.handleAction() in yii.js does not correctly detect if a hyperlink contains useful URL or not (joni-jones, qiangxue)
- Bug #3968: Messages logged in shutdown functions are not handled (qiangxue)
- Bug #3989: Fixed yii\log\FileTarget::$rotateByCopy to avoid any rename (cebe)
Expand Down
2 changes: 2 additions & 0 deletions framework/db/pgsql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ protected function findColumns($table)
} elseif ($column->defaultValue) {
if ($column->type === 'timestamp' && $column->defaultValue === 'now()') {
$column->defaultValue = new Expression($column->defaultValue);
} elseif ($column->type === 'boolean') {
$column->defaultValue = ($column->defaultValue === 'true');
} elseif (stripos($column->dbType, 'bit') === 0 || stripos($column->dbType, 'varbit') === 0) {
$column->defaultValue = bindec(trim($column->defaultValue, 'B\''));
} elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) {
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/data/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ CREATE TABLE "type" (

CREATE TABLE "bool_values" (
id serial not null primary key,
bool_col bool
bool_col bool,
default_true bool not null default true,
default_false boolean not null default false
);

INSERT INTO "profile" (description) VALUES ('profile customer 1');
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/framework/db/pgsql/PostgreSQLActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace yiiunit\framework\db\pgsql;

use yiiunit\data\ar\ActiveRecord;
use yiiunit\framework\db\ActiveRecordTest;

/**
Expand All @@ -11,4 +12,53 @@
class PostgreSQLActiveRecordTest extends ActiveRecordTest
{
protected $driverName = 'pgsql';


public function testBooleanValues()
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->batchInsert('bool_values',
['bool_col'], [
[true],
[false],
]
)->execute();

$this->assertEquals(1, BoolAR::find()->where('bool_col = TRUE')->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where('bool_col = FALSE')->count('*', $db));
$this->assertEquals(2, BoolAR::find()->where('bool_col IN (TRUE, FALSE)')->count('*', $db));

$this->assertEquals(1, BoolAR::find()->where(['bool_col' => true])->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where(['bool_col' => false])->count('*', $db));
$this->assertEquals(2, BoolAR::find()->where(['bool_col' => [true, false]])->count('*', $db));

$this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db));

$this->assertSame(true, BoolAR::find()->where(['bool_col' => true])->one($db)->bool_col);
$this->assertSame(false, BoolAR::find()->where(['bool_col' => false])->one($db)->bool_col);
}

public function testBooleanDefaultValues()
{
$model = new BoolAR();
$this->assertNull($model->bool_col);
$this->assertNull($model->default_true);
$this->assertNull($model->default_false);
$model->loadDefaultValues();
$this->assertNull($model->bool_col);
$this->assertSame(true, $model->default_true);
$this->assertSame(false, $model->default_false);

$this->assertTrue($model->save(false));
}
}

class BoolAR extends ActiveRecord
{
public static function tableName()
{
return 'bool_values';
}
}
24 changes: 24 additions & 0 deletions tests/unit/framework/db/pgsql/PostgreSQLQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace yiiunit\framework\db\pgsql;

use yii\db\pgsql\Schema;
use yii\db\Query;
use yiiunit\framework\db\QueryTest;
use yiiunit\framework\db\SchemaTest;

Expand All @@ -13,4 +14,27 @@
class PostgreSQLQueryTest extends QueryTest
{
public $driverName = 'pgsql';

public function testBooleanValues()
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->batchInsert('bool_values',
['bool_col'], [
[true],
[false],
]
)->execute();

$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = TRUE')->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = FALSE')->count('*', $db));
$this->assertEquals(2, (new Query())->from('bool_values')->where('bool_col IN (TRUE, FALSE)')->count('*', $db));

$this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => true])->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => false])->count('*', $db));
$this->assertEquals(2, (new Query())->from('bool_values')->where(['bool_col' => [true, false]])->count('*', $db));

$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db));
}
}
10 changes: 10 additions & 0 deletions tests/unit/framework/db/pgsql/PostgreSQLSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ public function testGetPDOType()
}
fclose($fp);
}

public function testBooleanDefaultValues()
{
/* @var $schema Schema */
$schema = $this->getConnection()->schema;

$table = $schema->getTableSchema('bool_values');
$this->assertSame(true, $table->getColumn('default_true')->defaultValue);
$this->assertSame(false, $table->getColumn('default_false')->defaultValue);
}
}

0 comments on commit c6274ac

Please sign in to comment.