@@ -16,6 +16,16 @@ class Blueprint
16
16
{
17
17
use Macroable;
18
18
19
+ /**
20
+ * The database connection instance.
21
+ */
22
+ protected Connection $ connection ;
23
+
24
+ /**
25
+ * The schema grammar instance.
26
+ */
27
+ protected Grammar $ grammar ;
28
+
19
29
/**
20
30
* The table the blueprint describes.
21
31
*
@@ -94,8 +104,10 @@ class Blueprint
94
104
* @param string $prefix
95
105
* @return void
96
106
*/
97
- public function __construct ($ table , ?Closure $ callback = null , $ prefix = '' )
107
+ public function __construct (Connection $ connection , $ table , ?Closure $ callback = null , $ prefix = '' )
98
108
{
109
+ $ this ->connection = $ connection ;
110
+ $ this ->grammar = $ connection ->getSchemaGrammar ();
99
111
$ this ->table = $ table ;
100
112
$ this ->prefix = $ prefix ;
101
113
@@ -107,34 +119,30 @@ public function __construct($table, ?Closure $callback = null, $prefix = '')
107
119
/**
108
120
* Execute the blueprint against the database.
109
121
*
110
- * @param \Illuminate\Database\Connection $connection
111
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
112
122
* @return void
113
123
*/
114
- public function build (Connection $ connection , Grammar $ grammar )
124
+ public function build ()
115
125
{
116
- foreach ($ this ->toSql ($ connection , $ grammar ) as $ statement ) {
117
- $ connection ->statement ($ statement );
126
+ foreach ($ this ->toSql () as $ statement ) {
127
+ $ this -> connection ->statement ($ statement );
118
128
}
119
129
}
120
130
121
131
/**
122
132
* Get the raw SQL statements for the blueprint.
123
133
*
124
- * @param \Illuminate\Database\Connection $connection
125
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
126
134
* @return array
127
135
*/
128
- public function toSql (Connection $ connection , Grammar $ grammar )
136
+ public function toSql ()
129
137
{
130
- $ this ->addImpliedCommands ($ connection , $ grammar );
138
+ $ this ->addImpliedCommands ();
131
139
132
140
$ statements = [];
133
141
134
142
// Each type of command has a corresponding compiler function on the schema
135
143
// grammar which is used to build the necessary SQL statements to build
136
144
// the blueprint element, so we'll just call that compilers function.
137
- $ this ->ensureCommandsAreValid ($ connection );
145
+ $ this ->ensureCommandsAreValid ();
138
146
139
147
foreach ($ this ->commands as $ command ) {
140
148
if ($ command ->shouldBeSkipped ) {
@@ -143,12 +151,12 @@ public function toSql(Connection $connection, Grammar $grammar)
143
151
144
152
$ method = 'compile ' .ucfirst ($ command ->name );
145
153
146
- if (method_exists ($ grammar , $ method ) || $ grammar ::hasMacro ($ method )) {
154
+ if (method_exists ($ this -> grammar , $ method ) || $ this -> grammar ::hasMacro ($ method )) {
147
155
if ($ this ->hasState ()) {
148
156
$ this ->state ->update ($ command );
149
157
}
150
158
151
- if (! is_null ($ sql = $ grammar ->$ method ($ this , $ command , $ connection ))) {
159
+ if (! is_null ($ sql = $ this -> grammar ->$ method ($ this , $ command , $ this -> connection ))) {
152
160
$ statements = array_merge ($ statements , (array ) $ sql );
153
161
}
154
162
}
@@ -160,12 +168,11 @@ public function toSql(Connection $connection, Grammar $grammar)
160
168
/**
161
169
* Ensure the commands on the blueprint are valid for the connection type.
162
170
*
163
- * @param \Illuminate\Database\Connection $connection
164
171
* @return void
165
172
*
166
173
* @throws \BadMethodCallException
167
174
*/
168
- protected function ensureCommandsAreValid (Connection $ connection )
175
+ protected function ensureCommandsAreValid ()
169
176
{
170
177
//
171
178
}
@@ -188,15 +195,12 @@ protected function commandsNamed(array $names)
188
195
/**
189
196
* Add the commands that are implied by the blueprint's state.
190
197
*
191
- * @param \Illuminate\Database\Connection $connection
192
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
193
198
* @return void
194
199
*/
195
- protected function addImpliedCommands (Connection $ connection , Grammar $ grammar )
200
+ protected function addImpliedCommands ()
196
201
{
197
- $ this ->addFluentIndexes ($ connection , $ grammar );
198
-
199
- $ this ->addFluentCommands ($ connection , $ grammar );
202
+ $ this ->addFluentIndexes ();
203
+ $ this ->addFluentCommands ();
200
204
201
205
if (! $ this ->creating ()) {
202
206
$ this ->commands = array_map (
@@ -206,25 +210,23 @@ protected function addImpliedCommands(Connection $connection, Grammar $grammar)
206
210
$ this ->commands
207
211
);
208
212
209
- $ this ->addAlterCommands ($ connection , $ grammar );
213
+ $ this ->addAlterCommands ();
210
214
}
211
215
}
212
216
213
217
/**
214
218
* Add the index commands fluently specified on columns.
215
219
*
216
- * @param \Illuminate\Database\Connection $connection
217
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
218
220
* @return void
219
221
*/
220
- protected function addFluentIndexes (Connection $ connection , Grammar $ grammar )
222
+ protected function addFluentIndexes ()
221
223
{
222
224
foreach ($ this ->columns as $ column ) {
223
225
foreach (['primary ' , 'unique ' , 'index ' , 'fulltext ' , 'fullText ' , 'spatialIndex ' ] as $ index ) {
224
226
// If the column is supposed to be changed to an auto increment column and
225
227
// the specified index is primary, there is no need to add a command on
226
228
// 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) {
228
230
continue 2 ;
229
231
}
230
232
@@ -264,14 +266,12 @@ protected function addFluentIndexes(Connection $connection, Grammar $grammar)
264
266
/**
265
267
* Add the fluent commands specified on any columns.
266
268
*
267
- * @param \Illuminate\Database\Connection $connection
268
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
269
269
* @return void
270
270
*/
271
- public function addFluentCommands (Connection $ connection , Grammar $ grammar )
271
+ public function addFluentCommands ()
272
272
{
273
273
foreach ($ this ->columns as $ column ) {
274
- foreach ($ grammar ->getFluentCommands () as $ commandName ) {
274
+ foreach ($ this -> grammar ->getFluentCommands () as $ commandName ) {
275
275
$ this ->addCommand ($ commandName , compact ('column ' ));
276
276
}
277
277
}
@@ -280,17 +280,15 @@ public function addFluentCommands(Connection $connection, Grammar $grammar)
280
280
/**
281
281
* Add the alter commands if whenever needed.
282
282
*
283
- * @param \Illuminate\Database\Connection $connection
284
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
285
283
* @return void
286
284
*/
287
- public function addAlterCommands (Connection $ connection , Grammar $ grammar )
285
+ public function addAlterCommands ()
288
286
{
289
- if (! $ grammar instanceof SQLiteGrammar) {
287
+ if (! $ this -> grammar instanceof SQLiteGrammar) {
290
288
return ;
291
289
}
292
290
293
- $ alterCommands = $ grammar ->getAlterCommands ($ connection );
291
+ $ alterCommands = $ this -> grammar ->getAlterCommands ($ this -> connection );
294
292
295
293
[$ commands , $ lastCommandWasAlter , $ hasAlterCommand ] = [
296
294
[], false , false ,
@@ -313,7 +311,7 @@ public function addAlterCommands(Connection $connection, Grammar $grammar)
313
311
}
314
312
315
313
if ($ hasAlterCommand ) {
316
- $ this ->state = new BlueprintState ($ this , $ connection , $ grammar );
314
+ $ this ->state = new BlueprintState ($ this , $ this -> connection , $ this -> grammar );
317
315
}
318
316
319
317
$ this ->commands = $ commands ;
@@ -1162,8 +1160,10 @@ public function date($column)
1162
1160
* @param int|null $precision
1163
1161
* @return \Illuminate\Database\Schema\ColumnDefinition
1164
1162
*/
1165
- public function dateTime ($ column , $ precision = 0 )
1163
+ public function dateTime ($ column , $ precision = null )
1166
1164
{
1165
+ $ precision ??= $ this ->defaultTimePrecision ();
1166
+
1167
1167
return $ this ->addColumn ('dateTime ' , $ column , compact ('precision ' ));
1168
1168
}
1169
1169
@@ -1174,8 +1174,10 @@ public function dateTime($column, $precision = 0)
1174
1174
* @param int|null $precision
1175
1175
* @return \Illuminate\Database\Schema\ColumnDefinition
1176
1176
*/
1177
- public function dateTimeTz ($ column , $ precision = 0 )
1177
+ public function dateTimeTz ($ column , $ precision = null )
1178
1178
{
1179
+ $ precision ??= $ this ->defaultTimePrecision ();
1180
+
1179
1181
return $ this ->addColumn ('dateTimeTz ' , $ column , compact ('precision ' ));
1180
1182
}
1181
1183
@@ -1186,8 +1188,10 @@ public function dateTimeTz($column, $precision = 0)
1186
1188
* @param int|null $precision
1187
1189
* @return \Illuminate\Database\Schema\ColumnDefinition
1188
1190
*/
1189
- public function time ($ column , $ precision = 0 )
1191
+ public function time ($ column , $ precision = null )
1190
1192
{
1193
+ $ precision ??= $ this ->defaultTimePrecision ();
1194
+
1191
1195
return $ this ->addColumn ('time ' , $ column , compact ('precision ' ));
1192
1196
}
1193
1197
@@ -1198,8 +1202,10 @@ public function time($column, $precision = 0)
1198
1202
* @param int|null $precision
1199
1203
* @return \Illuminate\Database\Schema\ColumnDefinition
1200
1204
*/
1201
- public function timeTz ($ column , $ precision = 0 )
1205
+ public function timeTz ($ column , $ precision = null )
1202
1206
{
1207
+ $ precision ??= $ this ->defaultTimePrecision ();
1208
+
1203
1209
return $ this ->addColumn ('timeTz ' , $ column , compact ('precision ' ));
1204
1210
}
1205
1211
@@ -1210,8 +1216,10 @@ public function timeTz($column, $precision = 0)
1210
1216
* @param int|null $precision
1211
1217
* @return \Illuminate\Database\Schema\ColumnDefinition
1212
1218
*/
1213
- public function timestamp ($ column , $ precision = 0 )
1219
+ public function timestamp ($ column , $ precision = null )
1214
1220
{
1221
+ $ precision ??= $ this ->defaultTimePrecision ();
1222
+
1215
1223
return $ this ->addColumn ('timestamp ' , $ column , compact ('precision ' ));
1216
1224
}
1217
1225
@@ -1222,8 +1230,10 @@ public function timestamp($column, $precision = 0)
1222
1230
* @param int|null $precision
1223
1231
* @return \Illuminate\Database\Schema\ColumnDefinition
1224
1232
*/
1225
- public function timestampTz ($ column , $ precision = 0 )
1233
+ public function timestampTz ($ column , $ precision = null )
1226
1234
{
1235
+ $ precision ??= $ this ->defaultTimePrecision ();
1236
+
1227
1237
return $ this ->addColumn ('timestampTz ' , $ column , compact ('precision ' ));
1228
1238
}
1229
1239
@@ -1233,7 +1243,7 @@ public function timestampTz($column, $precision = 0)
1233
1243
* @param int|null $precision
1234
1244
* @return void
1235
1245
*/
1236
- public function timestamps ($ precision = 0 )
1246
+ public function timestamps ($ precision = null )
1237
1247
{
1238
1248
$ this ->timestamp ('created_at ' , $ precision )->nullable ();
1239
1249
@@ -1248,7 +1258,7 @@ public function timestamps($precision = 0)
1248
1258
* @param int|null $precision
1249
1259
* @return void
1250
1260
*/
1251
- public function nullableTimestamps ($ precision = 0 )
1261
+ public function nullableTimestamps ($ precision = null )
1252
1262
{
1253
1263
$ this ->timestamps ($ precision );
1254
1264
}
@@ -1259,7 +1269,7 @@ public function nullableTimestamps($precision = 0)
1259
1269
* @param int|null $precision
1260
1270
* @return void
1261
1271
*/
1262
- public function timestampsTz ($ precision = 0 )
1272
+ public function timestampsTz ($ precision = null )
1263
1273
{
1264
1274
$ this ->timestampTz ('created_at ' , $ precision )->nullable ();
1265
1275
@@ -1272,7 +1282,7 @@ public function timestampsTz($precision = 0)
1272
1282
* @param int|null $precision
1273
1283
* @return void
1274
1284
*/
1275
- public function datetimes ($ precision = 0 )
1285
+ public function datetimes ($ precision = null )
1276
1286
{
1277
1287
$ this ->datetime ('created_at ' , $ precision )->nullable ();
1278
1288
@@ -1286,7 +1296,7 @@ public function datetimes($precision = 0)
1286
1296
* @param int|null $precision
1287
1297
* @return \Illuminate\Database\Schema\ColumnDefinition
1288
1298
*/
1289
- public function softDeletes ($ column = 'deleted_at ' , $ precision = 0 )
1299
+ public function softDeletes ($ column = 'deleted_at ' , $ precision = null )
1290
1300
{
1291
1301
return $ this ->timestamp ($ column , $ precision )->nullable ();
1292
1302
}
@@ -1298,7 +1308,7 @@ public function softDeletes($column = 'deleted_at', $precision = 0)
1298
1308
* @param int|null $precision
1299
1309
* @return \Illuminate\Database\Schema\ColumnDefinition
1300
1310
*/
1301
- public function softDeletesTz ($ column = 'deleted_at ' , $ precision = 0 )
1311
+ public function softDeletesTz ($ column = 'deleted_at ' , $ precision = null )
1302
1312
{
1303
1313
return $ this ->timestampTz ($ column , $ precision )->nullable ();
1304
1314
}
@@ -1310,7 +1320,7 @@ public function softDeletesTz($column = 'deleted_at', $precision = 0)
1310
1320
* @param int|null $precision
1311
1321
* @return \Illuminate\Database\Schema\ColumnDefinition
1312
1322
*/
1313
- public function softDeletesDatetime ($ column = 'deleted_at ' , $ precision = 0 )
1323
+ public function softDeletesDatetime ($ column = 'deleted_at ' , $ precision = null )
1314
1324
{
1315
1325
return $ this ->datetime ($ column , $ precision )->nullable ();
1316
1326
}
@@ -1853,4 +1863,12 @@ public function getChangedColumns()
1853
1863
return (bool ) $ column ->change ;
1854
1864
});
1855
1865
}
1866
+
1867
+ /**
1868
+ * Get the default time precision.
1869
+ */
1870
+ protected function defaultTimePrecision (): ?int
1871
+ {
1872
+ return $ this ->connection ->getSchemaBuilder ()::$ defaultTimePrecision ;
1873
+ }
1856
1874
}
0 commit comments