diff --git a/src/Illuminate/Database/Console/DbDumpCommand.php b/src/Illuminate/Database/Console/DbDumpCommand.php new file mode 100644 index 000000000000..1f4d9f8d4a1c --- /dev/null +++ b/src/Illuminate/Database/Console/DbDumpCommand.php @@ -0,0 +1,82 @@ +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)); + }); + } +} diff --git a/src/Illuminate/Database/Events/DatabaseDumped.php b/src/Illuminate/Database/Events/DatabaseDumped.php new file mode 100644 index 000000000000..7dd6c0780e41 --- /dev/null +++ b/src/Illuminate/Database/Events/DatabaseDumped.php @@ -0,0 +1,40 @@ +connection = $connection; + $this->connectionName = $connection->getName(); + $this->path = $path; + } +} diff --git a/src/Illuminate/Database/Schema/MySqlSchemaState.php b/src/Illuminate/Database/Schema/MySqlSchemaState.php index 427c943ff736..faa7f848e152 100644 --- a/src/Illuminate/Database/Schema/MySqlSchemaState.php +++ b/src/Illuminate/Database/Schema/MySqlSchemaState.php @@ -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); + } } } diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index d2ae86019342..37d7494b0e8a 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -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; @@ -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,