Skip to content

Commit

Permalink
Merge branch 'add_sqlite_schema_dump' into 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 16, 2020
2 parents 2dbdac5 + 9c61a90 commit 35ee745
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/SQLiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down
90 changes: 90 additions & 0 deletions src/Illuminate/Database/Schema/SqliteSchemaState.php
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'],
];
}
}

0 comments on commit 35ee745

Please sign in to comment.