Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FreshCommand.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. $this->call('migrate', array_filter([
  39. '--database' => $database,
  40. '--path' => $this->input->getOption('path'),
  41. '--realpath' => $this->input->getOption('realpath'),
  42. '--force' => true,
  43. '--step' => $this->option('step'),
  44. ]));
  45. if ($this->needsSeeding()) {
  46. $this->runSeeder($database);
  47. }
  48. }
  49. /**
  50. * Drop all of the database tables.
  51. *
  52. * @param string $database
  53. * @return void
  54. */
  55. protected function dropAllTables($database)
  56. {
  57. $this->laravel['db']->connection($database)
  58. ->getSchemaBuilder()
  59. ->dropAllTables();
  60. }
  61. /**
  62. * Drop all of the database views.
  63. *
  64. * @param string $database
  65. * @return void
  66. */
  67. protected function dropAllViews($database)
  68. {
  69. $this->laravel['db']->connection($database)
  70. ->getSchemaBuilder()
  71. ->dropAllViews();
  72. }
  73. /**
  74. * Determine if the developer has requested database seeding.
  75. *
  76. * @return bool
  77. */
  78. protected function needsSeeding()
  79. {
  80. return $this->option('seed') || $this->option('seeder');
  81. }
  82. /**
  83. * Run the database seeder command.
  84. *
  85. * @param string $database
  86. * @return void
  87. */
  88. protected function runSeeder($database)
  89. {
  90. $this->call('db:seed', array_filter([
  91. '--database' => $database,
  92. '--class' => $this->option('seeder') ?: 'DatabaseSeeder',
  93. '--force' => true,
  94. ]));
  95. }
  96. /**
  97. * Get the console command options.
  98. *
  99. * @return array
  100. */
  101. protected function getOptions()
  102. {
  103. return [
  104. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  105. ['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'],
  106. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  107. ['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed'],
  108. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
  109. ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
  110. ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
  111. ['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually'],
  112. ];
  113. }
  114. }