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] Fix Postgres Dump #35018

Merged
merged 1 commit into from
Oct 29, 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
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Console/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class DumpCommand extends Command
*/
public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher)
{
$this->schemaState(
$connection = $connections->connection($database = $this->input->getOption('database'))
)->dump($path = $this->path($connection));
$connection = $connections->connection($database = $this->input->getOption('database'));

$this->schemaState($connection)->dump(
$connection, $path = $this->path($connection)
);

$dispatcher->dispatch(new SchemaDumped($connection, $path));

Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Schema/MySqlSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Schema;

use Exception;
use Illuminate\Database\Connection;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;

Expand All @@ -11,10 +12,11 @@ class MySqlSchemaState extends SchemaState
/**
* Dump the database's schema into a file.
*
* @param \Illuminate\Database\Connection $connection
* @param string $path
* @return void
*/
public function dump($path)
public function dump(Connection $connection, $path)
{
$this->executeDumpProcess($this->makeProcess(
$this->baseDumpCommand().' --routines --result-file="${:LARAVEL_LOAD_PATH}" --no-data'
Expand Down
38 changes: 12 additions & 26 deletions src/Illuminate/Database/Schema/PostgresSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,33 @@

namespace Illuminate\Database\Schema;

use Illuminate\Database\Connection;
use Illuminate\Support\Str;

class PostgresSchemaState extends SchemaState
{
/**
* Dump the database's schema into a file.
*
* @param \Illuminate\Database\Connection $connection
* @param string $path
* @return void
*/
public function dump($path)
public function dump(Connection $connection, $path)
{
$excludedTables = collect($connection->getSchemaBuilder()->getAllTables())
->map->tablename
->reject(function ($table) {
return $table === 'migrations';
})->map(function ($table) {
return '--exclude-table-data='.$table;
})->implode(' ');

$this->makeProcess(
$this->baseDumpCommand().' --no-owner --file=$LARAVEL_LOAD_PATH --schema-only'
$this->baseDumpCommand().' --no-owner --file=$LARAVEL_LOAD_PATH '.$excludedTables
)->mustRun($this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
'LARAVEL_LOAD_PATH' => $path,
]));

$this->appendMigrationData($path);
}

/**
* Append the migration data to the schema dump.
*
* @param string $path
* @return void
*/
protected function appendMigrationData(string $path)
{
with($process = $this->makeProcess(
$this->baseDumpCommand().' --table=migrations --data-only --inserts'
))->setTimeout(null)->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*(--|SELECT\s|SET\s)/iu', $line) === 0 &&
strlen($line) > 0;
})->all();

$this->files->append($path, implode(PHP_EOL, $migrations).PHP_EOL);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Database/Schema/SchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public function __construct(Connection $connection, Filesystem $files = null, ca
/**
* Dump the database's schema into a file.
*
* @param \Illuminate\Database\Connection $connection
* @param string $path
* @return void
*/
abstract public function dump($path);
abstract public function dump(Connection $connection, $path);

/**
* Load the given schema file into the database.
Expand Down
11 changes: 6 additions & 5 deletions src/Illuminate/Database/Schema/SqliteSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace Illuminate\Database\Schema;

use Illuminate\Database\Connection;

class SqliteSchemaState extends SchemaState
{
/**
* Dump the database's schema into a file.
*
* @param string $path
*
* @param \Illuminate\Database\Connection
* @param string $path
* @return void
*/
public function dump($path)
public function dump(Connection $connection, $path)
{
with($process = $this->makeProcess(
$this->baseCommand().' .schema'
Expand Down Expand Up @@ -53,8 +55,7 @@ protected function appendMigrationData(string $path)
/**
* Load the given schema file into the database.
*
* @param string $path
*
* @param string $path
* @return void
*/
public function load($path)
Expand Down