WipeCommand.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Illuminate\Database\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\ConfirmableTrait;
  5. use Illuminate\Console\Prohibitable;
  6. use Symfony\Component\Console\Attribute\AsCommand;
  7. use Symfony\Component\Console\Input\InputOption;
  8. #[AsCommand(name: 'db:wipe')]
  9. class WipeCommand extends Command
  10. {
  11. use ConfirmableTrait, Prohibitable;
  12. /**
  13. * The console command name.
  14. *
  15. * @var string
  16. */
  17. protected $name = 'db:wipe';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Drop all tables, views, and types';
  24. /**
  25. * Execute the console command.
  26. *
  27. * @return int
  28. */
  29. public function handle()
  30. {
  31. if ($this->isProhibited() ||
  32. ! $this->confirmToProceed()) {
  33. return 1;
  34. }
  35. $database = $this->input->getOption('database');
  36. if ($this->option('drop-views')) {
  37. $this->dropAllViews($database);
  38. $this->components->info('Dropped all views successfully.');
  39. }
  40. $this->dropAllTables($database);
  41. $this->components->info('Dropped all tables successfully.');
  42. if ($this->option('drop-types')) {
  43. $this->dropAllTypes($database);
  44. $this->components->info('Dropped all types successfully.');
  45. }
  46. return 0;
  47. }
  48. /**
  49. * Drop all of the database tables.
  50. *
  51. * @param string $database
  52. * @return void
  53. */
  54. protected function dropAllTables($database)
  55. {
  56. $this->laravel['db']->connection($database)
  57. ->getSchemaBuilder()
  58. ->dropAllTables();
  59. }
  60. /**
  61. * Drop all of the database views.
  62. *
  63. * @param string $database
  64. * @return void
  65. */
  66. protected function dropAllViews($database)
  67. {
  68. $this->laravel['db']->connection($database)
  69. ->getSchemaBuilder()
  70. ->dropAllViews();
  71. }
  72. /**
  73. * Drop all of the database types.
  74. *
  75. * @param string $database
  76. * @return void
  77. */
  78. protected function dropAllTypes($database)
  79. {
  80. $this->laravel['db']->connection($database)
  81. ->getSchemaBuilder()
  82. ->dropAllTypes();
  83. }
  84. /**
  85. * Get the console command options.
  86. *
  87. * @return array
  88. */
  89. protected function getOptions()
  90. {
  91. return [
  92. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  93. ['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'],
  94. ['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'],
  95. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  96. ];
  97. }
  98. }