Skip to content

Commit daf50fe

Browse files
committed
prompt to create SQLite database if it does not yet exist
1 parent d23de5f commit daf50fe

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/Illuminate/Database/Connectors/SQLiteConnector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Illuminate\Database\Connectors;
44

5-
use InvalidArgumentException;
5+
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
66

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

3737
return $this->createConnection("sqlite:{$path}", $config, $options);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Events\Dispatcher;
88
use Illuminate\Database\Events\SchemaLoaded;
99
use Illuminate\Database\Migrations\Migrator;
10+
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
1011
use Illuminate\Database\SqlServerConnection;
1112

1213
class MigrateCommand extends BaseCommand
@@ -108,7 +109,21 @@ public function handle()
108109
*/
109110
protected function prepareDatabase()
110111
{
111-
if (! $this->migrator->repositoryExists()) {
112+
$repositoryExists = retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) {
113+
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
114+
return false;
115+
}
116+
117+
$this->components->warn('The SQLite database does yet exist: '.$e->getPrevious()->path);
118+
119+
if (! $this->components->confirm('Would you like to attempt to create it?')) {
120+
return false;
121+
}
122+
123+
return touch($e->getPrevious()->path);
124+
});
125+
126+
if (! $repositoryExists) {
112127
$this->components->info('Preparing database.');
113128

114129
$this->components->task('Creating migration table', function () {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Illuminate\Database;
4+
5+
use InvalidArgumentException;
6+
7+
class SQLiteDatabaseDoesNotExistException extends InvalidArgumentException
8+
{
9+
/**
10+
* The path to the database.
11+
*
12+
* @var string
13+
*/
14+
public $path;
15+
16+
/**
17+
* Create a new instance.
18+
*
19+
* @param string $path
20+
*/
21+
public function __construct($path)
22+
{
23+
parent::__construct("Database ({$path}) does not exist.");
24+
25+
$this->path = $path;
26+
}
27+
}

0 commit comments

Comments
 (0)