From 1be5ddb57888d37e446b192e02cdf08840a3d66a Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Wed, 25 Oct 2023 15:11:39 -0300 Subject: [PATCH] Fix boolean type. (#280) --- .scrutinizer.yml | 3 +-- CHANGELOG.md | 1 + src/Schema.php | 14 ++++---------- tests/ColumnSchemaTest.php | 2 +- tests/CommandTest.php | 16 ---------------- tests/Provider/CommandProvider.php | 16 ---------------- tests/Provider/QueryBuilderProvider.php | 2 +- tests/Provider/SchemaProvider.php | 14 +++++++------- tests/Provider/Type/BitProvider.php | 5 ++--- tests/Support/Fixture/Type/bit.sql | 1 - tests/Support/Fixture/mssql.sql | 4 ++-- tests/Type/BitTest.php | 10 ++++------ 12 files changed, 23 insertions(+), 65 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index b57b3b6a9..ab7dc8817 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -56,8 +56,7 @@ build: - /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE yiitest' - pecl channel-update pecl.php.net - pecl install pdo_sqlsrv - - composer config preferred-install.yiisoft/db source - - composer update --no-interaction --no-progress --optimize-autoloader --ansi + - composer require yiisoft/db:dev-master --ansi tests: override: diff --git a/CHANGELOG.md b/CHANGELOG.md index 12140e25c..25dcaf9e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.0.2 under development - Bug #278: Remove `RECURSIVE` expression from CTE queries (@Tigrov) +- Bug #280: Fix type boolean (@terabytesoftw) ## 1.0.1 July 24, 2023 diff --git a/src/Schema.php b/src/Schema.php index f1fb7caa7..1d9bfcde1 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -444,6 +444,10 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface $column->type($this->typeMap[$type]); } + if ($type === 'bit') { + $column->type(self::TYPE_BOOLEAN); + } + if (!empty($matches[2])) { $values = explode(',', $matches[2]); $column->precision((int) $values[0]); @@ -452,16 +456,6 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface if (isset($values[1])) { $column->scale((int) $values[1]); } - - if ($column->getSize() === 1 && ($type === 'tinyint' || $type === 'bit')) { - $column->type(self::TYPE_BOOLEAN); - } elseif ($type === 'bit') { - if ($column->getSize() > 32) { - $column->type(self::TYPE_BIGINT); - } elseif ($column->getSize() === 32) { - $column->type(self::TYPE_INTEGER); - } - } } } diff --git a/tests/ColumnSchemaTest.php b/tests/ColumnSchemaTest.php index f7ef59a68..9642c05ad 100644 --- a/tests/ColumnSchemaTest.php +++ b/tests/ColumnSchemaTest.php @@ -56,7 +56,7 @@ public function testPhpTypeCast(): void $this->assertSame(1.234, $floatColPhpType); $this->assertSame("\x10\x11\x12", $blobColPhpType); $this->assertSame('2023-07-11 14:50:00.123', $datetimeColPhpType); - $this->assertEquals(false, $boolColPhpType); + $this->assertFalse($boolColPhpType); $db->close(); } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 797686568..2c1896c4e 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -51,22 +51,6 @@ public function testAlterColumn(): void $this->assertSame('ntext', $columns['email']->getDbType()); } - /** - * @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\CommandProvider::batchInsert - * - * @throws Throwable - */ - public function testBatchInsert( - string $table, - array $columns, - array $values, - string $expected, - array $expectedParams = [], - int $insertedRow = 1 - ): void { - parent::testBatchInsert($table, $columns, $values, $expected, $expectedParams, $insertedRow); - } - /** * @throws Exception * @throws InvalidConfigException diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 70326b2bc..5f429c403 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -18,22 +18,6 @@ final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider protected static string $driverName = 'sqlsrv'; - public static function batchInsert(): array - { - $batchInsert = parent::batchInsert(); - - $batchInsert['multirow']['expectedParams'][':qp3'] = 1; - $batchInsert['multirow']['expectedParams'][':qp7'] = 0; - - $batchInsert['issue11242']['expectedParams'][':qp3'] = 1; - - $batchInsert['wrongBehavior']['expectedParams'][':qp3'] = 0; - - $batchInsert['batchInsert binds params from expression']['expectedParams'][':qp3'] = 0; - - return $batchInsert; - } - /** * @throws JsonException */ diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index aea94267a..21c71be3f 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -128,7 +128,7 @@ public static function insertWithReturningPks(): array ['{{%type}}.[[related_id]]' => null, '[[time]]' => new Expression('now()')], [], << null], ], diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index 039aa5c17..fd8b8ac6a 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -171,9 +171,9 @@ public static function columns(): array 'defaultValue' => '2002-01-01 00:00:00', ], 'bool_col' => [ - 'type' => 'tinyint', - 'dbType' => 'tinyint', - 'phpType' => 'integer', + 'type' => 'boolean', + 'dbType' => 'bit', + 'phpType' => 'boolean', 'primaryKey' => false, 'allowNull' => false, 'autoIncrement' => false, @@ -184,9 +184,9 @@ public static function columns(): array 'defaultValue' => null, ], 'bool_col2' => [ - 'type' => 'tinyint', - 'dbType' => 'tinyint', - 'phpType' => 'integer', + 'type' => 'boolean', + 'dbType' => 'bit', + 'phpType' => 'boolean', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -194,7 +194,7 @@ public static function columns(): array 'size' => null, 'precision' => null, 'scale' => null, - 'defaultValue' => 1, + 'defaultValue' => true, ], ], 'tableName' => 'type', diff --git a/tests/Provider/Type/BitProvider.php b/tests/Provider/Type/BitProvider.php index 4b28ae78c..7872310bf 100644 --- a/tests/Provider/Type/BitProvider.php +++ b/tests/Provider/Type/BitProvider.php @@ -9,9 +9,8 @@ final class BitProvider public static function columns(): array { return [ - ['Mybit1', 'bit', 'integer', 0], - ['Mybit2', 'bit', 'integer', 1], - ['Mybit3', 'bit', 'integer', 2], + ['Mybit1', 'bit', 'boolean', false], + ['Mybit2', 'bit', 'boolean', true], ]; } } diff --git a/tests/Support/Fixture/Type/bit.sql b/tests/Support/Fixture/Type/bit.sql index ab003f9be..2701be7ee 100644 --- a/tests/Support/Fixture/Type/bit.sql +++ b/tests/Support/Fixture/Type/bit.sql @@ -15,7 +15,6 @@ CREATE TABLE [dbo].[bit_default] ( [id] [int] IDENTITY NOT NULL, [Mybit1] [bit] NOT NULL DEFAULT 0, [Mybit2] [bit] NOT NULL DEFAULT 1, - [Mybit3] [bit] NOT NULL DEFAULT 2, CONSTRAINT [PK_bit_default_pk] PRIMARY KEY CLUSTERED ( [id] ASC ) ON [PRIMARY] diff --git a/tests/Support/Fixture/mssql.sql b/tests/Support/Fixture/mssql.sql index b50283531..3fdc5fd56 100644 --- a/tests/Support/Fixture/mssql.sql +++ b/tests/Support/Fixture/mssql.sql @@ -156,8 +156,8 @@ CREATE TABLE [dbo].[type] ( [blob_col] [varbinary](MAX), [numeric_col] [decimal](5,2) DEFAULT '33.22', [datetime_col] [datetime] NOT NULL DEFAULT '2002-01-01 00:00:00', - [bool_col] [tinyint] NOT NULL, - [bool_col2] [tinyint] DEFAULT '1' + [bool_col] [bit] NOT NULL, + [bool_col2] [bit] DEFAULT '1' ); CREATE TABLE [dbo].[animal] ( diff --git a/tests/Type/BitTest.php b/tests/Type/BitTest.php index 2b5141ef3..afde50cef 100644 --- a/tests/Type/BitTest.php +++ b/tests/Type/BitTest.php @@ -37,7 +37,7 @@ public function testCreateTableWithDefaultValue( string $column, string $dbType, string $phpType, - int $defaultValue + bool $defaultValue ): void { $db = $this->buildTable(); @@ -89,7 +89,7 @@ public function testDefaultValue( string $column, string $dbType, string $phpType, - int $defaultValue + bool $defaultValue ): void { $this->setFixture('Type/bit.sql'); @@ -198,7 +198,7 @@ public function testMaxValue(): void $this->assertSame( [ 'id' => '2', - 'Mybit1' => '0', + 'Mybit1' => '1', 'Mybit2' => '1', 'Mybit3' => '1', ], @@ -251,7 +251,7 @@ public function testMinValue(): void [ 'id' => '2', 'Mybit1' => null, - 'Mybit2' => '0', + 'Mybit2' => '1', 'Mybit3' => '1', ], $command->setSql( @@ -280,7 +280,6 @@ private function buildTable(): ConnectionInterface 'id' => 'INT IDENTITY NOT NULL', 'Mybit1' => 'BIT DEFAULT 0', // Min value 'Mybit2' => 'BIT DEFAULT 1', // Max value - 'Mybit3' => 'BIT DEFAULT 2', // Max value ], )->execute(); @@ -293,7 +292,6 @@ private function getColumns(): array 'id' => '1', 'Mybit1' => '0', 'Mybit2' => '1', - 'Mybit3' => '1', ]; } }