|
| 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