Factories
2 years ago
Migrations
2 years ago
Seeds
2 years ago
DbCommand.php
2 years ago
DumpCommand.php
2 years ago
PruneCommand.php
2 years ago
WipeCommand.php
2 years ago
DumpCommand.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\Illuminate\Database\Console; |
| 4 | |
| 5 | use IAWP_SCOPED\Illuminate\Console\Command; |
| 6 | use IAWP_SCOPED\Illuminate\Contracts\Events\Dispatcher; |
| 7 | use IAWP_SCOPED\Illuminate\Database\Connection; |
| 8 | use IAWP_SCOPED\Illuminate\Database\ConnectionResolverInterface; |
| 9 | use IAWP_SCOPED\Illuminate\Database\Events\SchemaDumped; |
| 10 | use IAWP_SCOPED\Illuminate\Filesystem\Filesystem; |
| 11 | use IAWP_SCOPED\Illuminate\Support\Facades\Config; |
| 12 | /** @internal */ |
| 13 | class DumpCommand extends Command |
| 14 | { |
| 15 | /** |
| 16 | * The console command name. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $signature = 'schema:dump |
| 21 | {--database= : The database connection to use} |
| 22 | {--path= : The path where the schema dump file should be stored} |
| 23 | {--prune : Delete all existing migration files}'; |
| 24 | /** |
| 25 | * The console command description. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $description = 'Dump the given database schema'; |
| 30 | /** |
| 31 | * Execute the console command. |
| 32 | * |
| 33 | * @param \Illuminate\Database\ConnectionResolverInterface $connections |
| 34 | * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher |
| 35 | * @return int |
| 36 | */ |
| 37 | public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher) |
| 38 | { |
| 39 | $connection = $connections->connection($database = $this->input->getOption('database')); |
| 40 | $this->schemaState($connection)->dump($connection, $path = $this->path($connection)); |
| 41 | $dispatcher->dispatch(new SchemaDumped($connection, $path)); |
| 42 | $this->info('Database schema dumped successfully.'); |
| 43 | if ($this->option('prune')) { |
| 44 | (new Filesystem())->deleteDirectory(database_path('migrations'), $preserve = \false); |
| 45 | $this->info('Migrations pruned successfully.'); |
| 46 | } |
| 47 | } |
| 48 | /** |
| 49 | * Create a schema state instance for the given connection. |
| 50 | * |
| 51 | * @param \Illuminate\Database\Connection $connection |
| 52 | * @return mixed |
| 53 | */ |
| 54 | protected function schemaState(Connection $connection) |
| 55 | { |
| 56 | return $connection->getSchemaState()->withMigrationTable($connection->getTablePrefix() . Config::get('database.migrations', 'migrations'))->handleOutputUsing(function ($type, $buffer) { |
| 57 | $this->output->write($buffer); |
| 58 | }); |
| 59 | } |
| 60 | /** |
| 61 | * Get the path that the dump should be written to. |
| 62 | * |
| 63 | * @param \Illuminate\Database\Connection $connection |
| 64 | */ |
| 65 | protected function path(Connection $connection) |
| 66 | { |
| 67 | return tap($this->option('path') ?: database_path('schema/' . $connection->getName() . '-schema.dump'), function ($path) { |
| 68 | (new Filesystem())->ensureDirectoryExists(\dirname($path)); |
| 69 | }); |
| 70 | } |
| 71 | } |
| 72 |