Skip to content

Commit

Permalink
[9.x] Prompt to create MySQL db when migrating (#44153)
Browse files Browse the repository at this point in the history
* create missing MySQL database

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
timacdonald and taylorotwell authored Sep 16, 2022
1 parent b088269 commit 85ca3fb
Showing 1 changed file with 74 additions and 10 deletions.
84 changes: 74 additions & 10 deletions src/Illuminate/Database/Console/Migrations/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
use Illuminate\Database\SqlServerConnection;
use PDOException;
use Throwable;

class MigrateCommand extends BaseCommand
{
Expand Down Expand Up @@ -134,26 +136,88 @@ protected function prepareDatabase()
protected function repositoryExists()
{
return retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) {
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
return false;
}
try {
if ($e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
return $this->createMissingSqliteDatbase($e->getPrevious()->path);
}

if ($this->option('force')) {
return touch($e->getPrevious()->path);
}
$connection = $this->migrator->resolveConnection($this->option('database'));

if ($this->option('no-interaction')) {
if (
$e->getPrevious() instanceof PDOException &&
$e->getPrevious()->getCode() === 1049 &&
$connection->getDriverName() === 'mysql') {
return $this->createMissingMysqlDatabase($connection);
}

return false;
} catch (Throwable) {
return false;
}
});
}

$this->components->warn('The SQLite database does not exist: '.$e->getPrevious()->path);
/**
* Create a missing SQLite database.
*
* @param string $path
* @return bool
*/
protected function createMissingSqliteDatbase($path)
{
if ($this->option('force')) {
return touch($path);
}

if ($this->option('no-interaction')) {
return false;
}

$this->components->warn('The SQLite database does not exist: '.$path);

if (! $this->components->confirm('Would you like to create it?')) {
return false;
}

return touch($path);
}

/**
* Create a missing MySQL database.
*
* @return bool
*/
protected function createMissingMysqlDatabase($connection)
{
if ($this->laravel['config']->get("database.connections.{$connection->getName()}.database") !== $connection->getDatabaseName()) {
return false;
}

if (! $this->option('force') && $this->option('no-interaction')) {
return false;
}

if (! $this->option('force') && ! $this->option('no-interaction')) {
$this->components->warn("The database '{$connection->getDatabaseName()}' does not exist on the '{$connection->getName()}' connection.");

if (! $this->components->confirm('Would you like to create it?')) {
return false;
}
}

return touch($e->getPrevious()->path);
});
try {
$this->laravel['config']->set("database.connections.{$connection->getName()}.database", null);

$this->laravel['db']->purge();

$freshConnection = $this->migrator->resolveConnection($this->option('database'));

return tap($freshConnection->unprepared("CREATE DATABASE IF NOT EXISTS {$connection->getDatabaseName()}"), function () {
$this->laravel['db']->purge();
});
} finally {
$this->laravel['config']->set("database.connections.{$connection->getName()}.database", $connection->getDatabaseName());
}
}

/**
Expand Down

0 comments on commit 85ca3fb

Please sign in to comment.