Skip to content

[11.x] Make floating-point types consistent #48861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 7 additions & 52 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,28 +958,23 @@ public function foreignIdFor($model, $column = null)
* Create a new float column on the table.
*
* @param string $column
* @param int $total
* @param int $places
* @param bool $unsigned
* @param int $precision
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function float($column, $total = 8, $places = 2, $unsigned = false)
public function float($column, $precision = 53)
{
return $this->addColumn('float', $column, compact('total', 'places', 'unsigned'));
return $this->addColumn('float', $column, compact('precision'));
}

/**
* Create a new double column on the table.
*
* @param string $column
* @param int|null $total
* @param int|null $places
* @param bool $unsigned
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function double($column, $total = 15, $places = 6, $unsigned = false)
public function double($column)
{
return $this->addColumn('double', $column, compact('total', 'places', 'unsigned'));
return $this->addColumn('double', $column);
}

/**
Expand All @@ -988,51 +983,11 @@ public function double($column, $total = 15, $places = 6, $unsigned = false)
* @param string $column
* @param int $total
* @param int $places
* @param bool $unsigned
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function decimal($column, $total = 8, $places = 2, $unsigned = false)
{
return $this->addColumn('decimal', $column, compact('total', 'places', 'unsigned'));
}

/**
* Create a new unsigned float column on the table.
*
* @param string $column
* @param int $total
* @param int $places
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function unsignedFloat($column, $total = 8, $places = 2)
{
return $this->float($column, $total, $places, true);
}

/**
* Create a new unsigned double column on the table.
*
* @param string $column
* @param int $total
* @param int $places
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function unsignedDouble($column, $total = null, $places = null)
{
return $this->double($column, $total, $places, true);
}

/**
* Create a new unsigned decimal column on the table.
*
* @param string $column
* @param int $total
* @param int $places
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function unsignedDecimal($column, $total = 8, $places = 2)
public function decimal($column, $total = 8, $places = 2)
{
return $this->decimal($column, $total, $places, true);
return $this->addColumn('decimal', $column, compact('total', 'places'));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,11 @@ protected function typeSmallInteger(Fluent $column)
*/
protected function typeFloat(Fluent $column)
{
return $this->typeDouble($column);
if ($column->precision) {
return "float({$column->precision})";
}

return 'float';
}

/**
Expand All @@ -727,10 +731,6 @@ protected function typeFloat(Fluent $column)
*/
protected function typeDouble(Fluent $column)
{
if ($column->total && $column->places) {
return "double({$column->total}, {$column->places})";
}

return 'double';
}

Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,11 @@ protected function typeSmallInteger(Fluent $column)
*/
protected function typeFloat(Fluent $column)
{
return $this->typeDouble($column);
if ($column->precision) {
return "float({$column->precision})";
}

return 'float';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ protected function typeFloat(Fluent $column)
*/
protected function typeDouble(Fluent $column)
{
return 'float';
return 'double';
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,10 @@ protected function typeSmallInteger(Fluent $column)
*/
protected function typeFloat(Fluent $column)
{
if ($column->precision) {
return "float({$column->precision})";
}

return 'float';
}

Expand All @@ -664,7 +668,7 @@ protected function typeFloat(Fluent $column)
*/
protected function typeDouble(Fluent $column)
{
return 'float';
return 'double precision';
}

/**
Expand Down
16 changes: 3 additions & 13 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,11 @@ public function testAddingTinyInteger()
public function testAddingFloat()
{
$blueprint = new Blueprint('users');
$blueprint->float('foo', 5, 2);
$blueprint->float('foo', 5);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `foo` double(5, 2) not null', $statements[0]);
$this->assertSame('alter table `users` add `foo` float(5) not null', $statements[0]);
}

public function testAddingDouble()
Expand All @@ -756,17 +756,7 @@ public function testAddingDouble()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `foo` double(15, 6) not null', $statements[0]);
}

public function testAddingDoubleSpecifyingPrecision()
{
$blueprint = new Blueprint('users');
$blueprint->double('foo', 15, 8);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `foo` double(15, 8) not null', $statements[0]);
$this->assertSame('alter table `users` add `foo` double not null', $statements[0]);
}

public function testAddingDecimal()
Expand Down
6 changes: 3 additions & 3 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,17 +605,17 @@ public function testAddingSmallInteger()
public function testAddingFloat()
{
$blueprint = new Blueprint('users');
$blueprint->float('foo', 5, 2);
$blueprint->float('foo', 5);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" double precision not null', $statements[0]);
$this->assertSame('alter table "users" add column "foo" float(5) not null', $statements[0]);
}

public function testAddingDouble()
{
$blueprint = new Blueprint('users');
$blueprint->double('foo', 15, 8);
$blueprint->double('foo');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
Expand Down
6 changes: 3 additions & 3 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public function testAddingSmallInteger()
public function testAddingFloat()
{
$blueprint = new Blueprint('users');
$blueprint->float('foo', 5, 2);
$blueprint->float('foo', 5);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
Expand All @@ -473,11 +473,11 @@ public function testAddingFloat()
public function testAddingDouble()
{
$blueprint = new Blueprint('users');
$blueprint->double('foo', 15, 8);
$blueprint->double('foo');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" float not null', $statements[0]);
$this->assertSame('alter table "users" add column "foo" double not null', $statements[0]);
}

public function testAddingDecimal()
Expand Down
12 changes: 0 additions & 12 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,6 @@ public function testDefaultCurrentTimestamp()
$this->assertEquals(['alter table "users" add "created" datetime not null default CURRENT_TIMESTAMP'], $blueprint->toSql($connection, new SqlServerGrammar));
}

public function testUnsignedDecimalTable()
{
$base = new Blueprint('users', function ($table) {
$table->unsignedDecimal('money', 10, 2)->useCurrent();
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;
$this->assertEquals(['alter table `users` add `money` decimal(10, 2) unsigned not null'], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testRemoveColumn()
{
$base = new Blueprint('users', function ($table) {
Expand Down
8 changes: 4 additions & 4 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,21 +509,21 @@ public function testAddingSmallInteger()
public function testAddingFloat()
{
$blueprint = new Blueprint('users');
$blueprint->float('foo', 5, 2);
$blueprint->float('foo', 5);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add "foo" float not null', $statements[0]);
$this->assertSame('alter table "users" add "foo" float(5) not null', $statements[0]);
}

public function testAddingDouble()
{
$blueprint = new Blueprint('users');
$blueprint->double('foo', 15, 2);
$blueprint->double('foo');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add "foo" float not null', $statements[0]);
$this->assertSame('alter table "users" add "foo" double precision not null', $statements[0]);
}

public function testAddingDecimal()
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testNativeColumnModifyingOnMySql()
$schema->useNativeSchemaOperationsIfPossible();

$blueprint = new Blueprint('users', function ($table) {
$table->double('amount', 6, 2)->nullable()->invisible()->after('name')->change();
$table->double('amount')->nullable()->invisible()->after('name')->change();
$table->timestamp('added_at', 4)->nullable(false)->useCurrent()->useCurrentOnUpdate()->change();
$table->enum('difficulty', ['easy', 'hard'])->default('easy')->charset('utf8mb4')->collation('unicode')->change();
$table->multiPolygon('positions')->srid(1234)->storedAs('expression')->change();
Expand All @@ -133,7 +133,7 @@ public function testNativeColumnModifyingOnMySql()

$this->assertEquals([
'alter table `users` '
.'modify `amount` double(6, 2) null invisible after `name`, '
.'modify `amount` double null invisible after `name`, '
.'modify `added_at` timestamp(4) not null default CURRENT_TIMESTAMP(4) on update CURRENT_TIMESTAMP(4), '
."modify `difficulty` enum('easy', 'hard') character set utf8mb4 collate 'unicode' not null default 'easy', "
.'modify `positions` multipolygon as (expression) stored srid 1234, '
Expand Down