FreshCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 FreshCommand extends Command
  7. {
  8. use ConfirmableTrait;
  9. /**
  10. * The console command name.
  11. *
  12. * @var string
  13. */
  14. protected $name = 'migrate:fresh';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Drop all tables 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. $database = $this->input->getOption('database');
  32. if ($this->option('drop-views')) {
  33. $this->dropAllViews($database);
  34. $this->info('Dropped all views successfully.');
  35. }
  36. $this->dropAllTables($database);
  37. $this->info('Dropped all tables successfully.');
  38. if ($this->option('drop-types')) {
  39. $this->dropAllTypes($database);
  40. $this->info('Dropped all types successfully.');
  41. }
  42. $this->call('migrate', array_filter([
  43. '--database' => $database,
  44. '--path' => $this->input->getOption('path'),
  45. '--realpath' => $this->input->getOption('realpath'),
  46. '--force' => true,
  47. '--step' => $this->option('step'),
  48. ]));
  49. if ($this->needsSeeding()) {
  50. $this->runSeeder($database);
  51. }
  52. }
  53. /**
  54. * Drop all of the database tables.
  55. *
  56. * @param string $database
  57. * @return void
  58. */
  59. protected function dropAllTables($database)
  60. {
  61. $this->laravel['db']->connection($database)
  62. ->getSchemaBuilder()
  63. ->dropAllTables();
  64. }
  65. /**
  66. * Drop all of the database views.
  67. *
  68. * @param string $database
  69. * @return void
  70. */
  71. protected function dropAllViews($database)
  72. {
  73. $this->laravel['db']->connection($database)
  74. ->getSchemaBuilder()
  75. ->dropAllViews();
  76. }
  77. /**
  78. * Drop all of the database types.
  79. *
  80. * @param string $database
  81. * @return void
  82. */
  83. protected function dropAllTypes($database)
  84. {
  85. $this->laravel['db']->connection($database)
  86. ->getSchemaBuilder()
  87. ->dropAllTypes();
  88. }
  89. /**
  90. * Determine if the developer has requested database seeding.
  91. *
  92. * @return bool
  93. */
  94. protected function needsSeeding()
  95. {
  96. return $this->option('seed') || $this->option('seeder');
  97. }
  98. /**
  99. * Run the database seeder command.
  100. *
  101. * @param string $database
  102. * @return void
  103. */
  104. protected function runSeeder($database)
  105. {
  106. $this->call('db:seed', array_filter([
  107. '--database' => $database,
  108. '--class' => $this->option('seeder') ?: 'DatabaseSeeder',
  109. '--force' => true,
  110. ]));
  111. }
  112. /**
  113. * Get the console command options.
  114. *
  115. * @return array
  116. */
  117. protected function getOptions()
  118. {
  119. return [
  120. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  121. ['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'],
  122. ['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'],
  123. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  124. ['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed'],
  125. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
  126. ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
  127. ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
  128. ['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually'],
  129. ];
  130. }
  131. }