Skip to content
Draft
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
83 changes: 83 additions & 0 deletions src/Illuminate/Database/Console/DbDumpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Illuminate\Database\Console;

use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Connection;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Events\DatabaseDumped;
use Illuminate\Database\Events\MigrationsPruned;
use Illuminate\Database\Events\SchemaDumped;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Config;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'db:dump')]
class DbDumpCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'db:dump
{--database= : The database connection to use}
{--path= : The path where the schema dump file should be stored}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Dump the given database schema and data';

/**
* Execute the console command.
*
* @param \Illuminate\Database\ConnectionResolverInterface $connections
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher)
{
$connection = $connections->connection($database = $this->input->getOption('database'));

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

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

$info = 'Database dumped';

$this->components->info($info.' successfully to ' . $path);
}

/**
* Create a schema state instance for the given connection.
*
* @param \Illuminate\Database\Connection $connection
* @return mixed
*/
protected function schemaState(Connection $connection)
{
return $connection->getSchemaState()
->withData()
->handleOutputUsing(function ($type, $buffer) {
$this->output->write($buffer);
});
}

/**
* Get the path that the dump should be written to.
*
* @param \Illuminate\Database\Connection $connection
*/
protected function path(Connection $connection)
{
return tap($this->option('path') ?: storage_path($connection->getName() . '-' . date("Ymdhis").'.sql'), function ($path) {
(new Filesystem)->ensureDirectoryExists(dirname($path));
});
}
}
86 changes: 86 additions & 0 deletions src/Illuminate/Database/Console/DbLoadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Illuminate\Database\Console;

use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Console\Prohibitable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Connection;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Events\DatabaseLoaded;
use Illuminate\Database\Events\MigrationsPruned;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Config;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'db:load')]
class DbLoadCommand extends Command
{
use ConfirmableTrait, Prohibitable;

/**
* The console command name.
*
* @var string
*/
protected $signature = 'db:load {path}
{--database= : The database connection to use}
{--force : Force the operation to run when in production}
{--drop : Drop the database before loading the dump}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Load the databse dump into the give database';

/**
* Execute the console command.
*
* @param \Illuminate\Database\ConnectionResolverInterface $connections
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher)
{
if ($this->isProhibited() ||
! $this->confirmToProceed()) {
return Command::FAILURE;
}

$connection = $connections->connection($database = $this->input->getOption('database'));

$path = $this->argument('path');

if ($this->input->getOption('drop')) {
$this->call('db:wipe', array_filter([
'--database' => $database,
'--force' => true,
]));
}

$this->schemaState($connection)->load($path);

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

$info = 'Database loaded';

$this->components->info($info.' successfully in database ' . $database);
}

/**
* Create a schema state instance for the given connection.
*
* @param \Illuminate\Database\Connection $connection
* @return mixed
*/
protected function schemaState(Connection $connection)
{
return $connection->getSchemaState()
->handleOutputUsing(function ($type, $buffer) {
$this->output->write($buffer);
});
}
}
40 changes: 40 additions & 0 deletions src/Illuminate/Database/Events/DatabaseDumped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Database\Events;

class DatabaseDumped
{
/**
* The database connection instance.
*
* @var \Illuminate\Database\Connection
*/
public $connection;

/**
* The database connection name.
*
* @var string
*/
public $connectionName;

/**
* The path to the schema dump.
*
* @var string
*/
public $path;

/**
* Create a new event instance.
*
* @param \Illuminate\Database\Connection $connection
* @param string $path
*/
public function __construct($connection, $path)
{
$this->connection = $connection;
$this->connectionName = $connection->getName();
$this->path = $path;
}
}
40 changes: 40 additions & 0 deletions src/Illuminate/Database/Events/DatabaseLoaded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Database\Events;

class DatabaseLoaded
{
/**
* The database connection instance.
*
* @var \Illuminate\Database\Connection
*/
public $connection;

/**
* The database connection name.
*
* @var string
*/
public $connectionName;

/**
* The path to the schema dump.
*
* @var string
*/
public $path;

/**
* Create a new event instance.
*
* @param \Illuminate\Database\Connection $connection
* @param string $path
*/
public function __construct($connection, $path)
{
$this->connection = $connection;
$this->connectionName = $connection->getName();
$this->path = $path;
}
}
19 changes: 13 additions & 6 deletions src/Illuminate/Database/Schema/MySqlSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ class MySqlSchemaState extends SchemaState
*/
public function dump(Connection $connection, $path)
{
$this->executeDumpProcess($this->makeProcess(
$this->baseDumpCommand().' --routines --result-file="${:LARAVEL_LOAD_PATH}" --no-data'
), $this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
$dumpCommand = $this->baseDumpCommand().' --routines --result-file="${:LARAVEL_LOAD_PATH}"';

if ($this->hasData()) {
$dumpCommand .= ' --single-transaction --quick';
} else {
$dumpCommand .= ' --no-data';
}
$this->executeDumpProcess($this->makeProcess($dumpCommand), $this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
'LARAVEL_LOAD_PATH' => $path,
]));

$this->removeAutoIncrementingState($path);
if (!$this->hasData()) {
$this->removeAutoIncrementingState($path);

if ($this->hasMigrationTable()) {
$this->appendMigrationData($path);
if ($this->hasMigrationTable()) {
$this->appendMigrationData($path);
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/Illuminate/Database/Schema/SchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ abstract class SchemaState
*/
protected $migrationTable = 'migrations';

/**
* Indicates if the dumper should include data.
*
* @var bool
*/
protected $data = false;

/**
* The process factory callback.
*
Expand Down Expand Up @@ -126,6 +133,29 @@ public function withMigrationTable(string $table)
return $this;
}

/**
* Check if the dumper should include data.
*
* @return bool
*/
public function hasData()
{
return $this->data;
}

/**
* Indicate that the dumper should include data.
*
* @param bool $data
* @return $this
*/
public function withData(bool $data = true)
{
$this->data = $data;

return $this;
}

/**
* Specify the callback that should be used to handle process output.
*
Expand Down
19 changes: 14 additions & 5 deletions src/Illuminate/Database/Schema/SqliteSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@ class SqliteSchemaState extends SchemaState
*/
public function dump(Connection $connection, $path)
{
$process = $this->makeProcess($this->baseCommand().' ".schema --indent"')
if ($this->hasData()) {
$dumpCommand = $this->baseCommand().' -batch ".dump"';
} else {
$dumpCommand = $this->baseCommand().' ".schema --indent"';
}
$process = $this->makeProcess($dumpCommand)
->setTimeout(null)
->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
//
]));

$migrations = preg_replace('/CREATE TABLE sqlite_.+?\);[\r\n]+/is', '', $process->getOutput());
if (!$this->hasData()) {
$migrations = preg_replace('/CREATE TABLE sqlite_.+?\);[\r\n]+/is', '', $process->getOutput());

$this->files->put($path, $migrations.PHP_EOL);
$this->files->put($path, $migrations.PHP_EOL);

if ($this->hasMigrationTable()) {
$this->appendMigrationData($path);
if ($this->hasMigrationTable()) {
$this->appendMigrationData($path);
}
} else {
$this->files->put($path, $process->getOutput());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Illuminate\Console\Signals;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Database\Console\DbCommand;
use Illuminate\Database\Console\DbDumpCommand;
use Illuminate\Database\Console\DbLoadCommand;
use Illuminate\Database\Console\DumpCommand;
use Illuminate\Database\Console\Factories\FactoryMakeCommand;
use Illuminate\Database\Console\MonitorCommand as DatabaseMonitorCommand;
Expand Down Expand Up @@ -126,6 +128,8 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'ConfigClear' => ConfigClearCommand::class,
'ConfigShow' => ConfigShowCommand::class,
'Db' => DbCommand::class,
'DbDump' => DbDumpCommand::class,
'DbLoad' => DbLoadCommand::class,
'DbMonitor' => DatabaseMonitorCommand::class,
'DbPrune' => PruneCommand::class,
'DbShow' => ShowCommand::class,
Expand Down
Loading