-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'add_sqlite_schema_dump' into 8.x
- Loading branch information
Showing
3 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Schema; | ||
|
||
class SqliteSchemaState extends SchemaState | ||
{ | ||
/** | ||
* Dump the database's schema into a file. | ||
* | ||
* @param string $path | ||
* | ||
* @return void | ||
*/ | ||
public function dump($path) | ||
{ | ||
with($process = $this->makeProcess( | ||
$this->baseCommand().' .schema' | ||
))->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ | ||
// | ||
])); | ||
|
||
$migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) { | ||
return stripos($line, 'sqlite_sequence') === false && | ||
strlen($line) > 0; | ||
})->all(); | ||
|
||
$this->files->put($path, implode(PHP_EOL, $migrations).PHP_EOL); | ||
|
||
$this->appendMigrationData($path); | ||
} | ||
|
||
/** | ||
* Append the migration data to the schema dump. | ||
* | ||
* @return void | ||
*/ | ||
protected function appendMigrationData(string $path) | ||
{ | ||
with($process = $this->makeProcess( | ||
$this->baseCommand().' ".dump \'migrations\'"' | ||
))->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ | ||
// | ||
])); | ||
|
||
$migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) { | ||
return preg_match('/^\s*(--|INSERT\s)/iu', $line) === 1 && | ||
strlen($line) > 0; | ||
})->all(); | ||
|
||
$this->files->append($path, implode(PHP_EOL, $migrations).PHP_EOL); | ||
} | ||
|
||
/** | ||
* Load the given schema file into the database. | ||
* | ||
* @param string $path | ||
* | ||
* @return void | ||
*/ | ||
public function load($path) | ||
{ | ||
$process = $this->makeProcess($this->baseCommand().' < $LARAVEL_LOAD_PATH'); | ||
|
||
$process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ | ||
'LARAVEL_LOAD_PATH' => $path, | ||
])); | ||
} | ||
|
||
/** | ||
* Get the base sqlite command arguments as a string. | ||
* | ||
* @return string | ||
*/ | ||
protected function baseCommand() | ||
{ | ||
return 'sqlite3 $LARAVEL_LOAD_DATABASE'; | ||
} | ||
|
||
/** | ||
* Get the base variables for a dump / load command. | ||
* | ||
* @return array | ||
*/ | ||
protected function baseVariables(array $config) | ||
{ | ||
return [ | ||
'LARAVEL_LOAD_DATABASE' => $config['database'], | ||
]; | ||
} | ||
} |