diff --git a/src/Illuminate/Database/Console/Migrations/MigrateCommand.php b/src/Illuminate/Database/Console/Migrations/MigrateCommand.php index f547d8d5e80e..e18835301687 100755 --- a/src/Illuminate/Database/Console/Migrations/MigrateCommand.php +++ b/src/Illuminate/Database/Console/Migrations/MigrateCommand.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\SchemaLoaded; use Illuminate\Database\Migrations\Migrator; -use Illuminate\Database\SQLiteConnection; use Illuminate\Database\SqlServerConnection; class MigrateCommand extends BaseCommand @@ -127,8 +126,7 @@ protected function loadSchemaState() // First, we will make sure that the connection supports schema loading and that // the schema file exists before we proceed any further. If not, we will just // continue with the standard migration operation as normal without errors. - if ($connection instanceof SQLiteConnection || - $connection instanceof SqlServerConnection || + if ($connection instanceof SqlServerConnection || ! is_file($path = $this->schemaPath($connection))) { return; } diff --git a/src/Illuminate/Database/SQLiteConnection.php b/src/Illuminate/Database/SQLiteConnection.php index 06d7fbf73ad1..e647b13a625c 100755 --- a/src/Illuminate/Database/SQLiteConnection.php +++ b/src/Illuminate/Database/SQLiteConnection.php @@ -7,8 +7,8 @@ use Illuminate\Database\Query\Processors\SQLiteProcessor; use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SchemaGrammar; use Illuminate\Database\Schema\SQLiteBuilder; +use Illuminate\Database\Schema\SqliteSchemaState; use Illuminate\Filesystem\Filesystem; -use RuntimeException; class SQLiteConnection extends Connection { @@ -80,7 +80,7 @@ protected function getDefaultSchemaGrammar() */ public function getSchemaState(Filesystem $files = null, callable $processFactory = null) { - throw new RuntimeException('Schema dumping is not supported when using SQLite.'); + return new SqliteSchemaState($this, $files, $processFactory); } /** diff --git a/src/Illuminate/Database/Schema/SqliteSchemaState.php b/src/Illuminate/Database/Schema/SqliteSchemaState.php new file mode 100644 index 000000000000..773affb2ccf2 --- /dev/null +++ b/src/Illuminate/Database/Schema/SqliteSchemaState.php @@ -0,0 +1,90 @@ +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'], + ]; + } +}