ResetCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Illuminate\Database\Console\Migrations;
  3. use Illuminate\Console\ConfirmableTrait;
  4. use Illuminate\Console\Prohibitable;
  5. use Illuminate\Database\Migrations\Migrator;
  6. use Symfony\Component\Console\Attribute\AsCommand;
  7. use Symfony\Component\Console\Input\InputOption;
  8. #[AsCommand(name: 'migrate:reset')]
  9. class ResetCommand extends BaseCommand
  10. {
  11. use ConfirmableTrait, Prohibitable;
  12. /**
  13. * The console command name.
  14. *
  15. * @var string
  16. */
  17. protected $name = 'migrate:reset';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Rollback all database migrations';
  24. /**
  25. * The migrator instance.
  26. *
  27. * @var \Illuminate\Database\Migrations\Migrator
  28. */
  29. protected $migrator;
  30. /**
  31. * Create a new migration rollback command instance.
  32. *
  33. * @param \Illuminate\Database\Migrations\Migrator $migrator
  34. * @return void
  35. */
  36. public function __construct(Migrator $migrator)
  37. {
  38. parent::__construct();
  39. $this->migrator = $migrator;
  40. }
  41. /**
  42. * Execute the console command.
  43. *
  44. * @return int
  45. */
  46. public function handle()
  47. {
  48. if ($this->isProhibited() ||
  49. ! $this->confirmToProceed()) {
  50. return 1;
  51. }
  52. return $this->migrator->usingConnection($this->option('database'), function () {
  53. // First, we'll make sure that the migration table actually exists before we
  54. // start trying to rollback and re-run all of the migrations. If it's not
  55. // present we'll just bail out with an info message for the developers.
  56. if (! $this->migrator->repositoryExists()) {
  57. return $this->components->warn('Migration table not found.');
  58. }
  59. $this->migrator->setOutput($this->output)->reset(
  60. $this->getMigrationPaths(), $this->option('pretend')
  61. );
  62. });
  63. }
  64. /**
  65. * Get the console command options.
  66. *
  67. * @return array
  68. */
  69. protected function getOptions()
  70. {
  71. return [
  72. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  73. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  74. ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
  75. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
  76. ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'],
  77. ];
  78. }
  79. }