Skip to content

Commit f29df47

Browse files
committed
feat: use database default datetime precision
1 parent c50ee30 commit f29df47

11 files changed

+1749
-1895
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ class Blueprint
1616
{
1717
use Macroable;
1818

19+
/**
20+
* The database connection instance.
21+
*/
22+
protected Connection $connection;
23+
24+
/**
25+
* The schema grammar instance.
26+
*/
27+
protected Grammar $grammar;
28+
1929
/**
2030
* The table the blueprint describes.
2131
*
@@ -94,8 +104,10 @@ class Blueprint
94104
* @param string $prefix
95105
* @return void
96106
*/
97-
public function __construct($table, ?Closure $callback = null, $prefix = '')
107+
public function __construct(Connection $connection, $table, ?Closure $callback = null, $prefix = '')
98108
{
109+
$this->connection = $connection;
110+
$this->grammar = $connection->getSchemaGrammar();
99111
$this->table = $table;
100112
$this->prefix = $prefix;
101113

@@ -107,34 +119,30 @@ public function __construct($table, ?Closure $callback = null, $prefix = '')
107119
/**
108120
* Execute the blueprint against the database.
109121
*
110-
* @param \Illuminate\Database\Connection $connection
111-
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
112122
* @return void
113123
*/
114-
public function build(Connection $connection, Grammar $grammar)
124+
public function build()
115125
{
116-
foreach ($this->toSql($connection, $grammar) as $statement) {
117-
$connection->statement($statement);
126+
foreach ($this->toSql() as $statement) {
127+
$this->connection->statement($statement);
118128
}
119129
}
120130

121131
/**
122132
* Get the raw SQL statements for the blueprint.
123133
*
124-
* @param \Illuminate\Database\Connection $connection
125-
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
126134
* @return array
127135
*/
128-
public function toSql(Connection $connection, Grammar $grammar)
136+
public function toSql()
129137
{
130-
$this->addImpliedCommands($connection, $grammar);
138+
$this->addImpliedCommands();
131139

132140
$statements = [];
133141

134142
// Each type of command has a corresponding compiler function on the schema
135143
// grammar which is used to build the necessary SQL statements to build
136144
// the blueprint element, so we'll just call that compilers function.
137-
$this->ensureCommandsAreValid($connection);
145+
$this->ensureCommandsAreValid();
138146

139147
foreach ($this->commands as $command) {
140148
if ($command->shouldBeSkipped) {
@@ -143,12 +151,12 @@ public function toSql(Connection $connection, Grammar $grammar)
143151

144152
$method = 'compile'.ucfirst($command->name);
145153

146-
if (method_exists($grammar, $method) || $grammar::hasMacro($method)) {
154+
if (method_exists($this->grammar, $method) || $this->grammar::hasMacro($method)) {
147155
if ($this->hasState()) {
148156
$this->state->update($command);
149157
}
150158

151-
if (! is_null($sql = $grammar->$method($this, $command, $connection))) {
159+
if (! is_null($sql = $this->grammar->$method($this, $command, $this->connection))) {
152160
$statements = array_merge($statements, (array) $sql);
153161
}
154162
}
@@ -160,12 +168,11 @@ public function toSql(Connection $connection, Grammar $grammar)
160168
/**
161169
* Ensure the commands on the blueprint are valid for the connection type.
162170
*
163-
* @param \Illuminate\Database\Connection $connection
164171
* @return void
165172
*
166173
* @throws \BadMethodCallException
167174
*/
168-
protected function ensureCommandsAreValid(Connection $connection)
175+
protected function ensureCommandsAreValid()
169176
{
170177
//
171178
}
@@ -188,15 +195,12 @@ protected function commandsNamed(array $names)
188195
/**
189196
* Add the commands that are implied by the blueprint's state.
190197
*
191-
* @param \Illuminate\Database\Connection $connection
192-
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
193198
* @return void
194199
*/
195-
protected function addImpliedCommands(Connection $connection, Grammar $grammar)
200+
protected function addImpliedCommands()
196201
{
197-
$this->addFluentIndexes($connection, $grammar);
198-
199-
$this->addFluentCommands($connection, $grammar);
202+
$this->addFluentIndexes();
203+
$this->addFluentCommands();
200204

201205
if (! $this->creating()) {
202206
$this->commands = array_map(
@@ -206,25 +210,23 @@ protected function addImpliedCommands(Connection $connection, Grammar $grammar)
206210
$this->commands
207211
);
208212

209-
$this->addAlterCommands($connection, $grammar);
213+
$this->addAlterCommands();
210214
}
211215
}
212216

213217
/**
214218
* Add the index commands fluently specified on columns.
215219
*
216-
* @param \Illuminate\Database\Connection $connection
217-
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
218220
* @return void
219221
*/
220-
protected function addFluentIndexes(Connection $connection, Grammar $grammar)
222+
protected function addFluentIndexes()
221223
{
222224
foreach ($this->columns as $column) {
223225
foreach (['primary', 'unique', 'index', 'fulltext', 'fullText', 'spatialIndex'] as $index) {
224226
// If the column is supposed to be changed to an auto increment column and
225227
// the specified index is primary, there is no need to add a command on
226228
// MySQL, as it will be handled during the column definition instead.
227-
if ($index === 'primary' && $column->autoIncrement && $column->change && $grammar instanceof MySqlGrammar) {
229+
if ($index === 'primary' && $column->autoIncrement && $column->change && $this->grammar instanceof MySqlGrammar) {
228230
continue 2;
229231
}
230232

@@ -264,14 +266,12 @@ protected function addFluentIndexes(Connection $connection, Grammar $grammar)
264266
/**
265267
* Add the fluent commands specified on any columns.
266268
*
267-
* @param \Illuminate\Database\Connection $connection
268-
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
269269
* @return void
270270
*/
271-
public function addFluentCommands(Connection $connection, Grammar $grammar)
271+
public function addFluentCommands()
272272
{
273273
foreach ($this->columns as $column) {
274-
foreach ($grammar->getFluentCommands() as $commandName) {
274+
foreach ($this->grammar->getFluentCommands() as $commandName) {
275275
$this->addCommand($commandName, compact('column'));
276276
}
277277
}
@@ -280,17 +280,15 @@ public function addFluentCommands(Connection $connection, Grammar $grammar)
280280
/**
281281
* Add the alter commands if whenever needed.
282282
*
283-
* @param \Illuminate\Database\Connection $connection
284-
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
285283
* @return void
286284
*/
287-
public function addAlterCommands(Connection $connection, Grammar $grammar)
285+
public function addAlterCommands()
288286
{
289-
if (! $grammar instanceof SQLiteGrammar) {
287+
if (! $this->grammar instanceof SQLiteGrammar) {
290288
return;
291289
}
292290

293-
$alterCommands = $grammar->getAlterCommands($connection);
291+
$alterCommands = $this->grammar->getAlterCommands($this->connection);
294292

295293
[$commands, $lastCommandWasAlter, $hasAlterCommand] = [
296294
[], false, false,
@@ -313,7 +311,7 @@ public function addAlterCommands(Connection $connection, Grammar $grammar)
313311
}
314312

315313
if ($hasAlterCommand) {
316-
$this->state = new BlueprintState($this, $connection, $grammar);
314+
$this->state = new BlueprintState($this, $this->connection, $this->grammar);
317315
}
318316

319317
$this->commands = $commands;
@@ -1162,8 +1160,10 @@ public function date($column)
11621160
* @param int|null $precision
11631161
* @return \Illuminate\Database\Schema\ColumnDefinition
11641162
*/
1165-
public function dateTime($column, $precision = 0)
1163+
public function dateTime($column, $precision = null)
11661164
{
1165+
$precision ??= $this->defaultTimePrecision();
1166+
11671167
return $this->addColumn('dateTime', $column, compact('precision'));
11681168
}
11691169

@@ -1174,8 +1174,10 @@ public function dateTime($column, $precision = 0)
11741174
* @param int|null $precision
11751175
* @return \Illuminate\Database\Schema\ColumnDefinition
11761176
*/
1177-
public function dateTimeTz($column, $precision = 0)
1177+
public function dateTimeTz($column, $precision = null)
11781178
{
1179+
$precision ??= $this->defaultTimePrecision();
1180+
11791181
return $this->addColumn('dateTimeTz', $column, compact('precision'));
11801182
}
11811183

@@ -1186,8 +1188,10 @@ public function dateTimeTz($column, $precision = 0)
11861188
* @param int|null $precision
11871189
* @return \Illuminate\Database\Schema\ColumnDefinition
11881190
*/
1189-
public function time($column, $precision = 0)
1191+
public function time($column, $precision = null)
11901192
{
1193+
$precision ??= $this->defaultTimePrecision();
1194+
11911195
return $this->addColumn('time', $column, compact('precision'));
11921196
}
11931197

@@ -1198,8 +1202,10 @@ public function time($column, $precision = 0)
11981202
* @param int|null $precision
11991203
* @return \Illuminate\Database\Schema\ColumnDefinition
12001204
*/
1201-
public function timeTz($column, $precision = 0)
1205+
public function timeTz($column, $precision = null)
12021206
{
1207+
$precision ??= $this->defaultTimePrecision();
1208+
12031209
return $this->addColumn('timeTz', $column, compact('precision'));
12041210
}
12051211

@@ -1210,8 +1216,10 @@ public function timeTz($column, $precision = 0)
12101216
* @param int|null $precision
12111217
* @return \Illuminate\Database\Schema\ColumnDefinition
12121218
*/
1213-
public function timestamp($column, $precision = 0)
1219+
public function timestamp($column, $precision = null)
12141220
{
1221+
$precision ??= $this->defaultTimePrecision();
1222+
12151223
return $this->addColumn('timestamp', $column, compact('precision'));
12161224
}
12171225

@@ -1222,8 +1230,10 @@ public function timestamp($column, $precision = 0)
12221230
* @param int|null $precision
12231231
* @return \Illuminate\Database\Schema\ColumnDefinition
12241232
*/
1225-
public function timestampTz($column, $precision = 0)
1233+
public function timestampTz($column, $precision = null)
12261234
{
1235+
$precision ??= $this->defaultTimePrecision();
1236+
12271237
return $this->addColumn('timestampTz', $column, compact('precision'));
12281238
}
12291239

@@ -1233,7 +1243,7 @@ public function timestampTz($column, $precision = 0)
12331243
* @param int|null $precision
12341244
* @return void
12351245
*/
1236-
public function timestamps($precision = 0)
1246+
public function timestamps($precision = null)
12371247
{
12381248
$this->timestamp('created_at', $precision)->nullable();
12391249

@@ -1248,7 +1258,7 @@ public function timestamps($precision = 0)
12481258
* @param int|null $precision
12491259
* @return void
12501260
*/
1251-
public function nullableTimestamps($precision = 0)
1261+
public function nullableTimestamps($precision = null)
12521262
{
12531263
$this->timestamps($precision);
12541264
}
@@ -1259,7 +1269,7 @@ public function nullableTimestamps($precision = 0)
12591269
* @param int|null $precision
12601270
* @return void
12611271
*/
1262-
public function timestampsTz($precision = 0)
1272+
public function timestampsTz($precision = null)
12631273
{
12641274
$this->timestampTz('created_at', $precision)->nullable();
12651275

@@ -1272,7 +1282,7 @@ public function timestampsTz($precision = 0)
12721282
* @param int|null $precision
12731283
* @return void
12741284
*/
1275-
public function datetimes($precision = 0)
1285+
public function datetimes($precision = null)
12761286
{
12771287
$this->datetime('created_at', $precision)->nullable();
12781288

@@ -1286,7 +1296,7 @@ public function datetimes($precision = 0)
12861296
* @param int|null $precision
12871297
* @return \Illuminate\Database\Schema\ColumnDefinition
12881298
*/
1289-
public function softDeletes($column = 'deleted_at', $precision = 0)
1299+
public function softDeletes($column = 'deleted_at', $precision = null)
12901300
{
12911301
return $this->timestamp($column, $precision)->nullable();
12921302
}
@@ -1298,7 +1308,7 @@ public function softDeletes($column = 'deleted_at', $precision = 0)
12981308
* @param int|null $precision
12991309
* @return \Illuminate\Database\Schema\ColumnDefinition
13001310
*/
1301-
public function softDeletesTz($column = 'deleted_at', $precision = 0)
1311+
public function softDeletesTz($column = 'deleted_at', $precision = null)
13021312
{
13031313
return $this->timestampTz($column, $precision)->nullable();
13041314
}
@@ -1310,7 +1320,7 @@ public function softDeletesTz($column = 'deleted_at', $precision = 0)
13101320
* @param int|null $precision
13111321
* @return \Illuminate\Database\Schema\ColumnDefinition
13121322
*/
1313-
public function softDeletesDatetime($column = 'deleted_at', $precision = 0)
1323+
public function softDeletesDatetime($column = 'deleted_at', $precision = null)
13141324
{
13151325
return $this->datetime($column, $precision)->nullable();
13161326
}
@@ -1853,4 +1863,12 @@ public function getChangedColumns()
18531863
return (bool) $column->change;
18541864
});
18551865
}
1866+
1867+
/**
1868+
* Get the default time precision.
1869+
*/
1870+
protected function defaultTimePrecision(): ?int
1871+
{
1872+
return $this->connection->getSchemaBuilder()::$defaultTimePrecision;
1873+
}
18561874
}

src/Illuminate/Database/Schema/Builder.php

Lines changed: 19 additions & 6 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
*
@@ -561,7 +574,7 @@ public function withoutForeignKeyConstraints(Closure $callback)
561574
*/
562575
protected function build(Blueprint $blueprint)
563576
{
564-
$blueprint->build($this->connection, $this->grammar);
577+
$blueprint->build();
565578
}
566579

567580
/**
@@ -573,15 +586,15 @@ protected function build(Blueprint $blueprint)
573586
*/
574587
protected function createBlueprint($table, ?Closure $callback = null)
575588
{
576-
$prefix = $this->connection->getConfig('prefix_indexes')
577-
? $this->connection->getConfig('prefix')
578-
: '';
589+
$connection = $this->connection;
590+
591+
$prefix = $connection->getConfig('prefix_indexes') ? $connection->getConfig('prefix') : '';
579592

580593
if (isset($this->resolver)) {
581-
return call_user_func($this->resolver, $table, $callback, $prefix);
594+
return call_user_func($this->resolver, $connection, $table, $callback, $prefix);
582595
}
583596

584-
return Container::getInstance()->make(Blueprint::class, compact('table', 'callback', 'prefix'));
597+
return Container::getInstance()->make(Blueprint::class, compact('connection', 'table', 'callback', 'prefix'));
585598
}
586599

587600
/**

0 commit comments

Comments
 (0)