RefreshCommand.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Illuminate\Database\Console\Migrations;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\ConfirmableTrait;
  5. use Symfony\Component\Console\Input\InputOption;
  6. class RefreshCommand extends Command
  7. {
  8. use ConfirmableTrait;
  9. /**
  10. * The console command name.
  11. *
  12. * @var string
  13. */
  14. protected $name = 'migrate:refresh';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Reset and re-run all migrations';
  21. /**
  22. * Execute the console command.
  23. *
  24. * @return void
  25. */
  26. public function handle()
  27. {
  28. if (! $this->confirmToProceed()) {
  29. return;
  30. }
  31. // Next we'll gather some of the options so that we can have the right options
  32. // to pass to the commands. This includes options such as which database to
  33. // use and the path to use for the migration. Then we'll run the command.
  34. $database = $this->input->getOption('database');
  35. $path = $this->input->getOption('path');
  36. $force = $this->input->getOption('force');
  37. // If the "step" option is specified it means we only want to rollback a small
  38. // number of migrations before migrating again. For example, the user might
  39. // only rollback and remigrate the latest four migrations instead of all.
  40. $step = $this->input->getOption('step') ?: 0;
  41. if ($step > 0) {
  42. $this->runRollback($database, $path, $step, $force);
  43. } else {
  44. $this->runReset($database, $path, $force);
  45. }
  46. // The refresh command is essentially just a brief aggregate of a few other of
  47. // the migration commands and just provides a convenient wrapper to execute
  48. // them in succession. We'll also see if we need to re-seed the database.
  49. $this->call('migrate', [
  50. '--database' => $database,
  51. '--path' => $path,
  52. '--force' => $force,
  53. ]);
  54. if ($this->needsSeeding()) {
  55. $this->runSeeder($database);
  56. }
  57. }
  58. /**
  59. * Run the rollback command.
  60. *
  61. * @param string $database
  62. * @param string $path
  63. * @param bool $step
  64. * @param bool $force
  65. * @return void
  66. */
  67. protected function runRollback($database, $path, $step, $force)
  68. {
  69. $this->call('migrate:rollback', [
  70. '--database' => $database,
  71. '--path' => $path,
  72. '--step' => $step,
  73. '--force' => $force,
  74. ]);
  75. }
  76. /**
  77. * Run the reset command.
  78. *
  79. * @param string $database
  80. * @param string $path
  81. * @param bool $force
  82. * @return void
  83. */
  84. protected function runReset($database, $path, $force)
  85. {
  86. $this->call('migrate:reset', [
  87. '--database' => $database,
  88. '--path' => $path,
  89. '--force' => $force,
  90. ]);
  91. }
  92. /**
  93. * Determine if the developer has requested database seeding.
  94. *
  95. * @return bool
  96. */
  97. protected function needsSeeding()
  98. {
  99. return $this->option('seed') || $this->option('seeder');
  100. }
  101. /**
  102. * Run the database seeder command.
  103. *
  104. * @param string $database
  105. * @return void
  106. */
  107. protected function runSeeder($database)
  108. {
  109. $this->call('db:seed', [
  110. '--database' => $database,
  111. '--class' => $this->option('seeder') ?: 'DatabaseSeeder',
  112. '--force' => $this->option('force'),
  113. ]);
  114. }
  115. /**
  116. * Get the console command options.
  117. *
  118. * @return array
  119. */
  120. protected function getOptions()
  121. {
  122. return [
  123. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
  124. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
  125. ['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed.'],
  126. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths.'],
  127. ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
  128. ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder.'],
  129. ['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted & re-run.'],
  130. ];
  131. }
  132. }