diff --git a/UPGRADE.md b/UPGRADE.md index 3a0827a72c8..e671789cb78 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,10 @@ awareness about deprecated code. # Upgrade to 4.0 +## BC BREAK: removed default precision and scale of decimal columns. + +The DBAL no longer provides default values for precision and scale of decimal columns. + ## BC BREAK: a non-empty WHERE clause is not enforced in data manipulation `Connection` methods. The `Connection::update()` and `::delete()` methods no longer enforce a non-empty WHERE clause. If modification diff --git a/src/Exception/ColumnPrecisionRequired.php b/src/Exception/ColumnPrecisionRequired.php new file mode 100644 index 00000000000..a006345ffd3 --- /dev/null +++ b/src/Exception/ColumnPrecisionRequired.php @@ -0,0 +1,18 @@ +addColumn('col', 'decimal'); - $table->addColumn('col_unsigned', 'decimal', ['unsigned' => true]); + $table->addColumn('col', 'decimal', [ + 'precision' => 10, + 'scale' => 6, + ]); + + $table->addColumn('col_unsigned', 'decimal', [ + 'precision' => 10, + 'scale' => 6, + 'unsigned' => true, + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index bfa1a205408..957822cd56b 100644 --- a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php +++ b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php @@ -370,7 +370,11 @@ public function testListNegativeColumnDefaultValue(): void $table->addColumn('col_integer', 'integer', ['default' => -1]); $table->addColumn('col_bigint', 'bigint', ['default' => -1]); $table->addColumn('col_float', 'float', ['default' => -1.1]); - $table->addColumn('col_decimal', 'decimal', ['default' => -1.1]); + $table->addColumn('col_decimal', 'decimal', [ + 'precision' => 2, + 'scale' => 1, + 'default' => -1.1, + ]); $table->addColumn('col_string', 'string', ['default' => '(-1)']); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Schema/SQLServerSchemaManagerTest.php b/tests/Functional/Schema/SQLServerSchemaManagerTest.php index 8017e1704e8..6478a3ae4d3 100644 --- a/tests/Functional/Schema/SQLServerSchemaManagerTest.php +++ b/tests/Functional/Schema/SQLServerSchemaManagerTest.php @@ -25,7 +25,7 @@ public function testDropColumnConstraints(): void { $table = new Table('sqlsrv_drop_column'); $table->addColumn('id', 'integer'); - $table->addColumn('todrop', 'decimal', ['default' => 10.2]); + $table->addColumn('todrop', 'integer', ['default' => 10]); $this->schemaManager->createTable($table); @@ -33,7 +33,7 @@ public function testDropColumnConstraints(): void 'sqlsrv_drop_column', [], [], - ['todrop' => new Column('todrop', Type::getType('decimal'))], + ['todrop' => new Column('todrop', Type::getType('integer'))], ); $this->schemaManager->alterTable($diff); diff --git a/tests/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Platforms/AbstractMySQLPlatformTestCase.php index 6e64e230674..6fe0ee1e8f6 100644 --- a/tests/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Platforms/AbstractMySQLPlatformTestCase.php @@ -688,14 +688,9 @@ protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL(): array */ public static function getGeneratesDecimalTypeDeclarationSQL(): iterable { - return [ - [[], 'NUMERIC(10, 0)'], - [['unsigned' => true], 'NUMERIC(10, 0) UNSIGNED'], - [['unsigned' => false], 'NUMERIC(10, 0)'], - [['precision' => 5], 'NUMERIC(5, 0)'], - [['scale' => 5], 'NUMERIC(10, 5)'], - [['precision' => 8, 'scale' => 2], 'NUMERIC(8, 2)'], - ]; + yield [['precision' => 10, 'scale' => 8, 'unsigned' => true], 'NUMERIC(10, 8) UNSIGNED']; + + yield from parent::getGeneratesDecimalTypeDeclarationSQL(); } /** diff --git a/tests/Platforms/AbstractPlatformTestCase.php b/tests/Platforms/AbstractPlatformTestCase.php index c40a9c91371..aa84489559f 100644 --- a/tests/Platforms/AbstractPlatformTestCase.php +++ b/tests/Platforms/AbstractPlatformTestCase.php @@ -7,6 +7,8 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\Events; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Exception\ColumnPrecisionRequired; +use Doctrine\DBAL\Exception\ColumnScaleRequired; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ColumnDiff; @@ -739,6 +741,18 @@ public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength(): s return 'VARBINARY(16)'; } + public function testGetDecimalTypeDeclarationSQLNoPrecision(): void + { + $this->expectException(ColumnPrecisionRequired::class); + $this->platform->getDecimalTypeDeclarationSQL(['scale' => 2]); + } + + public function testGetDecimalTypeDeclarationSQLNoScale(): void + { + $this->expectException(ColumnScaleRequired::class); + $this->platform->getDecimalTypeDeclarationSQL(['precision' => 10]); + } + public function testReturnsJsonTypeDeclarationSQL(): void { $column = [ @@ -1207,17 +1221,11 @@ public function testGeneratesDecimalTypeDeclarationSQL(array $column, string $ex self::assertSame($expectedSql, $this->platform->getDecimalTypeDeclarationSQL($column)); } - /** @return mixed[][] */ + /** @return iterable,string}> */ public static function getGeneratesDecimalTypeDeclarationSQL(): iterable { - return [ - [[], 'NUMERIC(10, 0)'], - [['unsigned' => true], 'NUMERIC(10, 0)'], - [['unsigned' => false], 'NUMERIC(10, 0)'], - [['precision' => 5], 'NUMERIC(5, 0)'], - [['scale' => 5], 'NUMERIC(10, 5)'], - [['precision' => 8, 'scale' => 2], 'NUMERIC(8, 2)'], - ]; + yield [['precision' => 10, 'scale' => 0], 'NUMERIC(10, 0)']; + yield [['precision' => 8, 'scale' => 2], 'NUMERIC(8, 2)']; } /**