Skip to content

Commit 4c1aa68

Browse files
[11.x] Make floating-types consistent (#48861)
* make floating-types consistent * fix tests * fix tests * fix tests
1 parent a111ada commit 4c1aa68

11 files changed

+38
-97
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -980,28 +980,23 @@ public function foreignIdFor($model, $column = null)
980980
* Create a new float column on the table.
981981
*
982982
* @param string $column
983-
* @param int $total
984-
* @param int $places
985-
* @param bool $unsigned
983+
* @param int $precision
986984
* @return \Illuminate\Database\Schema\ColumnDefinition
987985
*/
988-
public function float($column, $total = 8, $places = 2, $unsigned = false)
986+
public function float($column, $precision = 53)
989987
{
990-
return $this->addColumn('float', $column, compact('total', 'places', 'unsigned'));
988+
return $this->addColumn('float', $column, compact('precision'));
991989
}
992990

993991
/**
994992
* Create a new double column on the table.
995993
*
996994
* @param string $column
997-
* @param int|null $total
998-
* @param int|null $places
999-
* @param bool $unsigned
1000995
* @return \Illuminate\Database\Schema\ColumnDefinition
1001996
*/
1002-
public function double($column, $total = 15, $places = 6, $unsigned = false)
997+
public function double($column)
1003998
{
1004-
return $this->addColumn('double', $column, compact('total', 'places', 'unsigned'));
999+
return $this->addColumn('double', $column);
10051000
}
10061001

10071002
/**
@@ -1010,51 +1005,11 @@ public function double($column, $total = 15, $places = 6, $unsigned = false)
10101005
* @param string $column
10111006
* @param int $total
10121007
* @param int $places
1013-
* @param bool $unsigned
1014-
* @return \Illuminate\Database\Schema\ColumnDefinition
1015-
*/
1016-
public function decimal($column, $total = 8, $places = 2, $unsigned = false)
1017-
{
1018-
return $this->addColumn('decimal', $column, compact('total', 'places', 'unsigned'));
1019-
}
1020-
1021-
/**
1022-
* Create a new unsigned float column on the table.
1023-
*
1024-
* @param string $column
1025-
* @param int $total
1026-
* @param int $places
1027-
* @return \Illuminate\Database\Schema\ColumnDefinition
1028-
*/
1029-
public function unsignedFloat($column, $total = 8, $places = 2)
1030-
{
1031-
return $this->float($column, $total, $places, true);
1032-
}
1033-
1034-
/**
1035-
* Create a new unsigned double column on the table.
1036-
*
1037-
* @param string $column
1038-
* @param int $total
1039-
* @param int $places
1040-
* @return \Illuminate\Database\Schema\ColumnDefinition
1041-
*/
1042-
public function unsignedDouble($column, $total = null, $places = null)
1043-
{
1044-
return $this->double($column, $total, $places, true);
1045-
}
1046-
1047-
/**
1048-
* Create a new unsigned decimal column on the table.
1049-
*
1050-
* @param string $column
1051-
* @param int $total
1052-
* @param int $places
10531008
* @return \Illuminate\Database\Schema\ColumnDefinition
10541009
*/
1055-
public function unsignedDecimal($column, $total = 8, $places = 2)
1010+
public function decimal($column, $total = 8, $places = 2)
10561011
{
1057-
return $this->decimal($column, $total, $places, true);
1012+
return $this->addColumn('decimal', $column, compact('total', 'places'));
10581013
}
10591014

10601015
/**

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,11 @@ protected function typeSmallInteger(Fluent $column)
774774
*/
775775
protected function typeFloat(Fluent $column)
776776
{
777-
return $this->typeDouble($column);
777+
if ($column->precision) {
778+
return "float({$column->precision})";
779+
}
780+
781+
return 'float';
778782
}
779783

780784
/**
@@ -785,10 +789,6 @@ protected function typeFloat(Fluent $column)
785789
*/
786790
protected function typeDouble(Fluent $column)
787791
{
788-
if ($column->total && $column->places) {
789-
return "double({$column->total}, {$column->places})";
790-
}
791-
792792
return 'double';
793793
}
794794

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,11 @@ protected function typeSmallInteger(Fluent $column)
809809
*/
810810
protected function typeFloat(Fluent $column)
811811
{
812-
return $this->typeDouble($column);
812+
if ($column->precision) {
813+
return "float({$column->precision})";
814+
}
815+
816+
return 'float';
813817
}
814818

815819
/**

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ protected function typeFloat(Fluent $column)
698698
*/
699699
protected function typeDouble(Fluent $column)
700700
{
701-
return 'float';
701+
return 'double';
702702
}
703703

704704
/**

src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,10 @@ protected function typeSmallInteger(Fluent $column)
708708
*/
709709
protected function typeFloat(Fluent $column)
710710
{
711+
if ($column->precision) {
712+
return "float({$column->precision})";
713+
}
714+
711715
return 'float';
712716
}
713717

@@ -719,7 +723,7 @@ protected function typeFloat(Fluent $column)
719723
*/
720724
protected function typeDouble(Fluent $column)
721725
{
722-
return 'float';
726+
return 'double precision';
723727
}
724728

725729
/**

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -754,11 +754,11 @@ public function testAddingTinyInteger()
754754
public function testAddingFloat()
755755
{
756756
$blueprint = new Blueprint('users');
757-
$blueprint->float('foo', 5, 2);
757+
$blueprint->float('foo', 5);
758758
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
759759

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

764764
public function testAddingDouble()
@@ -768,17 +768,7 @@ public function testAddingDouble()
768768
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
769769

770770
$this->assertCount(1, $statements);
771-
$this->assertSame('alter table `users` add `foo` double(15, 6) not null', $statements[0]);
772-
}
773-
774-
public function testAddingDoubleSpecifyingPrecision()
775-
{
776-
$blueprint = new Blueprint('users');
777-
$blueprint->double('foo', 15, 8);
778-
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
779-
780-
$this->assertCount(1, $statements);
781-
$this->assertSame('alter table `users` add `foo` double(15, 8) not null', $statements[0]);
771+
$this->assertSame('alter table `users` add `foo` double not null', $statements[0]);
782772
}
783773

784774
public function testAddingDecimal()

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,17 @@ public function testAddingSmallInteger()
606606
public function testAddingFloat()
607607
{
608608
$blueprint = new Blueprint('users');
609-
$blueprint->float('foo', 5, 2);
609+
$blueprint->float('foo', 5);
610610
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
611611

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

616616
public function testAddingDouble()
617617
{
618618
$blueprint = new Blueprint('users');
619-
$blueprint->double('foo', 15, 8);
619+
$blueprint->double('foo');
620620
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
621621

622622
$this->assertCount(1, $statements);

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public function testAddingSmallInteger()
465465
public function testAddingFloat()
466466
{
467467
$blueprint = new Blueprint('users');
468-
$blueprint->float('foo', 5, 2);
468+
$blueprint->float('foo', 5);
469469
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
470470

471471
$this->assertCount(1, $statements);
@@ -475,11 +475,11 @@ public function testAddingFloat()
475475
public function testAddingDouble()
476476
{
477477
$blueprint = new Blueprint('users');
478-
$blueprint->double('foo', 15, 8);
478+
$blueprint->double('foo');
479479
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
480480

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

485485
public function testAddingDecimal()

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,6 @@ public function testDefaultCurrentTimestamp()
146146
$this->assertEquals(['alter table "users" add "created" datetime not null default CURRENT_TIMESTAMP'], $blueprint->toSql($connection, new SqlServerGrammar));
147147
}
148148

149-
public function testUnsignedDecimalTable()
150-
{
151-
$base = new Blueprint('users', function ($table) {
152-
$table->unsignedDecimal('money', 10, 2)->useCurrent();
153-
});
154-
155-
$connection = m::mock(Connection::class);
156-
157-
$blueprint = clone $base;
158-
$this->assertEquals(['alter table `users` add `money` decimal(10, 2) unsigned not null'], $blueprint->toSql($connection, new MySqlGrammar));
159-
}
160-
161149
public function testRemoveColumn()
162150
{
163151
$base = new Blueprint('users', function ($table) {

tests/Database/DatabaseSqlServerSchemaGrammarTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,21 +510,21 @@ public function testAddingSmallInteger()
510510
public function testAddingFloat()
511511
{
512512
$blueprint = new Blueprint('users');
513-
$blueprint->float('foo', 5, 2);
513+
$blueprint->float('foo', 5);
514514
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
515515

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

520520
public function testAddingDouble()
521521
{
522522
$blueprint = new Blueprint('users');
523-
$blueprint->double('foo', 15, 2);
523+
$blueprint->double('foo');
524524
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
525525

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

530530
public function testAddingDecimal()

0 commit comments

Comments
 (0)