DumpCommand.php 2.9 KB

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