Skip to content

Commit c5772dd

Browse files
[9.x] Prompt to create sqlite db when migrating (#43867)
* prompt to create SQLite database if it does not yet exist * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0d936a9 commit c5772dd

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-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: 32 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,7 @@ public function handle()
108109
*/
109110
protected function prepareDatabase()
110111
{
111-
if (! $this->migrator->repositoryExists()) {
112+
if (! $this->repositoryExists()) {
112113
$this->components->info('Preparing database.');
113114

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

129+
/**
130+
* Determine if the migrator repository exists.
131+
*
132+
* @return bool
133+
*/
134+
protected function repositoryExists()
135+
{
136+
return retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) {
137+
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
138+
return false;
139+
}
140+
141+
if ($this->option('force')) {
142+
return touch($e->getPrevious()->path);
143+
}
144+
145+
if ($this->option('no-interaction')) {
146+
return false;
147+
}
148+
149+
$this->components->warn('The SQLite database does not exist: '.$e->getPrevious()->path);
150+
151+
if (! $this->components->confirm('Would you like to create it?')) {
152+
return false;
153+
}
154+
155+
return touch($e->getPrevious()->path);
156+
});
157+
}
158+
128159
/**
129160
* Load the schema state to seed the initial database schema structure.
130161
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 exception instance.
18+
*
19+
* @param string $path
20+
* @return void
21+
*/
22+
public function __construct($path)
23+
{
24+
parent::__construct("Database ({$path}) does not exist.");
25+
26+
$this->path = $path;
27+
}
28+
}

0 commit comments

Comments
 (0)