Skip to content

Commit ffabb41

Browse files
committed
feat: move default time precision to schema builder
1 parent 9cd5f5f commit ffabb41

9 files changed

+104
-41
lines changed

src/Illuminate/Database/Grammar.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,6 @@ public function getDateFormat()
277277
return 'Y-m-d H:i:s';
278278
}
279279

280-
/**
281-
* Get the precision for storing datetimes (null uses the database default).
282-
*/
283-
public function getDatetimePrecision(): ?int
284-
{
285-
return 0;
286-
}
287-
288280
/**
289281
* Get the grammar's table prefix.
290282
*

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ public function date($column)
11061106
*/
11071107
public function dateTime($column, $precision = null)
11081108
{
1109-
$precision ??= $this->defaultDatetimePrecision();
1109+
$precision ??= $this->defaultTimePrecision();
11101110

11111111
return $this->addColumn('dateTime', $column, compact('precision'));
11121112
}
@@ -1120,7 +1120,7 @@ public function dateTime($column, $precision = null)
11201120
*/
11211121
public function dateTimeTz($column, $precision = null)
11221122
{
1123-
$precision ??= $this->defaultDatetimePrecision();
1123+
$precision ??= $this->defaultTimePrecision();
11241124

11251125
return $this->addColumn('dateTimeTz', $column, compact('precision'));
11261126
}
@@ -1134,7 +1134,7 @@ public function dateTimeTz($column, $precision = null)
11341134
*/
11351135
public function time($column, $precision = null)
11361136
{
1137-
$precision ??= $this->defaultDatetimePrecision();
1137+
$precision ??= $this->defaultTimePrecision();
11381138

11391139
return $this->addColumn('time', $column, compact('precision'));
11401140
}
@@ -1148,7 +1148,7 @@ public function time($column, $precision = null)
11481148
*/
11491149
public function timeTz($column, $precision = null)
11501150
{
1151-
$precision ??= $this->defaultDatetimePrecision();
1151+
$precision ??= $this->defaultTimePrecision();
11521152

11531153
return $this->addColumn('timeTz', $column, compact('precision'));
11541154
}
@@ -1162,7 +1162,7 @@ public function timeTz($column, $precision = null)
11621162
*/
11631163
public function timestamp($column, $precision = null)
11641164
{
1165-
$precision ??= $this->defaultDatetimePrecision();
1165+
$precision ??= $this->defaultTimePrecision();
11661166

11671167
return $this->addColumn('timestamp', $column, compact('precision'));
11681168
}
@@ -1176,7 +1176,7 @@ public function timestamp($column, $precision = null)
11761176
*/
11771177
public function timestampTz($column, $precision = null)
11781178
{
1179-
$precision ??= $this->defaultDatetimePrecision();
1179+
$precision ??= $this->defaultTimePrecision();
11801180

11811181
return $this->addColumn('timestampTz', $column, compact('precision'));
11821182
}
@@ -1778,10 +1778,10 @@ public function getChangedColumns()
17781778
}
17791779

17801780
/**
1781-
* Get the default datetime precision.
1781+
* Get the default time precision.
17821782
*/
1783-
protected function defaultDatetimePrecision(): ?int
1783+
protected function defaultTimePrecision(): ?int
17841784
{
1785-
return $this->grammar->getDatetimePrecision();
1785+
return $this->connection->getSchemaBuilder()::$defaultTimePrecision;
17861786
}
17871787
}

src/Illuminate/Database/Schema/Builder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class Builder
4141
*/
4242
public static $defaultStringLength = 255;
4343

44+
/**
45+
* The default time precision for migrations.
46+
*/
47+
public static ?int $defaultTimePrecision = 0;
48+
4449
/**
4550
* The default relationship morph key type.
4651
*
@@ -71,6 +76,14 @@ public static function defaultStringLength($length)
7176
static::$defaultStringLength = $length;
7277
}
7378

79+
/**
80+
* Set the default time precision for migrations.
81+
*/
82+
public static function defaultTimePrecision(?int $precision): void
83+
{
84+
static::$defaultTimePrecision = $precision;
85+
}
86+
7487
/**
7588
* Set the default morph key type for migrations.
7689
*

tests/Database/DatabaseMariaDbSchemaGrammarTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Schema\Blueprint;
88
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
99
use Illuminate\Database\Schema\Grammars\MariaDbGrammar;
10+
use Illuminate\Database\Schema\MariaDbBuilder;
1011
use Illuminate\Tests\Database\Fixtures\Enums\Foo;
1112
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
@@ -1456,17 +1457,26 @@ public function testGrammarsAreMacroable()
14561457
$this->assertTrue($c);
14571458
}
14581459

1459-
protected function getConnection(MariaDbGrammar $grammar = null)
1460-
{
1460+
protected function getConnection(
1461+
?MariaDbGrammar $grammar = null,
1462+
?MariaDbBuilder $builder = null,
1463+
) {
14611464
$grammar ??= $this->getGrammar();
1465+
$builder ??= $this->getBuilder();
14621466

14631467
return m::mock(Connection::class)
14641468
->shouldReceive('getSchemaGrammar')->andReturn($grammar)
1469+
->shouldReceive('getSchemaBuilder')->andReturn($builder)
14651470
->getMock();
14661471
}
14671472

14681473
public function getGrammar()
14691474
{
14701475
return new MariaDbGrammar;
14711476
}
1477+
1478+
public function getBuilder()
1479+
{
1480+
return mock(MariaDbBuilder::class);
1481+
}
14721482
}

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Schema\Blueprint;
88
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
99
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
10+
use Illuminate\Database\Schema\MySqlBuilder;
1011
use Illuminate\Tests\Database\Fixtures\Enums\Foo;
1112
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
@@ -1456,17 +1457,26 @@ public function testGrammarsAreMacroable()
14561457
$this->assertTrue($c);
14571458
}
14581459

1459-
protected function getConnection(MySqlGrammar $grammar = null)
1460-
{
1460+
protected function getConnection(
1461+
?MySqlGrammar $grammar = null,
1462+
?MySqlBuilder $builder = null,
1463+
) {
14611464
$grammar ??= $this->getGrammar();
1465+
$builder ??= $this->getBuilder();
14621466

14631467
return m::mock(Connection::class)
14641468
->shouldReceive('getSchemaGrammar')->andReturn($grammar)
1469+
->shouldReceive('getSchemaBuilder')->andReturn($builder)
14651470
->getMock();
14661471
}
14671472

14681473
public function getGrammar()
14691474
{
14701475
return new MySqlGrammar;
14711476
}
1477+
1478+
public function getBuilder()
1479+
{
1480+
return mock(MySqlBuilder::class);
1481+
}
14721482
}

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Schema\Builder;
99
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
1010
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
11+
use Illuminate\Database\Schema\PostgresBuilder;
1112
use Mockery as m;
1213
use PHPUnit\Framework\Attributes\DataProvider;
1314
use PHPUnit\Framework\Attributes\TestWith;
@@ -697,8 +698,8 @@ public function testAddingJsonb()
697698
#[DataProvider('datetimeAndPrecisionProvider')]
698699
public function testAddingDatetimeMethods(string $method, string $type, ?int $userPrecision, false|int|null $grammarPrecision, ?int $expected)
699700
{
700-
$grammar = $this->getGrammar($grammarPrecision);
701-
$blueprint = new Blueprint($this->getConnection($grammar), 'users');
701+
PostgresBuilder::defaultTimePrecision($grammarPrecision);
702+
$blueprint = new Blueprint($this->getConnection(), 'users');
702703
$blueprint->{$method}('created_at', $userPrecision);
703704
$statements = $blueprint->toSql();
704705
$type = is_null($expected) ? $type : "{$type}({$expected})";
@@ -711,6 +712,7 @@ public function testAddingDatetimeMethods(string $method, string $type, ?int $us
711712
#[TestWith(['timestampsTz'])]
712713
public function testAddingTimestamps(string $method)
713714
{
715+
PostgresBuilder::defaultTimePrecision(0);
714716
$blueprint = new Blueprint($this->getConnection(), 'users');
715717
$blueprint->{$method}();
716718
$statements = $blueprint->toSql();
@@ -1074,26 +1076,27 @@ public function testCompileColumns()
10741076
$this->assertStringContainsString("where c.relname = 'table' and n.nspname = 'public'", $statement);
10751077
}
10761078

1077-
protected function getConnection(PostgresGrammar $grammar = null)
1078-
{
1079+
protected function getConnection(
1080+
?PostgresGrammar $grammar = null,
1081+
?PostgresBuilder $builder = null
1082+
) {
10791083
$grammar ??= $this->getGrammar();
1084+
$builder ??= $this->getBuilder();
10801085

10811086
return m::mock(Connection::class)
10821087
->shouldReceive('getSchemaGrammar')->andReturn($grammar)
1088+
->shouldReceive('getSchemaBuilder')->andReturn($builder)
10831089
->getMock();
10841090
}
10851091

1086-
public function getGrammar(false|int|null $precision = false)
1092+
public function getGrammar()
10871093
{
1088-
if ($precision === false) {
1089-
return new PostgresGrammar;
1090-
}
1094+
return new PostgresGrammar;
1095+
}
10911096

1092-
return m::mock(PostgresGrammar::class)
1093-
->makePartial()
1094-
->shouldReceive('getDatetimePrecision')
1095-
->andReturn($precision)
1096-
->getMock();
1097+
public function getBuilder()
1098+
{
1099+
return mock(PostgresBuilder::class);
10971100
}
10981101

10991102
/** @return list<array{method: string, type: string, user: int|null, grammar: false|int|null, expected: int|null}> */

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Schema\Blueprint;
99
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
1010
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
11+
use Illuminate\Database\Schema\SQLiteBuilder;
1112
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
1314
use RuntimeException;
@@ -925,17 +926,26 @@ public function testCreateTableWithStoredAsColumn()
925926
$this->assertSame('create table "users" ("my_json_column" varchar not null, "my_other_column" varchar as (json_extract("my_json_column", \'$."some_attribute"."nested"\')) stored)', $statements[0]);
926927
}
927928

928-
protected function getConnection(SQLiteGrammar $grammar = null)
929-
{
929+
protected function getConnection(
930+
?SQLiteGrammar $grammar = null,
931+
?SQLiteBuilder $builder = null,
932+
) {
930933
$grammar ??= $this->getGrammar();
934+
$builder ??= $this->getBuilder();
931935

932936
return m::mock(Connection::class)
933937
->shouldReceive('getSchemaGrammar')->andReturn($grammar)
938+
->shouldReceive('getSchemaBuilder')->andReturn($builder)
934939
->getMock();
935940
}
936941

937942
public function getGrammar()
938943
{
939944
return new SQLiteGrammar;
940945
}
946+
947+
public function getBuilder()
948+
{
949+
return mock(SQLiteBuilder::class);
950+
}
941951
}

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
1313
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
1414
use Illuminate\Database\Schema\Grammars\SqlServerGrammar;
15+
use Illuminate\Database\Schema\MariaDbBuilder;
16+
use Illuminate\Database\Schema\MySqlBuilder;
17+
use Illuminate\Database\Schema\PostgresBuilder;
18+
use Illuminate\Database\Schema\SQLiteBuilder;
19+
use Illuminate\Database\Schema\SqlServerBuilder;
1520
use Mockery as m;
1621
use PHPUnit\Framework\TestCase;
1722

@@ -170,7 +175,7 @@ public function testNativeRenameColumnOnMysql57()
170175
$connection = $this->getConnection(new MySqlGrammar);
171176
$connection->shouldReceive('isMaria')->andReturn(false);
172177
$connection->shouldReceive('getServerVersion')->andReturn('5.7');
173-
$connection->shouldReceive('getSchemaBuilder->getColumns')->andReturn([
178+
$connection->getSchemaBuilder()->shouldReceive('getColumns')->andReturn([
174179
['name' => 'name', 'type' => 'varchar(255)', 'type_name' => 'varchar', 'nullable' => true, 'collation' => 'utf8mb4_unicode_ci', 'default' => 'foo', 'comment' => null, 'auto_increment' => false, 'generation' => null],
175180
['name' => 'id', 'type' => 'bigint unsigned', 'type_name' => 'bigint', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => 'lorem ipsum', 'auto_increment' => true, 'generation' => null],
176181
['name' => 'generated', 'type' => 'int', 'type_name' => 'int', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => null, 'auto_increment' => false, 'generation' => ['type' => 'stored', 'expression' => 'expression']],
@@ -194,7 +199,7 @@ public function testNativeRenameColumnOnLegacyMariaDB()
194199
$connection = $this->getConnection(new MariaDbGrammar);
195200
$connection->shouldReceive('isMaria')->andReturn(true);
196201
$connection->shouldReceive('getServerVersion')->andReturn('10.1.35');
197-
$connection->shouldReceive('getSchemaBuilder->getColumns')->andReturn([
202+
$connection->getSchemaBuilder()->shouldReceive('getColumns')->andReturn([
198203
['name' => 'name', 'type' => 'varchar(255)', 'type_name' => 'varchar', 'nullable' => true, 'collation' => 'utf8mb4_unicode_ci', 'default' => 'foo', 'comment' => null, 'auto_increment' => false, 'generation' => null],
199204
['name' => 'id', 'type' => 'bigint unsigned', 'type_name' => 'bigint', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => 'lorem ipsum', 'auto_increment' => true, 'generation' => null],
200205
['name' => 'generated', 'type' => 'int', 'type_name' => 'int', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => null, 'auto_increment' => false, 'generation' => ['type' => 'stored', 'expression' => 'expression']],
@@ -484,12 +489,22 @@ public function testTableComment()
484489
$this->assertEquals(['comment on table "posts" is \'Look at my comment, it is amazing\''], $getSql(new PostgresGrammar));
485490
}
486491

487-
protected function getConnection(Grammar $grammar = null)
492+
protected function getConnection(?Grammar $grammar = null)
488493
{
489494
$grammar ??= new MySqlGrammar;
490495

496+
$builder = mock(match ($grammar::class) {
497+
MySqlGrammar::class => MySqlBuilder::class,
498+
PostgresGrammar::class => PostgresBuilder::class,
499+
SQLiteGrammar::class => SQLiteBuilder::class,
500+
SqlServerGrammar::class => SqlServerBuilder::class,
501+
MariaDbGrammar::class => MariaDbBuilder::class,
502+
default => Builder::class,
503+
});
504+
491505
return m::mock(Connection::class)
492506
->shouldReceive('getSchemaGrammar')->andReturn($grammar)
507+
->shouldReceive('getSchemaBuilder')->andReturn($builder)
493508
->getMock();
494509
}
495510

tests/Database/DatabaseSqlServerSchemaGrammarTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Schema\Blueprint;
88
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
99
use Illuminate\Database\Schema\Grammars\SqlServerGrammar;
10+
use Illuminate\Database\Schema\SqlServerBuilder;
1011
use Mockery as m;
1112
use PHPUnit\Framework\TestCase;
1213

@@ -923,17 +924,26 @@ public function testDropDatabaseIfExists()
923924
);
924925
}
925926

926-
protected function getConnection(SqlServerGrammar $grammar = null)
927-
{
927+
protected function getConnection(
928+
?SqlServerGrammar $grammar = null,
929+
?SqlServerBuilder $builder = null
930+
) {
928931
$grammar ??= $this->getGrammar();
932+
$builder ??= $this->getBuilder();
929933

930934
return m::mock(Connection::class)
931935
->shouldReceive('getSchemaGrammar')->andReturn($grammar)
936+
->shouldReceive('getSchemaBuilder')->andReturn($builder)
932937
->getMock();
933938
}
934939

935940
public function getGrammar()
936941
{
937942
return new SqlServerGrammar;
938943
}
944+
945+
public function getBuilder()
946+
{
947+
return mock(SqlServerBuilder::class);
948+
}
939949
}

0 commit comments

Comments
 (0)