Skip to content

Commit 5f46f87

Browse files
authored
Fix phantom schema diff for float columns with default values (#7501)
1 parent 05c9919 commit 5f46f87

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

src/Platforms/AbstractPlatform.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,10 @@ public function getDefaultValueDeclarationSQL(array $column): string
15971597
return ' DEFAULT ' . $default;
15981598
}
15991599

1600+
if ($type instanceof Types\PhpFloatMappingType) {
1601+
return ' DEFAULT ' . (float) $default;
1602+
}
1603+
16001604
return ' DEFAULT ' . $this->quoteStringLiteral($default);
16011605
}
16021606

src/Types/FloatType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88

9-
class FloatType extends Type
9+
class FloatType extends Type implements PhpFloatMappingType
1010
{
1111
/**
1212
* {@inheritDoc}

src/Types/PhpFloatMappingType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Types;
6+
7+
/**
8+
* Implementations should map a database type to a PHP float.
9+
*
10+
* @internal
11+
*/
12+
interface PhpFloatMappingType
13+
{
14+
}

src/Types/SmallFloatType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88

9-
class SmallFloatType extends Type
9+
class SmallFloatType extends Type implements PhpFloatMappingType
1010
{
1111
/**
1212
* {@inheritDoc}

tests/Functional/Schema/ComparatorTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Platforms\MariaDBPlatform;
99
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1010
use Doctrine\DBAL\Schema\Column;
11+
use Doctrine\DBAL\Schema\ColumnEditor;
1112
use Doctrine\DBAL\Schema\Comparator;
1213
use Doctrine\DBAL\Schema\ComparatorConfig;
1314
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
@@ -17,6 +18,7 @@
1718
use Doctrine\DBAL\Types\Type;
1819
use Doctrine\DBAL\Types\Types;
1920
use PHPUnit\Framework\Attributes\DataProvider;
21+
use PHPUnit\Framework\Attributes\TestWith;
2022

2123
class ComparatorTest extends FunctionalTestCase
2224
{
@@ -61,6 +63,46 @@ public function testDefaultValueComparison(string $typeName, mixed $value): void
6163
);
6264
}
6365

66+
#[TestWith([Types::FLOAT])]
67+
#[TestWith([Types::SMALLFLOAT])]
68+
public function testFloatDefaultValueComparisonConverges(string $typeName): void
69+
{
70+
$table = Table::editor()
71+
->setUnquotedName('float_default_value')
72+
->setColumns(
73+
Column::editor()
74+
->setUnquotedName('score')
75+
->setTypeName($typeName)
76+
->setDefaultValue(14.75)
77+
->create(),
78+
)
79+
->create();
80+
81+
$this->dropAndCreateTable($table);
82+
83+
$comparator = $this->schemaManager->createComparator();
84+
85+
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
86+
$this->schemaManager,
87+
$comparator,
88+
$table,
89+
)->isEmpty());
90+
91+
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
92+
$this->schemaManager,
93+
$comparator,
94+
$table,
95+
)->isEmpty());
96+
97+
$desiredTable = $table->edit()
98+
->modifyColumnByUnquotedName('score', static function (ColumnEditor $editor): void {
99+
$editor->setDefaultValue(50.0);
100+
})
101+
->create();
102+
103+
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $comparator, $desiredTable);
104+
}
105+
64106
public function testRenameColumnComparison(): void
65107
{
66108
$platform = $this->connection->getDatabasePlatform();
@@ -147,6 +189,9 @@ public static function defaultValueProvider(): iterable
147189
{
148190
return [
149191
[Types::INTEGER, 1],
192+
[Types::FLOAT, 14.75],
193+
[Types::FLOAT, '50.0'],
194+
[Types::SMALLFLOAT, 14.75],
150195
[Types::BOOLEAN, false],
151196
[Types::TEXT, 'Doctrine'],
152197
];

0 commit comments

Comments
 (0)