Skip to content

Commit a5bfc4b

Browse files
Add sqlite schema dump support
1 parent 257b95c commit a5bfc4b

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ protected function loadSchemaState()
127127
// First, we will make sure that the connection supports schema loading and that
128128
// the schema file exists before we proceed any further. If not, we will just
129129
// continue with the standard migration operation as normal without errors.
130-
if ($connection instanceof SQLiteConnection ||
131-
$connection instanceof SqlServerConnection ||
130+
if ($connection instanceof SqlServerConnection ||
132131
! is_file($path = $this->schemaPath($connection))) {
133132
return;
134133
}

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Query\Processors\SQLiteProcessor;
88
use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SchemaGrammar;
99
use Illuminate\Database\Schema\SQLiteBuilder;
10+
use Illuminate\Database\Schema\SqliteSchemaState;
1011
use Illuminate\Filesystem\Filesystem;
1112
use RuntimeException;
1213

@@ -80,7 +81,7 @@ protected function getDefaultSchemaGrammar()
8081
*/
8182
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
8283
{
83-
throw new RuntimeException('Schema dumping is not supported when using SQLite.');
84+
return new SqliteSchemaState($this, $files, $processFactory);
8485
}
8586

8687
/**
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Schema;
4+
5+
use Exception;
6+
use Illuminate\Support\Str;
7+
use Symfony\Component\Process\Process;
8+
9+
class SqliteSchemaState extends SchemaState
10+
{
11+
/**
12+
* Dump the database's schema into a file.
13+
*
14+
* @param string $path
15+
*
16+
* @return void
17+
*/
18+
public function dump($path)
19+
{
20+
with($process = $this->makeProcess(
21+
$this->baseCommand().' .schema'
22+
))->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
23+
//
24+
]));
25+
26+
$migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) {
27+
return stripos($line, 'sqlite_sequence') === false &&
28+
strlen($line) > 0;
29+
})->all();
30+
31+
$this->files->put($path, implode(PHP_EOL, $migrations).PHP_EOL);
32+
33+
$this->appendMigrationData($path);
34+
}
35+
36+
/**
37+
* Append the migration data to the schema dump.
38+
*
39+
* @return void
40+
*/
41+
protected function appendMigrationData(string $path)
42+
{
43+
with($process = $this->makeProcess(
44+
$this->baseCommand().' ".dump \'migrations\'"'
45+
))->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
46+
//
47+
]));
48+
49+
$migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) {
50+
return preg_match('/^\s*(--|INSERT\s)/iu', $line) === 1 &&
51+
strlen($line) > 0;
52+
})->all();
53+
54+
$this->files->append($path, implode(PHP_EOL, $migrations).PHP_EOL);
55+
}
56+
57+
/**
58+
* Load the given schema file into the database.
59+
*
60+
* @param string $path
61+
*
62+
* @return void
63+
*/
64+
public function load($path)
65+
{
66+
$process = $this->makeProcess($this->baseCommand().' < $LARAVEL_LOAD_PATH');
67+
68+
$process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
69+
'LARAVEL_LOAD_PATH' => $path,
70+
]));
71+
}
72+
73+
/**
74+
* Get the base sqlite command arguments as a string.
75+
*
76+
* @return string
77+
*/
78+
protected function baseCommand()
79+
{
80+
return 'sqlite3 $LARAVEL_LOAD_DATABASE';
81+
}
82+
83+
/**
84+
* Get the base variables for a dump / load command.
85+
*
86+
* @return array
87+
*/
88+
protected function baseVariables(array $config)
89+
{
90+
return [
91+
'LARAVEL_LOAD_DATABASE' => $config['database'],
92+
];
93+
}
94+
}

0 commit comments

Comments
 (0)