@@ -15,6 +15,16 @@ class Blueprint
1515{
1616 use Macroable;
1717
18+ /**
19+ * The database connection instance.
20+ */
21+ protected Connection $ connection ;
22+
23+ /**
24+ * The schema grammar instance.
25+ */
26+ protected Grammar $ grammar ;
27+
1828 /**
1929 * The table the blueprint describes.
2030 *
@@ -86,8 +96,10 @@ class Blueprint
8696 * @param string $prefix
8797 * @return void
8898 */
89- public function __construct ($ table , ?Closure $ callback = null , $ prefix = '' )
99+ public function __construct (Connection $ connection , $ table , ?Closure $ callback = null , $ prefix = '' )
90100 {
101+ $ this ->connection = $ connection ;
102+ $ this ->grammar = $ connection ->getSchemaGrammar ();
91103 $ this ->table = $ table ;
92104 $ this ->prefix = $ prefix ;
93105
@@ -99,34 +111,30 @@ public function __construct($table, ?Closure $callback = null, $prefix = '')
99111 /**
100112 * Execute the blueprint against the database.
101113 *
102- * @param \Illuminate\Database\Connection $connection
103- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
104114 * @return void
105115 */
106- public function build (Connection $ connection , Grammar $ grammar )
116+ public function build ()
107117 {
108- foreach ($ this ->toSql ($ connection , $ grammar ) as $ statement ) {
109- $ connection ->statement ($ statement );
118+ foreach ($ this ->toSql () as $ statement ) {
119+ $ this -> connection ->statement ($ statement );
110120 }
111121 }
112122
113123 /**
114124 * Get the raw SQL statements for the blueprint.
115125 *
116- * @param \Illuminate\Database\Connection $connection
117- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
118126 * @return array
119127 */
120- public function toSql (Connection $ connection , Grammar $ grammar )
128+ public function toSql ()
121129 {
122- $ this ->addImpliedCommands ($ connection , $ grammar );
130+ $ this ->addImpliedCommands ();
123131
124132 $ statements = [];
125133
126134 // Each type of command has a corresponding compiler function on the schema
127135 // grammar which is used to build the necessary SQL statements to build
128136 // the blueprint element, so we'll just call that compilers function.
129- $ this ->ensureCommandsAreValid ($ connection );
137+ $ this ->ensureCommandsAreValid ();
130138
131139 foreach ($ this ->commands as $ command ) {
132140 if ($ command ->shouldBeSkipped ) {
@@ -135,8 +143,8 @@ public function toSql(Connection $connection, Grammar $grammar)
135143
136144 $ method = 'compile ' .ucfirst ($ command ->name );
137145
138- if (method_exists ($ grammar , $ method ) || $ grammar ::hasMacro ($ method )) {
139- 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 ))) {
140148 $ statements = array_merge ($ statements , (array ) $ sql );
141149 }
142150 }
@@ -148,12 +156,11 @@ public function toSql(Connection $connection, Grammar $grammar)
148156 /**
149157 * Ensure the commands on the blueprint are valid for the connection type.
150158 *
151- * @param \Illuminate\Database\Connection $connection
152159 * @return void
153160 *
154161 * @throws \BadMethodCallException
155162 */
156- protected function ensureCommandsAreValid (Connection $ connection )
163+ protected function ensureCommandsAreValid ()
157164 {
158165 //
159166 }
@@ -174,11 +181,9 @@ protected function commandsNamed(array $names)
174181 /**
175182 * Add the commands that are implied by the blueprint's state.
176183 *
177- * @param \Illuminate\Database\Connection $connection
178- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
179184 * @return void
180185 */
181- protected function addImpliedCommands (Connection $ connection , Grammar $ grammar )
186+ protected function addImpliedCommands ()
182187 {
183188 if (count ($ this ->getAddedColumns ()) > 0 && ! $ this ->creating ()) {
184189 array_unshift ($ this ->commands , $ this ->createCommand ('add ' ));
@@ -188,26 +193,24 @@ protected function addImpliedCommands(Connection $connection, Grammar $grammar)
188193 array_unshift ($ this ->commands , $ this ->createCommand ('change ' ));
189194 }
190195
191- $ this ->addFluentIndexes ($ connection , $ grammar );
196+ $ this ->addFluentIndexes ();
192197
193- $ this ->addFluentCommands ($ connection , $ grammar );
198+ $ this ->addFluentCommands ();
194199 }
195200
196201 /**
197202 * Add the index commands fluently specified on columns.
198203 *
199- * @param \Illuminate\Database\Connection $connection
200- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
201204 * @return void
202205 */
203- protected function addFluentIndexes (Connection $ connection , Grammar $ grammar )
206+ protected function addFluentIndexes ()
204207 {
205208 foreach ($ this ->columns as $ column ) {
206209 foreach (['primary ' , 'unique ' , 'index ' , 'fulltext ' , 'fullText ' , 'spatialIndex ' ] as $ index ) {
207210 // If the column is supposed to be changed to an auto increment column and
208211 // the specified index is primary, there is no need to add a command on
209212 // MySQL, as it will be handled during the column definition instead.
210- if ($ index === 'primary ' && $ column ->autoIncrement && $ column ->change && $ grammar instanceof MySqlGrammar) {
213+ if ($ index === 'primary ' && $ column ->autoIncrement && $ column ->change && $ this -> grammar instanceof MySqlGrammar) {
211214 continue 2 ;
212215 }
213216
@@ -247,14 +250,12 @@ protected function addFluentIndexes(Connection $connection, Grammar $grammar)
247250 /**
248251 * Add the fluent commands specified on any columns.
249252 *
250- * @param \Illuminate\Database\Connection $connection
251- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
252253 * @return void
253254 */
254- public function addFluentCommands (Connection $ connection , Grammar $ grammar )
255+ public function addFluentCommands ()
255256 {
256257 foreach ($ this ->columns as $ column ) {
257- foreach ($ grammar ->getFluentCommands () as $ commandName ) {
258+ foreach ($ this -> grammar ->getFluentCommands () as $ commandName ) {
258259 $ this ->addCommand ($ commandName , compact ('column ' ));
259260 }
260261 }
@@ -1103,8 +1104,10 @@ public function date($column)
11031104 * @param int|null $precision
11041105 * @return \Illuminate\Database\Schema\ColumnDefinition
11051106 */
1106- public function dateTime ($ column , $ precision = 0 )
1107+ public function dateTime ($ column , $ precision = null )
11071108 {
1109+ $ precision ??= $ this ->defaultDatetimePrecision ();
1110+
11081111 return $ this ->addColumn ('dateTime ' , $ column , compact ('precision ' ));
11091112 }
11101113
@@ -1115,8 +1118,10 @@ public function dateTime($column, $precision = 0)
11151118 * @param int|null $precision
11161119 * @return \Illuminate\Database\Schema\ColumnDefinition
11171120 */
1118- public function dateTimeTz ($ column , $ precision = 0 )
1121+ public function dateTimeTz ($ column , $ precision = null )
11191122 {
1123+ $ precision ??= $ this ->defaultDatetimePrecision ();
1124+
11201125 return $ this ->addColumn ('dateTimeTz ' , $ column , compact ('precision ' ));
11211126 }
11221127
@@ -1127,8 +1132,10 @@ public function dateTimeTz($column, $precision = 0)
11271132 * @param int|null $precision
11281133 * @return \Illuminate\Database\Schema\ColumnDefinition
11291134 */
1130- public function time ($ column , $ precision = 0 )
1135+ public function time ($ column , $ precision = null )
11311136 {
1137+ $ precision ??= $ this ->defaultDatetimePrecision ();
1138+
11321139 return $ this ->addColumn ('time ' , $ column , compact ('precision ' ));
11331140 }
11341141
@@ -1139,8 +1146,10 @@ public function time($column, $precision = 0)
11391146 * @param int|null $precision
11401147 * @return \Illuminate\Database\Schema\ColumnDefinition
11411148 */
1142- public function timeTz ($ column , $ precision = 0 )
1149+ public function timeTz ($ column , $ precision = null )
11431150 {
1151+ $ precision ??= $ this ->defaultDatetimePrecision ();
1152+
11441153 return $ this ->addColumn ('timeTz ' , $ column , compact ('precision ' ));
11451154 }
11461155
@@ -1151,8 +1160,10 @@ public function timeTz($column, $precision = 0)
11511160 * @param int|null $precision
11521161 * @return \Illuminate\Database\Schema\ColumnDefinition
11531162 */
1154- public function timestamp ($ column , $ precision = 0 )
1163+ public function timestamp ($ column , $ precision = null )
11551164 {
1165+ $ precision ??= $ this ->defaultDatetimePrecision ();
1166+
11561167 return $ this ->addColumn ('timestamp ' , $ column , compact ('precision ' ));
11571168 }
11581169
@@ -1163,8 +1174,10 @@ public function timestamp($column, $precision = 0)
11631174 * @param int|null $precision
11641175 * @return \Illuminate\Database\Schema\ColumnDefinition
11651176 */
1166- public function timestampTz ($ column , $ precision = 0 )
1177+ public function timestampTz ($ column , $ precision = null )
11671178 {
1179+ $ precision ??= $ this ->defaultDatetimePrecision ();
1180+
11681181 return $ this ->addColumn ('timestampTz ' , $ column , compact ('precision ' ));
11691182 }
11701183
@@ -1174,7 +1187,7 @@ public function timestampTz($column, $precision = 0)
11741187 * @param int|null $precision
11751188 * @return void
11761189 */
1177- public function timestamps ($ precision = 0 )
1190+ public function timestamps ($ precision = null )
11781191 {
11791192 $ this ->timestamp ('created_at ' , $ precision )->nullable ();
11801193
@@ -1189,7 +1202,7 @@ public function timestamps($precision = 0)
11891202 * @param int|null $precision
11901203 * @return void
11911204 */
1192- public function nullableTimestamps ($ precision = 0 )
1205+ public function nullableTimestamps ($ precision = null )
11931206 {
11941207 $ this ->timestamps ($ precision );
11951208 }
@@ -1200,7 +1213,7 @@ public function nullableTimestamps($precision = 0)
12001213 * @param int|null $precision
12011214 * @return void
12021215 */
1203- public function timestampsTz ($ precision = 0 )
1216+ public function timestampsTz ($ precision = null )
12041217 {
12051218 $ this ->timestampTz ('created_at ' , $ precision )->nullable ();
12061219
@@ -1213,7 +1226,7 @@ public function timestampsTz($precision = 0)
12131226 * @param int|null $precision
12141227 * @return void
12151228 */
1216- public function datetimes ($ precision = 0 )
1229+ public function datetimes ($ precision = null )
12171230 {
12181231 $ this ->datetime ('created_at ' , $ precision )->nullable ();
12191232
@@ -1227,7 +1240,7 @@ public function datetimes($precision = 0)
12271240 * @param int|null $precision
12281241 * @return \Illuminate\Database\Schema\ColumnDefinition
12291242 */
1230- public function softDeletes ($ column = 'deleted_at ' , $ precision = 0 )
1243+ public function softDeletes ($ column = 'deleted_at ' , $ precision = null )
12311244 {
12321245 return $ this ->timestamp ($ column , $ precision )->nullable ();
12331246 }
@@ -1239,7 +1252,7 @@ public function softDeletes($column = 'deleted_at', $precision = 0)
12391252 * @param int|null $precision
12401253 * @return \Illuminate\Database\Schema\ColumnDefinition
12411254 */
1242- public function softDeletesTz ($ column = 'deleted_at ' , $ precision = 0 )
1255+ public function softDeletesTz ($ column = 'deleted_at ' , $ precision = null )
12431256 {
12441257 return $ this ->timestampTz ($ column , $ precision )->nullable ();
12451258 }
@@ -1251,7 +1264,7 @@ public function softDeletesTz($column = 'deleted_at', $precision = 0)
12511264 * @param int|null $precision
12521265 * @return \Illuminate\Database\Schema\ColumnDefinition
12531266 */
1254- public function softDeletesDatetime ($ column = 'deleted_at ' , $ precision = 0 )
1267+ public function softDeletesDatetime ($ column = 'deleted_at ' , $ precision = null )
12551268 {
12561269 return $ this ->datetime ($ column , $ precision )->nullable ();
12571270 }
@@ -1763,4 +1776,12 @@ public function getChangedColumns()
17631776 return (bool ) $ column ->change ;
17641777 });
17651778 }
1779+
1780+ /**
1781+ * Get the default datetime precision.
1782+ */
1783+ protected function defaultDatetimePrecision (): ?int
1784+ {
1785+ return $ this ->grammar ->getDatetimePrecision ();
1786+ }
17661787}
0 commit comments