9
9
use Illuminate \Database \Migrations \Migrator ;
10
10
use Illuminate \Database \SQLiteDatabaseDoesNotExistException ;
11
11
use Illuminate \Database \SqlServerConnection ;
12
+ use Illuminate \Support \Str ;
12
13
use PDOException ;
13
14
use RuntimeException ;
14
15
use Symfony \Component \Console \Attribute \AsCommand ;
@@ -163,26 +164,41 @@ protected function repositoryExists()
163
164
{
164
165
return retry (2 , fn () => $ this ->migrator ->repositoryExists (), 0 , function ($ e ) {
165
166
try {
166
- if ($ e ->getPrevious () instanceof SQLiteDatabaseDoesNotExistException) {
167
- return $ this ->createMissingSqliteDatabase ($ e ->getPrevious ()->path );
168
- }
169
-
170
- $ connection = $ this ->migrator ->resolveConnection ($ this ->option ('database ' ));
171
-
172
- if (
173
- $ e ->getPrevious () instanceof PDOException &&
174
- $ e ->getPrevious ()->getCode () === 1049 &&
175
- in_array ($ connection ->getDriverName (), ['mysql ' , 'mariadb ' ])) {
176
- return $ this ->createMissingMysqlDatabase ($ connection );
177
- }
178
-
179
- return false ;
167
+ return $ this ->handleMissingDatabase ($ e ->getPrevious ());
180
168
} catch (Throwable ) {
181
169
return false ;
182
170
}
183
171
});
184
172
}
185
173
174
+ /**
175
+ * Attempt to create the database if it is missing.
176
+ *
177
+ * @param \Throwable $e
178
+ * @return bool
179
+ */
180
+ protected function handleMissingDatabase (Throwable $ e )
181
+ {
182
+ if ($ e instanceof SQLiteDatabaseDoesNotExistException) {
183
+ return $ this ->createMissingSqliteDatabase ($ e ->path );
184
+ }
185
+
186
+ $ connection = $ this ->migrator ->resolveConnection ($ this ->option ('database ' ));
187
+
188
+ if (! $ e instanceof PDOException) {
189
+ return false ;
190
+ }
191
+
192
+ if (($ e ->getCode () === 1049 && in_array ($ connection ->getDriverName (), ['mysql ' , 'mariadb ' ])) ||
193
+ (($ e ->errorInfo [0 ] ?? null ) == '08006 ' &&
194
+ $ connection ->getDriverName () == 'pgsql ' &&
195
+ Str::contains ($ e ->getMessage (), '" ' .$ connection ->getDatabaseName ().'" ' ))) {
196
+ return $ this ->createMissingMySqlOrPgsqlDatabase ($ connection );
197
+ }
198
+
199
+ return false ;
200
+ }
201
+
186
202
/**
187
203
* Create a missing SQLite database.
188
204
*
@@ -213,13 +229,14 @@ protected function createMissingSqliteDatabase($path)
213
229
}
214
230
215
231
/**
216
- * Create a missing MySQL database.
232
+ * Create a missing MySQL or Postgres database.
217
233
*
234
+ * @param \Illuminate\Database\Connection $connection
218
235
* @return bool
219
236
*
220
237
* @throws \RuntimeException
221
238
*/
222
- protected function createMissingMysqlDatabase ($ connection )
239
+ protected function createMissingMySqlOrPgsqlDatabase ($ connection )
223
240
{
224
241
if ($ this ->laravel ['config ' ]->get ("database.connections. {$ connection ->getName ()}.database " ) !== $ connection ->getDatabaseName ()) {
225
242
return false ;
@@ -238,15 +255,25 @@ protected function createMissingMysqlDatabase($connection)
238
255
throw new RuntimeException ('Database was not created. Aborting migration. ' );
239
256
}
240
257
}
241
-
242
258
try {
243
- $ this ->laravel ['config ' ]->set ("database.connections. {$ connection ->getName ()}.database " , null );
259
+ $ this ->laravel ['config ' ]->set (
260
+ "database.connections. {$ connection ->getName ()}.database " ,
261
+ match ($ connection ->getDriverName ()) {
262
+ 'mysql ' , 'mariadb ' => null ,
263
+ 'pgsql ' => 'postgres ' ,
264
+ },
265
+ );
244
266
245
267
$ this ->laravel ['db ' ]->purge ();
246
268
247
269
$ freshConnection = $ this ->migrator ->resolveConnection ($ this ->option ('database ' ));
248
270
249
- return tap ($ freshConnection ->unprepared ("CREATE DATABASE IF NOT EXISTS ` {$ connection ->getDatabaseName ()}` " ), function () {
271
+ return tap ($ freshConnection ->unprepared (
272
+ match ($ connection ->getDriverName ()) {
273
+ 'mysql ' , 'mariadb ' => "CREATE DATABASE IF NOT EXISTS ` {$ connection ->getDatabaseName ()}` " ,
274
+ 'pgsql ' => 'CREATE DATABASE " ' .$ connection ->getDatabaseName ().'" ' ,
275
+ }
276
+ ), function () {
250
277
$ this ->laravel ['db ' ]->purge ();
251
278
});
252
279
} finally {
0 commit comments