Skip to content

Commit 35ee745

Browse files
committed
Merge branch 'add_sqlite_schema_dump' into 8.x
2 parents 2dbdac5 + 9c61a90 commit 35ee745

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Database\Events\SchemaLoaded;
88
use Illuminate\Database\Migrations\Migrator;
9-
use Illuminate\Database\SQLiteConnection;
109
use Illuminate\Database\SqlServerConnection;
1110

1211
class MigrateCommand extends BaseCommand
@@ -127,8 +126,7 @@ protected function loadSchemaState()
127126
// First, we will make sure that the connection supports schema loading and that
128127
// the schema file exists before we proceed any further. If not, we will just
129128
// continue with the standard migration operation as normal without errors.
130-
if ($connection instanceof SQLiteConnection ||
131-
$connection instanceof SqlServerConnection ||
129+
if ($connection instanceof SqlServerConnection ||
132130
! is_file($path = $this->schemaPath($connection))) {
133131
return;
134132
}

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
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;
11-
use RuntimeException;
1212

1313
class SQLiteConnection extends Connection
1414
{
@@ -80,7 +80,7 @@ protected function getDefaultSchemaGrammar()
8080
*/
8181
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
8282
{
83-
throw new RuntimeException('Schema dumping is not supported when using SQLite.');
83+
return new SqliteSchemaState($this, $files, $processFactory);
8484
}
8585

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

0 commit comments

Comments
 (0)