Skip to content

Commit 85ca3fb

Browse files
[9.x] Prompt to create MySQL db when migrating (#44153)
* create missing MySQL database * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent b088269 commit 85ca3fb

File tree

1 file changed

+74
-10
lines changed

1 file changed

+74
-10
lines changed

src/Illuminate/Database/Console/Migrations/MigrateCommand.php

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Illuminate\Database\Migrations\Migrator;
1010
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
1111
use Illuminate\Database\SqlServerConnection;
12+
use PDOException;
13+
use Throwable;
1214

1315
class MigrateCommand extends BaseCommand
1416
{
@@ -134,26 +136,88 @@ protected function prepareDatabase()
134136
protected function repositoryExists()
135137
{
136138
return retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) {
137-
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
138-
return false;
139-
}
139+
try {
140+
if ($e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
141+
return $this->createMissingSqliteDatbase($e->getPrevious()->path);
142+
}
140143

141-
if ($this->option('force')) {
142-
return touch($e->getPrevious()->path);
143-
}
144+
$connection = $this->migrator->resolveConnection($this->option('database'));
144145

145-
if ($this->option('no-interaction')) {
146+
if (
147+
$e->getPrevious() instanceof PDOException &&
148+
$e->getPrevious()->getCode() === 1049 &&
149+
$connection->getDriverName() === 'mysql') {
150+
return $this->createMissingMysqlDatabase($connection);
151+
}
152+
153+
return false;
154+
} catch (Throwable) {
146155
return false;
147156
}
157+
});
158+
}
148159

149-
$this->components->warn('The SQLite database does not exist: '.$e->getPrevious()->path);
160+
/**
161+
* Create a missing SQLite database.
162+
*
163+
* @param string $path
164+
* @return bool
165+
*/
166+
protected function createMissingSqliteDatbase($path)
167+
{
168+
if ($this->option('force')) {
169+
return touch($path);
170+
}
171+
172+
if ($this->option('no-interaction')) {
173+
return false;
174+
}
175+
176+
$this->components->warn('The SQLite database does not exist: '.$path);
177+
178+
if (! $this->components->confirm('Would you like to create it?')) {
179+
return false;
180+
}
181+
182+
return touch($path);
183+
}
184+
185+
/**
186+
* Create a missing MySQL database.
187+
*
188+
* @return bool
189+
*/
190+
protected function createMissingMysqlDatabase($connection)
191+
{
192+
if ($this->laravel['config']->get("database.connections.{$connection->getName()}.database") !== $connection->getDatabaseName()) {
193+
return false;
194+
}
195+
196+
if (! $this->option('force') && $this->option('no-interaction')) {
197+
return false;
198+
}
199+
200+
if (! $this->option('force') && ! $this->option('no-interaction')) {
201+
$this->components->warn("The database '{$connection->getDatabaseName()}' does not exist on the '{$connection->getName()}' connection.");
150202

151203
if (! $this->components->confirm('Would you like to create it?')) {
152204
return false;
153205
}
206+
}
154207

155-
return touch($e->getPrevious()->path);
156-
});
208+
try {
209+
$this->laravel['config']->set("database.connections.{$connection->getName()}.database", null);
210+
211+
$this->laravel['db']->purge();
212+
213+
$freshConnection = $this->migrator->resolveConnection($this->option('database'));
214+
215+
return tap($freshConnection->unprepared("CREATE DATABASE IF NOT EXISTS {$connection->getDatabaseName()}"), function () {
216+
$this->laravel['db']->purge();
217+
});
218+
} finally {
219+
$this->laravel['config']->set("database.connections.{$connection->getName()}.database", $connection->getDatabaseName());
220+
}
157221
}
158222

159223
/**

0 commit comments

Comments
 (0)