Commit c6274acf by Carsten Brandt

ensure postgres boolean values are handled correctly

also fixed an issue with default value loading of bool columns. fixes #3489, fixes #4085, fixes #3920 related to #4672
parent 92d65ab7
...@@ -57,6 +57,7 @@ Yii Framework 2 Change Log ...@@ -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 #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 #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 #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 #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 #3968: Messages logged in shutdown functions are not handled (qiangxue)
- Bug #3989: Fixed yii\log\FileTarget::$rotateByCopy to avoid any rename (cebe) - Bug #3989: Fixed yii\log\FileTarget::$rotateByCopy to avoid any rename (cebe)
......
...@@ -412,6 +412,8 @@ SQL; ...@@ -412,6 +412,8 @@ SQL;
} elseif ($column->defaultValue) { } elseif ($column->defaultValue) {
if ($column->type === 'timestamp' && $column->defaultValue === 'now()') { if ($column->type === 'timestamp' && $column->defaultValue === 'now()') {
$column->defaultValue = new Expression($column->defaultValue); $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) { } elseif (stripos($column->dbType, 'bit') === 0 || stripos($column->dbType, 'varbit') === 0) {
$column->defaultValue = bindec(trim($column->defaultValue, 'B\'')); $column->defaultValue = bindec(trim($column->defaultValue, 'B\''));
} elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) { } elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) {
......
...@@ -116,7 +116,9 @@ CREATE TABLE "type" ( ...@@ -116,7 +116,9 @@ CREATE TABLE "type" (
CREATE TABLE "bool_values" ( CREATE TABLE "bool_values" (
id serial not null primary key, 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'); INSERT INTO "profile" (description) VALUES ('profile customer 1');
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace yiiunit\framework\db\pgsql; namespace yiiunit\framework\db\pgsql;
use yiiunit\data\ar\ActiveRecord;
use yiiunit\framework\db\ActiveRecordTest; use yiiunit\framework\db\ActiveRecordTest;
/** /**
...@@ -11,4 +12,53 @@ use yiiunit\framework\db\ActiveRecordTest; ...@@ -11,4 +12,53 @@ use yiiunit\framework\db\ActiveRecordTest;
class PostgreSQLActiveRecordTest extends ActiveRecordTest class PostgreSQLActiveRecordTest extends ActiveRecordTest
{ {
protected $driverName = 'pgsql'; 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';
}
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace yiiunit\framework\db\pgsql; namespace yiiunit\framework\db\pgsql;
use yii\db\pgsql\Schema; use yii\db\pgsql\Schema;
use yii\db\Query;
use yiiunit\framework\db\QueryTest; use yiiunit\framework\db\QueryTest;
use yiiunit\framework\db\SchemaTest; use yiiunit\framework\db\SchemaTest;
...@@ -13,4 +14,27 @@ use yiiunit\framework\db\SchemaTest; ...@@ -13,4 +14,27 @@ use yiiunit\framework\db\SchemaTest;
class PostgreSQLQueryTest extends QueryTest class PostgreSQLQueryTest extends QueryTest
{ {
public $driverName = 'pgsql'; 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));
}
} }
...@@ -80,4 +80,14 @@ class PostgreSQLSchemaTest extends SchemaTest ...@@ -80,4 +80,14 @@ class PostgreSQLSchemaTest extends SchemaTest
} }
fclose($fp); 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);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment