@@ -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,35 +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 ->normalizeDatetimePrecision ($ connection , $ grammar );
123- $ this ->addImpliedCommands ($ connection , $ grammar );
130+ $ this ->addImpliedCommands ();
124131
125132 $ statements = [];
126133
127134 // Each type of command has a corresponding compiler function on the schema
128135 // grammar which is used to build the necessary SQL statements to build
129136 // the blueprint element, so we'll just call that compilers function.
130- $ this ->ensureCommandsAreValid ($ connection );
137+ $ this ->ensureCommandsAreValid ();
131138
132139 foreach ($ this ->commands as $ command ) {
133140 if ($ command ->shouldBeSkipped ) {
@@ -136,8 +143,8 @@ public function toSql(Connection $connection, Grammar $grammar)
136143
137144 $ method = 'compile ' .ucfirst ($ command ->name );
138145
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 ))) {
141148 $ statements = array_merge ($ statements , (array ) $ sql );
142149 }
143150 }
@@ -149,12 +156,11 @@ public function toSql(Connection $connection, Grammar $grammar)
149156 /**
150157 * Ensure the commands on the blueprint are valid for the connection type.
151158 *
152- * @param \Illuminate\Database\Connection $connection
153159 * @return void
154160 *
155161 * @throws \BadMethodCallException
156162 */
157- protected function ensureCommandsAreValid (Connection $ connection )
163+ protected function ensureCommandsAreValid ()
158164 {
159165 //
160166 }
@@ -175,11 +181,9 @@ protected function commandsNamed(array $names)
175181 /**
176182 * Add the commands that are implied by the blueprint's state.
177183 *
178- * @param \Illuminate\Database\Connection $connection
179- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
180184 * @return void
181185 */
182- protected function addImpliedCommands (Connection $ connection , Grammar $ grammar )
186+ protected function addImpliedCommands ()
183187 {
184188 if (count ($ this ->getAddedColumns ()) > 0 && ! $ this ->creating ()) {
185189 array_unshift ($ this ->commands , $ this ->createCommand ('add ' ));
@@ -189,26 +193,24 @@ protected function addImpliedCommands(Connection $connection, Grammar $grammar)
189193 array_unshift ($ this ->commands , $ this ->createCommand ('change ' ));
190194 }
191195
192- $ this ->addFluentIndexes ($ connection , $ grammar );
196+ $ this ->addFluentIndexes ();
193197
194- $ this ->addFluentCommands ($ connection , $ grammar );
198+ $ this ->addFluentCommands ();
195199 }
196200
197201 /**
198202 * Add the index commands fluently specified on columns.
199203 *
200- * @param \Illuminate\Database\Connection $connection
201- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
202204 * @return void
203205 */
204- protected function addFluentIndexes (Connection $ connection , Grammar $ grammar )
206+ protected function addFluentIndexes ()
205207 {
206208 foreach ($ this ->columns as $ column ) {
207209 foreach (['primary ' , 'unique ' , 'index ' , 'fulltext ' , 'fullText ' , 'spatialIndex ' ] as $ index ) {
208210 // If the column is supposed to be changed to an auto increment column and
209211 // the specified index is primary, there is no need to add a command on
210212 // 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) {
212214 continue 2 ;
213215 }
214216
@@ -248,35 +250,17 @@ protected function addFluentIndexes(Connection $connection, Grammar $grammar)
248250 /**
249251 * Add the fluent commands specified on any columns.
250252 *
251- * @param \Illuminate\Database\Connection $connection
252- * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
253253 * @return void
254254 */
255- public function addFluentCommands (Connection $ connection , Grammar $ grammar )
255+ public function addFluentCommands ()
256256 {
257257 foreach ($ this ->columns as $ column ) {
258- foreach ($ grammar ->getFluentCommands () as $ commandName ) {
258+ foreach ($ this -> grammar ->getFluentCommands () as $ commandName ) {
259259 $ this ->addCommand ($ commandName , compact ('column ' ));
260260 }
261261 }
262262 }
263263
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-
280264 /**
281265 * Determine if the blueprint has a create command.
282266 *
@@ -1122,6 +1106,8 @@ public function date($column)
11221106 */
11231107 public function dateTime ($ column , $ precision = null )
11241108 {
1109+ $ precision ??= $ this ->defaultDatetimePrecision ();
1110+
11251111 return $ this ->addColumn ('dateTime ' , $ column , compact ('precision ' ));
11261112 }
11271113
@@ -1134,6 +1120,8 @@ public function dateTime($column, $precision = null)
11341120 */
11351121 public function dateTimeTz ($ column , $ precision = null )
11361122 {
1123+ $ precision ??= $ this ->defaultDatetimePrecision ();
1124+
11371125 return $ this ->addColumn ('dateTimeTz ' , $ column , compact ('precision ' ));
11381126 }
11391127
@@ -1146,6 +1134,8 @@ public function dateTimeTz($column, $precision = null)
11461134 */
11471135 public function time ($ column , $ precision = null )
11481136 {
1137+ $ precision ??= $ this ->defaultDatetimePrecision ();
1138+
11491139 return $ this ->addColumn ('time ' , $ column , compact ('precision ' ));
11501140 }
11511141
@@ -1158,6 +1148,8 @@ public function time($column, $precision = null)
11581148 */
11591149 public function timeTz ($ column , $ precision = null )
11601150 {
1151+ $ precision ??= $ this ->defaultDatetimePrecision ();
1152+
11611153 return $ this ->addColumn ('timeTz ' , $ column , compact ('precision ' ));
11621154 }
11631155
@@ -1170,6 +1162,8 @@ public function timeTz($column, $precision = null)
11701162 */
11711163 public function timestamp ($ column , $ precision = null )
11721164 {
1165+ $ precision ??= $ this ->defaultDatetimePrecision ();
1166+
11731167 return $ this ->addColumn ('timestamp ' , $ column , compact ('precision ' ));
11741168 }
11751169
@@ -1182,6 +1176,8 @@ public function timestamp($column, $precision = null)
11821176 */
11831177 public function timestampTz ($ column , $ precision = null )
11841178 {
1179+ $ precision ??= $ this ->defaultDatetimePrecision ();
1180+
11851181 return $ this ->addColumn ('timestampTz ' , $ column , compact ('precision ' ));
11861182 }
11871183
@@ -1780,4 +1776,12 @@ public function getChangedColumns()
17801776 return (bool ) $ column ->change ;
17811777 });
17821778 }
1779+
1780+ /**
1781+ * Get the default datetime precision.
1782+ */
1783+ protected function defaultDatetimePrecision (): ?int
1784+ {
1785+ return $ this ->grammar ->getDatetimePrecision ();
1786+ }
17831787}
0 commit comments