@@ -15,6 +15,16 @@ class Blueprint
15
15
{
16
16
use Macroable;
17
17
18
+ /**
19
+ * The database connection instance.
20
+ */
21
+ protected Connection $ connection ;
22
+
23
+ /**
24
+ * The schema grammar instance.
25
+ */
26
+ protected Grammar $ grammar ;
27
+
18
28
/**
19
29
* The table the blueprint describes.
20
30
*
@@ -86,8 +96,10 @@ class Blueprint
86
96
* @param string $prefix
87
97
* @return void
88
98
*/
89
- public function __construct ($ table , ?Closure $ callback = null , $ prefix = '' )
99
+ public function __construct (Connection $ connection , $ table , ?Closure $ callback = null , $ prefix = '' )
90
100
{
101
+ $ this ->connection = $ connection ;
102
+ $ this ->grammar = $ connection ->getSchemaGrammar ();
91
103
$ this ->table = $ table ;
92
104
$ this ->prefix = $ prefix ;
93
105
@@ -99,35 +111,30 @@ public function __construct($table, ?Closure $callback = null, $prefix = '')
99
111
/**
100
112
* Execute the blueprint against the database.
101
113
*
102
- * @param \Illuminate\Database\Connection $connection
103
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
104
114
* @return void
105
115
*/
106
- public function build (Connection $ connection , Grammar $ grammar )
116
+ public function build ()
107
117
{
108
- foreach ($ this ->toSql ($ connection , $ grammar ) as $ statement ) {
109
- $ connection ->statement ($ statement );
118
+ foreach ($ this ->toSql () as $ statement ) {
119
+ $ this -> connection ->statement ($ statement );
110
120
}
111
121
}
112
122
113
123
/**
114
124
* Get the raw SQL statements for the blueprint.
115
125
*
116
- * @param \Illuminate\Database\Connection $connection
117
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
118
126
* @return array
119
127
*/
120
- public function toSql (Connection $ connection , Grammar $ grammar )
128
+ public function toSql ()
121
129
{
122
- $ this ->normalizeDatetimePrecision ($ connection , $ grammar );
123
- $ this ->addImpliedCommands ($ connection , $ grammar );
130
+ $ this ->addImpliedCommands ();
124
131
125
132
$ statements = [];
126
133
127
134
// Each type of command has a corresponding compiler function on the schema
128
135
// grammar which is used to build the necessary SQL statements to build
129
136
// the blueprint element, so we'll just call that compilers function.
130
- $ this ->ensureCommandsAreValid ($ connection );
137
+ $ this ->ensureCommandsAreValid ();
131
138
132
139
foreach ($ this ->commands as $ command ) {
133
140
if ($ command ->shouldBeSkipped ) {
@@ -136,8 +143,8 @@ public function toSql(Connection $connection, Grammar $grammar)
136
143
137
144
$ method = 'compile ' .ucfirst ($ command ->name );
138
145
139
- if (method_exists ($ grammar , $ method ) || $ grammar ::hasMacro ($ method )) {
140
- if (! is_null ($ sql = $ grammar ->$ method ($ this , $ command , $ connection ))) {
146
+ if (method_exists ($ this -> grammar , $ method ) || $ this -> grammar ::hasMacro ($ method )) {
147
+ if (! is_null ($ sql = $ this -> grammar ->$ method ($ this , $ command , $ this -> connection ))) {
141
148
$ statements = array_merge ($ statements , (array ) $ sql );
142
149
}
143
150
}
@@ -149,12 +156,11 @@ public function toSql(Connection $connection, Grammar $grammar)
149
156
/**
150
157
* Ensure the commands on the blueprint are valid for the connection type.
151
158
*
152
- * @param \Illuminate\Database\Connection $connection
153
159
* @return void
154
160
*
155
161
* @throws \BadMethodCallException
156
162
*/
157
- protected function ensureCommandsAreValid (Connection $ connection )
163
+ protected function ensureCommandsAreValid ()
158
164
{
159
165
//
160
166
}
@@ -175,11 +181,9 @@ protected function commandsNamed(array $names)
175
181
/**
176
182
* Add the commands that are implied by the blueprint's state.
177
183
*
178
- * @param \Illuminate\Database\Connection $connection
179
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
180
184
* @return void
181
185
*/
182
- protected function addImpliedCommands (Connection $ connection , Grammar $ grammar )
186
+ protected function addImpliedCommands ()
183
187
{
184
188
if (count ($ this ->getAddedColumns ()) > 0 && ! $ this ->creating ()) {
185
189
array_unshift ($ this ->commands , $ this ->createCommand ('add ' ));
@@ -189,26 +193,24 @@ protected function addImpliedCommands(Connection $connection, Grammar $grammar)
189
193
array_unshift ($ this ->commands , $ this ->createCommand ('change ' ));
190
194
}
191
195
192
- $ this ->addFluentIndexes ($ connection , $ grammar );
196
+ $ this ->addFluentIndexes ();
193
197
194
- $ this ->addFluentCommands ($ connection , $ grammar );
198
+ $ this ->addFluentCommands ();
195
199
}
196
200
197
201
/**
198
202
* Add the index commands fluently specified on columns.
199
203
*
200
- * @param \Illuminate\Database\Connection $connection
201
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
202
204
* @return void
203
205
*/
204
- protected function addFluentIndexes (Connection $ connection , Grammar $ grammar )
206
+ protected function addFluentIndexes ()
205
207
{
206
208
foreach ($ this ->columns as $ column ) {
207
209
foreach (['primary ' , 'unique ' , 'index ' , 'fulltext ' , 'fullText ' , 'spatialIndex ' ] as $ index ) {
208
210
// If the column is supposed to be changed to an auto increment column and
209
211
// the specified index is primary, there is no need to add a command on
210
212
// MySQL, as it will be handled during the column definition instead.
211
- if ($ index === 'primary ' && $ column ->autoIncrement && $ column ->change && $ grammar instanceof MySqlGrammar) {
213
+ if ($ index === 'primary ' && $ column ->autoIncrement && $ column ->change && $ this -> grammar instanceof MySqlGrammar) {
212
214
continue 2 ;
213
215
}
214
216
@@ -248,35 +250,17 @@ protected function addFluentIndexes(Connection $connection, Grammar $grammar)
248
250
/**
249
251
* Add the fluent commands specified on any columns.
250
252
*
251
- * @param \Illuminate\Database\Connection $connection
252
- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
253
253
* @return void
254
254
*/
255
- public function addFluentCommands (Connection $ connection , Grammar $ grammar )
255
+ public function addFluentCommands ()
256
256
{
257
257
foreach ($ this ->columns as $ column ) {
258
- foreach ($ grammar ->getFluentCommands () as $ commandName ) {
258
+ foreach ($ this -> grammar ->getFluentCommands () as $ commandName ) {
259
259
$ this ->addCommand ($ commandName , compact ('column ' ));
260
260
}
261
261
}
262
262
}
263
263
264
- /**
265
- * Normalizes any `null` datetime precision to the grammar's default.
266
- */
267
- protected function normalizeDatetimePrecision (Connection $ connection , Grammar $ grammar ): void
268
- {
269
- $ types = ['dateTime ' , 'dateTimeTz ' , 'time ' , 'timeTz ' , 'timestamp ' , 'timestampTz ' ];
270
-
271
- foreach ($ this ->columns as $ column ) {
272
- if (! in_array ($ column ->type , $ types , strict: true )) {
273
- continue ;
274
- }
275
-
276
- $ column ->precision ??= $ grammar ->getDatetimePrecision ();
277
- }
278
- }
279
-
280
264
/**
281
265
* Determine if the blueprint has a create command.
282
266
*
@@ -1122,6 +1106,8 @@ public function date($column)
1122
1106
*/
1123
1107
public function dateTime ($ column , $ precision = null )
1124
1108
{
1109
+ $ precision ??= $ this ->defaultDatetimePrecision ();
1110
+
1125
1111
return $ this ->addColumn ('dateTime ' , $ column , compact ('precision ' ));
1126
1112
}
1127
1113
@@ -1134,6 +1120,8 @@ public function dateTime($column, $precision = null)
1134
1120
*/
1135
1121
public function dateTimeTz ($ column , $ precision = null )
1136
1122
{
1123
+ $ precision ??= $ this ->defaultDatetimePrecision ();
1124
+
1137
1125
return $ this ->addColumn ('dateTimeTz ' , $ column , compact ('precision ' ));
1138
1126
}
1139
1127
@@ -1146,6 +1134,8 @@ public function dateTimeTz($column, $precision = null)
1146
1134
*/
1147
1135
public function time ($ column , $ precision = null )
1148
1136
{
1137
+ $ precision ??= $ this ->defaultDatetimePrecision ();
1138
+
1149
1139
return $ this ->addColumn ('time ' , $ column , compact ('precision ' ));
1150
1140
}
1151
1141
@@ -1158,6 +1148,8 @@ public function time($column, $precision = null)
1158
1148
*/
1159
1149
public function timeTz ($ column , $ precision = null )
1160
1150
{
1151
+ $ precision ??= $ this ->defaultDatetimePrecision ();
1152
+
1161
1153
return $ this ->addColumn ('timeTz ' , $ column , compact ('precision ' ));
1162
1154
}
1163
1155
@@ -1170,6 +1162,8 @@ public function timeTz($column, $precision = null)
1170
1162
*/
1171
1163
public function timestamp ($ column , $ precision = null )
1172
1164
{
1165
+ $ precision ??= $ this ->defaultDatetimePrecision ();
1166
+
1173
1167
return $ this ->addColumn ('timestamp ' , $ column , compact ('precision ' ));
1174
1168
}
1175
1169
@@ -1182,6 +1176,8 @@ public function timestamp($column, $precision = null)
1182
1176
*/
1183
1177
public function timestampTz ($ column , $ precision = null )
1184
1178
{
1179
+ $ precision ??= $ this ->defaultDatetimePrecision ();
1180
+
1185
1181
return $ this ->addColumn ('timestampTz ' , $ column , compact ('precision ' ));
1186
1182
}
1187
1183
@@ -1780,4 +1776,12 @@ public function getChangedColumns()
1780
1776
return (bool ) $ column ->change ;
1781
1777
});
1782
1778
}
1779
+
1780
+ /**
1781
+ * Get the default datetime precision.
1782
+ */
1783
+ protected function defaultDatetimePrecision (): ?int
1784
+ {
1785
+ return $ this ->grammar ->getDatetimePrecision ();
1786
+ }
1783
1787
}
0 commit comments