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
82 changes: 82 additions & 0 deletions src/Illuminate/Database/Console/DbDumpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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), true
);

$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()
->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));
});
}
}
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;
}
}
12 changes: 7 additions & 5 deletions src/Illuminate/Database/Schema/MySqlSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ class MySqlSchemaState extends SchemaState
* @param string $path
* @return void
*/
public function dump(Connection $connection, $path)
public function dump(Connection $connection, $path, $withData = false)
{
$this->executeDumpProcess($this->makeProcess(
$this->baseDumpCommand().' --routines --result-file="${:LARAVEL_LOAD_PATH}" --no-data'
$this->baseDumpCommand().' --routines --result-file="${:LARAVEL_LOAD_PATH}" ' . ($withData ? '' : '--no-data')
), $this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
'LARAVEL_LOAD_PATH' => $path,
]));

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

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

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