RollbackCommand.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 RollbackCommand extends BaseCommand
  7. {
  8. use ConfirmableTrait;
  9. /**
  10. * The console command name.
  11. *
  12. * @var string
  13. */
  14. protected $name = 'migrate:rollback';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Rollback the last database migration';
  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. $this->migrator->setOutput($this->output)->rollback(
  50. $this->getMigrationPaths(), [
  51. 'pretend' => $this->option('pretend'),
  52. 'step' => (int) $this->option('step'),
  53. ]
  54. );
  55. }
  56. /**
  57. * Get the console command options.
  58. *
  59. * @return array
  60. */
  61. protected function getOptions()
  62. {
  63. return [
  64. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  65. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  66. ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
  67. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
  68. ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'],
  69. ['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted'],
  70. ];
  71. }
  72. }