Skip to content
Merged
Show file tree
Hide file tree
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
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;
}
}