Skip to content

[12.x] Fix migrate:fresh failing when database does not exist#59113

Merged
taylorotwell merged 1 commit into
laravel:12.xfrom
MElkmeshi:fix/migrate-fresh-missing-database
Mar 6, 2026
Merged

[12.x] Fix migrate:fresh failing when database does not exist#59113
taylorotwell merged 1 commit into
laravel:12.xfrom
MElkmeshi:fix/migrate-fresh-missing-database

Conversation

@MElkmeshi

Copy link
Copy Markdown
Contributor

Summary

  • When running migrate:fresh against a non-existent database, repositoryExists() throws a QueryException instead of gracefully handling the missing database
  • The migrate command already handles this by prompting the user to create the database, but migrate:fresh crashes before reaching that point
  • This wraps the repositoryExists() call in a try-catch so the wipe step is skipped and the subsequent migrate call handles database creation with its existing prompt

Example

Before (migrate:fresh crashes):

$ php artisan migrate:fresh --seed

   Illuminate\Database\QueryException

  SQLSTATE[HY000] [1049] Unknown database 'backend23'

After (falls through to migrate's database creation prompt):

$ php artisan migrate:fresh --seed

   WARN  The database 'backend23' does not exist on the 'mysql' connection.

 ┌ Would you like to create it? ────────────────────────────────┐
 │ ● Yes / ○ No                                                 │
 └──────────────────────────────────────────────────────────────┘

While migrate already handles this correctly:

$ php artisan migrate

   WARN  The database 'backend23' does not exist on the 'mysql' connection.

 ┌ Would you like to create it? ────────────────────────────────┐
 │ ● Yes / ○ No                                                 │
 └──────────────────────────────────────────────────────────────┘

When running `migrate:fresh` against a non-existent database, `repositoryExists()` throws a QueryException instead of gracefully handling the missing database. The `migrate` command handles this by prompting the user to create the database, but `migrate:fresh` crashes before reaching that point.

This wraps the `repositoryExists()` call in a try-catch so that when the database doesn't exist, the wipe step is skipped and the subsequent `migrate` call handles database creation with its existing prompt.
@MElkmeshi

Copy link
Copy Markdown
Contributor Author

I'm really sorry for using Claude Code English is my second Language and it's a small fix and thought it would make it the PR more readable
I come by each time I want to start from scratch in a new database

@shaedrich

Copy link
Copy Markdown
Contributor

While you are at it, it might be nice if the command could also check if this is merely a typo of one of the configured databases, but this doesn't necessarily have to be in this PR

@taylorotwell taylorotwell merged commit 6575242 into laravel:12.x Mar 6, 2026
72 checks passed
@jasonmccreary

jasonmccreary commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Nice work @MElkmeshi! I spiked on this same issue a while back, but never submitted the PR.

@MElkmeshi

Copy link
Copy Markdown
Contributor Author

Nice work @MElkmeshi! I spiked on this same issue a while back, but never submitted the PR.

Thanks so much it's my first PR

jonagoldman pushed a commit to deplox/laravel-framework that referenced this pull request Apr 30, 2026
…l#59113)

When running `migrate:fresh` against a non-existent database, `repositoryExists()` throws a QueryException instead of gracefully handling the missing database. The `migrate` command handles this by prompting the user to create the database, but `migrate:fresh` crashes before reaching that point.

This wraps the `repositoryExists()` call in a try-catch so that when the database doesn't exist, the wipe step is skipped and the subsequent `migrate` call handles database creation with its existing prompt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants