Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Prompt to create MySQL db when migrating #44153

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you create a MySQLDatabaseDoesNotExistException, instead of this if with three conditions (like SQLiteDatabaseDoesNotExistException on L.140)?

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