diff --git a/CHANGELOG.md b/CHANGELOG.md index e1dbc55fe..4511e5b9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ - New #355, #368, #370, #399: Implement `ColumnFactory` class (@Tigrov) - Enh #359: Separate column type constants (@Tigrov) - Enh #359: Remove `Schema::TYPE_ARRAY` and `Schema::TYPE_STRUCTURED` constants (@Tigrov) -- New #360: Realize `ColumnBuilder` class (@Tigrov) +- New #360, #460: Realize `ColumnBuilder` class (@Tigrov, @vjik) - Enh #362: Update according changes in `ColumnSchemaInterface` (@Tigrov) - New #364, #372: Add `ColumnDefinitionBuilder` class (@Tigrov) - Enh #365, #427: Refactor `Dsn` class (@Tigrov) @@ -56,6 +56,7 @@ - Enh #444: Improve `ArrayExpressionBuilder` and `JsonExpressionBuilder` classes (@Tigrov) - Chg #447: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov) - Bug #456: Fix typecasting bit columns' values with big size (@Tigrov) +- Chg #460: Throw exception on "unsigned" column usage (@vjik) ## 1.3.0 March 21, 2024 diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php index d99451211..cf0d4c1ff 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -37,9 +37,11 @@ public static function integer(?int $size = null): IntegerColumn return new IntegerColumn(ColumnType::INTEGER, size: $size); } - public static function bigint(?int $size = null): IntegerColumn + public static function bigint(?int $size = null, bool $unsigned = false): BigIntColumn|IntegerColumn { - return new IntegerColumn(ColumnType::BIGINT, size: $size); + return PHP_INT_SIZE === 4 || $unsigned + ? new BigIntColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned) + : new IntegerColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned); } public static function binary(?int $size = null): BinaryColumn diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index 7d2693f40..26bb1c45d 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Pgsql\Column; use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder; use Yiisoft\Db\Schema\Column\AbstractArrayColumn; use Yiisoft\Db\Schema\Column\CollatableColumnInterface; @@ -40,6 +41,10 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder public function build(ColumnInterface $column): string { + if ($column->isUnsigned()) { + throw new NotSupportedException('The "unsigned" attribute is not supported by PostgreSQL.'); + } + return $this->buildType($column) . $this->buildNotNull($column) . $this->buildPrimaryKey($column) diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 270314eb0..8068888d1 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -104,6 +104,12 @@ public function fromType(string $type, array $info = []): ColumnInterface return $column; } + public function fromPseudoType(string $pseudoType, array $info = []): ColumnInterface + { + // PostgreSQL doesn't support unsigned types + return parent::fromPseudoType($pseudoType, $info)->unsigned(false); + } + protected function columnDefinitionParser(): ColumnDefinitionParser { return new ColumnDefinitionParser(); diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 3b8928ec6..f30edcd98 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -105,9 +105,9 @@ public static function pseudoTypes(): array { $result = parent::pseudoTypes(); $result['pk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true); - $result['upk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true, unsigned: true); + $result['upk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true, unsigned: false); $result['bigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true); - $result['ubigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true, unsigned: true); + $result['ubigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true, unsigned: false); return $result; } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 289d23655..bfd2ec6d5 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -396,6 +396,12 @@ public static function buildColumnDefinition(): array { $values = parent::buildColumnDefinition(); + // PostgreSQL does not support unsigned types + unset( + $values['bigint(15) unsigned'], + $values['unsigned()'], + ); + $values[PseudoType::PK][0] = 'serial PRIMARY KEY'; $values[PseudoType::UPK][0] = 'serial PRIMARY KEY'; $values[PseudoType::BIGPK][0] = 'bigserial PRIMARY KEY'; @@ -443,7 +449,6 @@ public static function buildColumnDefinition(): array $values['structured()'][0] = 'jsonb'; $values['json()'][0] = 'jsonb'; $values['json(100)'][0] = 'jsonb'; - $values['unsigned()'][0] = 'integer'; $values['scale(2)'][0] = 'numeric(10,2)'; $values['integer(8)->scale(2)'][0] = 'integer'; $values["collation('collation_name')"] = [