forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed boolean handling for PostgreSQL
- do not allow boolean values for integer columns - use native boolean type with boolean values - removed workaround that turned out to be wrong when schema and values are used correctly. Workaround resulted from wrong usage of boolean values before. fixes yiisoft#4672
- Loading branch information
Showing
5 changed files
with
147 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ CREATE TABLE "customer" ( | |
name varchar(128), | ||
address text, | ||
status integer DEFAULT 0, | ||
bool_status boolean DEFAULT FALSE, | ||
profile_id integer | ||
); | ||
|
||
|
@@ -109,8 +110,8 @@ CREATE TABLE "type" ( | |
blob_col bytea, | ||
numeric_col decimal(5,2) DEFAULT '33.22', | ||
time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00', | ||
bool_col smallint NOT NULL, | ||
bool_col2 smallint DEFAULT '1', | ||
bool_col boolean NOT NULL, | ||
bool_col2 boolean DEFAULT TRUE, | ||
ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
bit_col BIT(8) NOT NULL DEFAULT B'10000010' | ||
); | ||
|
@@ -125,9 +126,9 @@ CREATE TABLE "bool_values" ( | |
INSERT INTO "profile" (description) VALUES ('profile customer 1'); | ||
INSERT INTO "profile" (description) VALUES ('profile customer 3'); | ||
|
||
INSERT INTO "customer" (email, name, address, status, profile_id) VALUES ('[email protected]', 'user1', 'address1', 1, 1); | ||
INSERT INTO "customer" (email, name, address, status) VALUES ('[email protected]', 'user2', 'address2', 1); | ||
INSERT INTO "customer" (email, name, address, status, profile_id) VALUES ('[email protected]', 'user3', 'address3', 2, 2); | ||
INSERT INTO "customer" (email, name, address, status, bool_status, profile_id) VALUES ('[email protected]', 'user1', 'address1', 1, true, 1); | ||
INSERT INTO "customer" (email, name, address, status, bool_status) VALUES ('[email protected]', 'user2', 'address2', 1, true); | ||
INSERT INTO "customer" (email, name, address, status, bool_status, profile_id) VALUES ('[email protected]', 'user3', 'address3', 2, false, 2); | ||
|
||
INSERT INTO "category" (name) VALUES ('Books'); | ||
INSERT INTO "category" (name) VALUES ('Movies'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,12 @@ | |
|
||
namespace yiiunit\framework\db\pgsql; | ||
|
||
use yii\behaviors\TimestampBehavior; | ||
use yii\db\pgsql\Schema; | ||
use yiiunit\data\ar\ActiveRecord; | ||
use yiiunit\framework\ar\ActiveRecordTestTrait; | ||
use yiiunit\framework\db\ActiveRecordTest; | ||
use yiiunit\TestCase; | ||
|
||
/** | ||
* @group db | ||
|
@@ -13,6 +17,72 @@ class PostgreSQLActiveRecordTest extends ActiveRecordTest | |
{ | ||
protected $driverName = 'pgsql'; | ||
|
||
public function testBooleanAttribute() | ||
{ | ||
/* @var $customerClass \yii\db\ActiveRecordInterface */ | ||
$customerClass = $this->getCustomerClass(); | ||
/* @var $this TestCase|ActiveRecordTestTrait */ | ||
$customer = new $customerClass(); | ||
$customer->name = 'boolean customer'; | ||
$customer->email = '[email protected]'; | ||
$customer->bool_status = false; | ||
$customer->save(false); | ||
|
||
$customer->refresh(); | ||
$this->assertSame(false, $customer->bool_status); | ||
|
||
$customer->bool_status = true; | ||
$customer->save(false); | ||
|
||
$customer->refresh(); | ||
$this->assertSame(true, $customer->bool_status); | ||
|
||
$customers = $customerClass::find()->where(['bool_status' => true])->all(); | ||
$this->assertEquals(3, count($customers)); | ||
|
||
$customers = $customerClass::find()->where(['bool_status' => false])->all(); | ||
$this->assertEquals(1, count($customers)); | ||
} | ||
|
||
public function testFindAsArray() | ||
{ | ||
/* @var $customerClass \yii\db\ActiveRecordInterface */ | ||
$customerClass = $this->getCustomerClass(); | ||
|
||
// asArray | ||
$customer = $customerClass::find()->where(['id' => 2])->asArray()->one(); | ||
$this->assertEquals([ | ||
'id' => 2, | ||
'email' => '[email protected]', | ||
'name' => 'user2', | ||
'address' => 'address2', | ||
'status' => 1, | ||
'profile_id' => null, | ||
'bool_status' => true, | ||
], $customer); | ||
|
||
// find all asArray | ||
$customers = $customerClass::find()->asArray()->all(); | ||
$this->assertEquals(3, count($customers)); | ||
$this->assertArrayHasKey('id', $customers[0]); | ||
$this->assertArrayHasKey('name', $customers[0]); | ||
$this->assertArrayHasKey('email', $customers[0]); | ||
$this->assertArrayHasKey('address', $customers[0]); | ||
$this->assertArrayHasKey('status', $customers[0]); | ||
$this->assertArrayHasKey('bool_status', $customers[0]); | ||
$this->assertArrayHasKey('id', $customers[1]); | ||
$this->assertArrayHasKey('name', $customers[1]); | ||
$this->assertArrayHasKey('email', $customers[1]); | ||
$this->assertArrayHasKey('address', $customers[1]); | ||
$this->assertArrayHasKey('status', $customers[1]); | ||
$this->assertArrayHasKey('bool_status', $customers[1]); | ||
$this->assertArrayHasKey('id', $customers[2]); | ||
$this->assertArrayHasKey('name', $customers[2]); | ||
$this->assertArrayHasKey('email', $customers[2]); | ||
$this->assertArrayHasKey('address', $customers[2]); | ||
$this->assertArrayHasKey('status', $customers[2]); | ||
$this->assertArrayHasKey('bool_status', $customers[2]); | ||
} | ||
|
||
public function testBooleanValues() | ||
{ | ||
|
@@ -40,6 +110,42 @@ public function testBooleanValues() | |
$this->assertSame(false, BoolAR::find()->where(['bool_col' => false])->one($db)->bool_col); | ||
} | ||
|
||
/** | ||
* https://github.com/yiisoft/yii2/issues/4672 | ||
*/ | ||
public function testBooleanValues2() | ||
{ | ||
$db = $this->getConnection(); | ||
$db->charset = 'utf8'; | ||
|
||
$db->createCommand("DROP TABLE IF EXISTS bool_user;")->execute(); | ||
$db->createCommand()->createTable('bool_user', [ | ||
'id' => Schema::TYPE_PK, | ||
'username' => Schema::TYPE_STRING . ' NOT NULL', | ||
'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL', | ||
'password_hash' => Schema::TYPE_STRING . ' NOT NULL', | ||
'password_reset_token' => Schema::TYPE_STRING, | ||
'email' => Schema::TYPE_STRING . ' NOT NULL', | ||
'role' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', | ||
|
||
'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', | ||
'created_at' => Schema::TYPE_INTEGER . ' NOT NULL', | ||
'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL', | ||
])->execute(); | ||
$db->createCommand()->addColumn('bool_user', 'is_deleted', Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT FALSE')->execute(); | ||
|
||
$user = new UserAR(); | ||
$user->username = 'test'; | ||
$user->auth_key = 'test'; | ||
$user->password_hash = 'test'; | ||
$user->email = '[email protected]'; | ||
$user->save(false); | ||
|
||
$this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => false])->all($db))); | ||
$this->assertEquals(0, count(UserAR::find()->where(['is_deleted' => true])->all($db))); | ||
$this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => [true, false]])->all($db))); | ||
} | ||
|
||
public function testBooleanDefaultValues() | ||
{ | ||
$model = new BoolAR(); | ||
|
@@ -61,4 +167,24 @@ public static function tableName() | |
{ | ||
return 'bool_values'; | ||
} | ||
} | ||
} | ||
|
||
class UserAR extends ActiveRecord | ||
{ | ||
const STATUS_DELETED = 0; | ||
const STATUS_ACTIVE = 10; | ||
const ROLE_USER = 10; | ||
|
||
public static function tableName() | ||
{ | ||
return '{{%bool_user}}'; | ||
} | ||
|
||
public function behaviors() | ||
{ | ||
return [ | ||
TimestampBehavior::className(), | ||
]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters