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