Skip to content

Commit

Permalink
[9.x] Prompt to create sqlite db when migrating (#43867)
Browse files Browse the repository at this point in the history
* prompt to create SQLite database if it does not yet exist

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
timacdonald and taylorotwell authored Aug 26, 2022
1 parent 0d936a9 commit c5772dd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Connectors/SQLiteConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Database\Connectors;

use InvalidArgumentException;
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;

class SQLiteConnector extends Connector implements ConnectorInterface
{
Expand All @@ -12,7 +12,7 @@ class SQLiteConnector extends Connector implements ConnectorInterface
* @param array $config
* @return \PDO
*
* @throws \InvalidArgumentException
* @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException
*/
public function connect(array $config)
{
Expand All @@ -31,7 +31,7 @@ public function connect(array $config)
// as the developer probably wants to know if the database exists and this
// SQLite driver will not throw any exception if it does not by default.
if ($path === false) {
throw new InvalidArgumentException("Database ({$config['database']}) does not exist.");
throw new SQLiteDatabaseDoesNotExistException($config['database']);
}

return $this->createConnection("sqlite:{$path}", $config, $options);
Expand Down
33 changes: 32 additions & 1 deletion src/Illuminate/Database/Console/Migrations/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\SchemaLoaded;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
use Illuminate\Database\SqlServerConnection;

class MigrateCommand extends BaseCommand
Expand Down Expand Up @@ -108,7 +109,7 @@ public function handle()
*/
protected function prepareDatabase()
{
if (! $this->migrator->repositoryExists()) {
if (! $this->repositoryExists()) {
$this->components->info('Preparing database.');

$this->components->task('Creating migration table', function () {
Expand All @@ -125,6 +126,36 @@ protected function prepareDatabase()
}
}

/**
* Determine if the migrator repository exists.
*
* @return bool
*/
protected function repositoryExists()
{
return retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) {
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
return false;
}

if ($this->option('force')) {
return touch($e->getPrevious()->path);
}

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

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

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

return touch($e->getPrevious()->path);
});
}

/**
* Load the schema state to seed the initial database schema structure.
*
Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/Database/SQLiteDatabaseDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Illuminate\Database;

use InvalidArgumentException;

class SQLiteDatabaseDoesNotExistException extends InvalidArgumentException
{
/**
* The path to the database.
*
* @var string
*/
public $path;

/**
* Create a new exception instance.
*
* @param string $path
* @return void
*/
public function __construct($path)
{
parent::__construct("Database ({$path}) does not exist.");

$this->path = $path;
}
}

0 comments on commit c5772dd

Please sign in to comment.