RollbackCommand.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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->rollback(
  50. $this->getMigrationPaths(), [
  51. 'pretend' => $this->option('pretend'),
  52. 'step' => (int) $this->option('step'),
  53. ]
  54. );
  55. // Once the migrator has run we will grab the note output and send it out to
  56. // the console screen, since the migrator itself functions without having
  57. // any instances of the OutputInterface contract passed into the class.
  58. foreach ($this->migrator->getNotes() as $note) {
  59. $this->output->writeln($note);
  60. }
  61. }
  62. /**
  63. * Get the console command options.
  64. *
  65. * @return array
  66. */
  67. protected function getOptions()
  68. {
  69. return [
  70. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
  71. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
  72. ['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed.'],
  73. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths.'],
  74. ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
  75. ['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted.'],
  76. ];
  77. }
  78. }