Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add SQLite schema dump support #34323

Merged
merged 2 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
];
}
}