@@ -181,9 +181,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
181
181
$ directory .'/.env '
182
182
);
183
183
184
- $ database = $ this ->promptForDatabaseOptions ($ input );
184
+ [ $ database, $ migrate ] = $ this ->promptForDatabaseOptions ($ directory , $ input );
185
185
186
- $ this ->configureDefaultDatabaseConnection ($ directory , $ database , $ name );
186
+ $ this ->configureDefaultDatabaseConnection ($ directory , $ database , $ name , $ migrate );
187
+
188
+ if ($ migrate ) {
189
+ $ this ->runCommands ([
190
+ $ this ->phpBinary ().' artisan migrate ' ,
191
+ ], $ input , $ output , workingPath: $ directory );
192
+ }
187
193
}
188
194
189
195
if ($ input ->getOption ('git ' ) || $ input ->getOption ('github ' ) !== false ) {
@@ -231,47 +237,42 @@ protected function defaultBranch()
231
237
* @param string $directory
232
238
* @param string $database
233
239
* @param string $name
240
+ * @param bool $migrate
234
241
* @return void
235
242
*/
236
- protected function configureDefaultDatabaseConnection (string $ directory , string $ database , string $ name )
243
+ protected function configureDefaultDatabaseConnection (string $ directory , string $ database , string $ name, bool $ migrate )
237
244
{
238
245
// MariaDB configuration only exists as of Laravel 11...
239
246
if ($ database === 'mariadb ' && ! $ this ->hasMariaDBConfig ($ directory )) {
240
247
$ database = 'mysql ' ;
241
248
}
242
249
243
- $ this ->replaceInFile (
244
- 'DB_CONNECTION=mysql ' ,
250
+ $ this ->pregReplaceInFile (
251
+ '/ DB_CONNECTION=.*/ ' ,
245
252
'DB_CONNECTION= ' .$ database ,
246
253
$ directory .'/.env '
247
254
);
248
255
249
- if (! in_array ($ database , ['sqlite ' ])) {
250
- $ this ->replaceInFile (
251
- 'DB_CONNECTION=mysql ' ,
252
- 'DB_CONNECTION= ' .$ database ,
253
- $ directory .'/.env.example '
254
- );
255
- }
256
-
257
- $ defaults = [
258
- 'DB_HOST=127.0.0.1 ' ,
259
- 'DB_PORT=3306 ' ,
260
- 'DB_DATABASE=laravel ' ,
261
- 'DB_USERNAME=root ' ,
262
- 'DB_PASSWORD= ' ,
263
- ];
256
+ $ this ->pregReplaceInFile (
257
+ '/DB_CONNECTION=.*/ ' ,
258
+ 'DB_CONNECTION= ' .$ database ,
259
+ $ directory .'/.env.example '
260
+ );
264
261
265
262
if ($ database === 'sqlite ' ) {
266
- $ this ->replaceInFile (
267
- $ defaults ,
268
- collect ($ defaults )->map (fn ($ default ) => "# {$ default }" )->all (),
269
- $ directory .'/.env '
270
- );
263
+ $ environment = file_get_contents ($ directory .'/.env ' );
264
+
265
+ // If database options aren't commented, comment them for SQLite...
266
+ if (! str_contains ($ environment , '# DB_HOST=127.0.0.1 ' )) {
267
+ return $ this ->commentDatabaseConfigurationForSqlite ($ directory );
268
+ }
271
269
272
270
return ;
273
271
}
274
272
273
+ // Any commented database configuration options should be uncommented when not on SQLite...
274
+ $ this ->uncommentDatabaseConfiguration ($ directory );
275
+
275
276
$ defaultPorts = [
276
277
'pgsql ' => '5432 ' ,
277
278
'sqlsrv ' => '1433 ' ,
@@ -323,6 +324,64 @@ protected function hasMariaDBConfig(string $directory): bool
323
324
);
324
325
}
325
326
327
+ /**
328
+ * Comment the irrelevant database configuration entries for SQLite applications.
329
+ *
330
+ * @param string $directory
331
+ * @return void
332
+ */
333
+ protected function commentDatabaseConfigurationForSqlite (string $ directory )
334
+ {
335
+ $ defaults = [
336
+ 'DB_HOST=127.0.0.1 ' ,
337
+ 'DB_PORT=3306 ' ,
338
+ 'DB_DATABASE=laravel ' ,
339
+ 'DB_USERNAME=root ' ,
340
+ 'DB_PASSWORD= ' ,
341
+ ];
342
+
343
+ $ this ->replaceInFile (
344
+ $ defaults ,
345
+ collect ($ defaults )->map (fn ($ default ) => "# {$ default }" )->all (),
346
+ $ directory .'/.env '
347
+ );
348
+
349
+ $ this ->replaceInFile (
350
+ $ defaults ,
351
+ collect ($ defaults )->map (fn ($ default ) => "# {$ default }" )->all (),
352
+ $ directory .'/.env.example '
353
+ );
354
+ }
355
+
356
+ /**
357
+ * Uncomment the relevant database configuration entries for non SQLite applications.
358
+ *
359
+ * @param string $directory
360
+ * @return void
361
+ */
362
+ protected function uncommentDatabaseConfiguration (string $ directory )
363
+ {
364
+ $ defaults = [
365
+ '# DB_HOST=127.0.0.1 ' ,
366
+ '# DB_PORT=3306 ' ,
367
+ '# DB_DATABASE=laravel ' ,
368
+ '# DB_USERNAME=root ' ,
369
+ '# DB_PASSWORD= ' ,
370
+ ];
371
+
372
+ $ this ->replaceInFile (
373
+ $ defaults ,
374
+ collect ($ defaults )->map (fn ($ default ) => substr ($ default , 2 ))->all (),
375
+ $ directory .'/.env '
376
+ );
377
+
378
+ $ this ->replaceInFile (
379
+ $ defaults ,
380
+ collect ($ defaults )->map (fn ($ default ) => substr ($ default , 2 ))->all (),
381
+ $ directory .'/.env.example '
382
+ );
383
+ }
384
+
326
385
/**
327
386
* Install Laravel Breeze into the application.
328
387
*
@@ -382,12 +441,14 @@ protected function installJetstream(string $directory, InputInterface $input, Ou
382
441
/**
383
442
* Determine the default database connection.
384
443
*
444
+ * @param string $directory
385
445
* @param \Symfony\Component\Console\Input\InputInterface $input
386
446
* @return string
387
447
*/
388
- protected function promptForDatabaseOptions (InputInterface $ input )
448
+ protected function promptForDatabaseOptions (string $ directory , InputInterface $ input )
389
449
{
390
- $ database = 'mysql ' ;
450
+ // Laravel 11.x appliations use SQLite as default...
451
+ $ defaultDatabase = $ this ->hasMariaDBConfig ($ directory ) ? 'sqlite ' : 'mysql ' ;
391
452
392
453
if ($ input ->isInteractive ()) {
393
454
$ database = select (
@@ -399,11 +460,15 @@ protected function promptForDatabaseOptions(InputInterface $input)
399
460
'sqlite ' => 'SQLite ' ,
400
461
'sqlsrv ' => 'SQL Server ' ,
401
462
],
402
- default: $ database
463
+ default: $ defaultDatabase
403
464
);
465
+
466
+ if ($ database !== $ defaultDatabase ) {
467
+ $ migrate = confirm (label: 'Default database updated. Would you like to run the default database migrations? ' , default: true );
468
+ }
404
469
}
405
470
406
- return $ database ;
471
+ return [ $ database ?? $ defaultDatabase , $ migrate ?? false ] ;
407
472
}
408
473
409
474
/**
@@ -808,4 +873,20 @@ protected function replaceInFile(string|array $search, string|array $replace, st
808
873
str_replace ($ search , $ replace , file_get_contents ($ file ))
809
874
);
810
875
}
876
+
877
+ /**
878
+ * Replace the given string in the given file using regular expressions.
879
+ *
880
+ * @param string|array $search
881
+ * @param string|array $replace
882
+ * @param string $file
883
+ * @return void
884
+ */
885
+ protected function pregReplaceInFile (string $ pattern , string $ replace , string $ file )
886
+ {
887
+ file_put_contents (
888
+ $ file ,
889
+ preg_replace ($ pattern , $ replace , file_get_contents ($ file ))
890
+ );
891
+ }
811
892
}
0 commit comments