FreshCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Illuminate\Database\Console\Migrations;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\ConfirmableTrait;
  5. use Illuminate\Console\Prohibitable;
  6. use Illuminate\Contracts\Events\Dispatcher;
  7. use Illuminate\Database\Events\DatabaseRefreshed;
  8. use Illuminate\Database\Migrations\Migrator;
  9. use Symfony\Component\Console\Attribute\AsCommand;
  10. use Symfony\Component\Console\Input\InputOption;
  11. #[AsCommand(name: 'migrate:fresh')]
  12. class FreshCommand extends Command
  13. {
  14. use ConfirmableTrait, Prohibitable;
  15. /**
  16. * The console command name.
  17. *
  18. * @var string
  19. */
  20. protected $name = 'migrate:fresh';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'Drop all tables and re-run all migrations';
  27. /**
  28. * The migrator instance.
  29. *
  30. * @var \Illuminate\Database\Migrations\Migrator
  31. */
  32. protected $migrator;
  33. /**
  34. * Create a new fresh command instance.
  35. *
  36. * @param \Illuminate\Database\Migrations\Migrator $migrator
  37. * @return void
  38. */
  39. public function __construct(Migrator $migrator)
  40. {
  41. parent::__construct();
  42. $this->migrator = $migrator;
  43. }
  44. /**
  45. * Execute the console command.
  46. *
  47. * @return int
  48. */
  49. public function handle()
  50. {
  51. if ($this->isProhibited() ||
  52. ! $this->confirmToProceed()) {
  53. return 1;
  54. }
  55. $database = $this->input->getOption('database');
  56. $this->migrator->usingConnection($database, function () use ($database) {
  57. if ($this->migrator->repositoryExists()) {
  58. $this->newLine();
  59. $this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([
  60. '--database' => $database,
  61. '--drop-views' => $this->option('drop-views'),
  62. '--drop-types' => $this->option('drop-types'),
  63. '--force' => true,
  64. ])) == 0);
  65. }
  66. });
  67. $this->newLine();
  68. $this->call('migrate', array_filter([
  69. '--database' => $database,
  70. '--path' => $this->input->getOption('path'),
  71. '--realpath' => $this->input->getOption('realpath'),
  72. '--schema-path' => $this->input->getOption('schema-path'),
  73. '--force' => true,
  74. '--step' => $this->option('step'),
  75. ]));
  76. if ($this->laravel->bound(Dispatcher::class)) {
  77. $this->laravel[Dispatcher::class]->dispatch(
  78. new DatabaseRefreshed($database, $this->needsSeeding())
  79. );
  80. }
  81. if ($this->needsSeeding()) {
  82. $this->runSeeder($database);
  83. }
  84. return 0;
  85. }
  86. /**
  87. * Determine if the developer has requested database seeding.
  88. *
  89. * @return bool
  90. */
  91. protected function needsSeeding()
  92. {
  93. return $this->option('seed') || $this->option('seeder');
  94. }
  95. /**
  96. * Run the database seeder command.
  97. *
  98. * @param string $database
  99. * @return void
  100. */
  101. protected function runSeeder($database)
  102. {
  103. $this->call('db:seed', array_filter([
  104. '--database' => $database,
  105. '--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
  106. '--force' => true,
  107. ]));
  108. }
  109. /**
  110. * Get the console command options.
  111. *
  112. * @return array
  113. */
  114. protected function getOptions()
  115. {
  116. return [
  117. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  118. ['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'],
  119. ['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'],
  120. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  121. ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
  122. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
  123. ['schema-path', null, InputOption::VALUE_OPTIONAL, 'The path to a schema dump file'],
  124. ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
  125. ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
  126. ['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually'],
  127. ];
  128. }
  129. }