You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RefreshCommand.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // If the "step" option is specified it means we only want to rollback a small
  37. // number of migrations before migrating again. For example, the user might
  38. // only rollback and remigrate the latest four migrations instead of all.
  39. $step = $this->input->getOption('step') ?: 0;
  40. if ($step > 0) {
  41. $this->runRollback($database, $path, $step);
  42. } else {
  43. $this->runReset($database, $path);
  44. }
  45. // The refresh command is essentially just a brief aggregate of a few other of
  46. // the migration commands and just provides a convenient wrapper to execute
  47. // them in succession. We'll also see if we need to re-seed the database.
  48. $this->call('migrate', array_filter([
  49. '--database' => $database,
  50. '--path' => $path,
  51. '--realpath' => $this->input->getOption('realpath'),
  52. '--force' => true,
  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 int $step
  64. * @return void
  65. */
  66. protected function runRollback($database, $path, $step)
  67. {
  68. $this->call('migrate:rollback', array_filter([
  69. '--database' => $database,
  70. '--path' => $path,
  71. '--realpath' => $this->input->getOption('realpath'),
  72. '--step' => $step,
  73. '--force' => true,
  74. ]));
  75. }
  76. /**
  77. * Run the reset command.
  78. *
  79. * @param string $database
  80. * @param string $path
  81. * @return void
  82. */
  83. protected function runReset($database, $path)
  84. {
  85. $this->call('migrate:reset', array_filter([
  86. '--database' => $database,
  87. '--path' => $path,
  88. '--realpath' => $this->input->getOption('realpath'),
  89. '--force' => true,
  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', array_filter([
  110. '--database' => $database,
  111. '--class' => $this->option('seeder') ?: 'DatabaseSeeder',
  112. '--force' => true,
  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. }