ResetCommand.php 2.7KB

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