RollbackCommand.php 2.6 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\Attribute\AsCommand;
  6. use Symfony\Component\Console\Input\InputOption;
  7. #[AsCommand('migrate:rollback')]
  8. class RollbackCommand extends BaseCommand
  9. {
  10. use ConfirmableTrait;
  11. /**
  12. * The console command name.
  13. *
  14. * @var string
  15. */
  16. protected $name = 'migrate:rollback';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Rollback the last database migration';
  23. /**
  24. * The migrator instance.
  25. *
  26. * @var \Illuminate\Database\Migrations\Migrator
  27. */
  28. protected $migrator;
  29. /**
  30. * Create a new migration rollback command instance.
  31. *
  32. * @param \Illuminate\Database\Migrations\Migrator $migrator
  33. * @return void
  34. */
  35. public function __construct(Migrator $migrator)
  36. {
  37. parent::__construct();
  38. $this->migrator = $migrator;
  39. }
  40. /**
  41. * Execute the console command.
  42. *
  43. * @return int
  44. */
  45. public function handle()
  46. {
  47. if (! $this->confirmToProceed()) {
  48. return 1;
  49. }
  50. $this->migrator->usingConnection($this->option('database'), function () {
  51. $this->migrator->setOutput($this->output)->rollback(
  52. $this->getMigrationPaths(), [
  53. 'pretend' => $this->option('pretend'),
  54. 'step' => (int) $this->option('step'),
  55. 'batch' => (int) $this->option('batch'),
  56. ]
  57. );
  58. });
  59. return 0;
  60. }
  61. /**
  62. * Get the console command options.
  63. *
  64. * @return array
  65. */
  66. protected function getOptions()
  67. {
  68. return [
  69. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  70. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  71. ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
  72. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
  73. ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'],
  74. ['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted'],
  75. ['batch', null, InputOption::VALUE_REQUIRED, 'The batch of migrations (identified by their batch number) to be reverted'],
  76. ];
  77. }
  78. }