FreshCommand.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $this->dropAllTables(
  32. $database = $this->input->getOption('database')
  33. );
  34. $this->info('Dropped all tables successfully.');
  35. $this->call('migrate', [
  36. '--database' => $database,
  37. '--path' => $this->input->getOption('path'),
  38. '--force' => true,
  39. ]);
  40. if ($this->needsSeeding()) {
  41. $this->runSeeder($database);
  42. }
  43. }
  44. /**
  45. * Drop all of the database tables.
  46. *
  47. * @param string $database
  48. * @return void
  49. */
  50. protected function dropAllTables($database)
  51. {
  52. $this->laravel['db']->connection($database)
  53. ->getSchemaBuilder()
  54. ->dropAllTables();
  55. }
  56. /**
  57. * Determine if the developer has requested database seeding.
  58. *
  59. * @return bool
  60. */
  61. protected function needsSeeding()
  62. {
  63. return $this->option('seed') || $this->option('seeder');
  64. }
  65. /**
  66. * Run the database seeder command.
  67. *
  68. * @param string $database
  69. * @return void
  70. */
  71. protected function runSeeder($database)
  72. {
  73. $this->call('db:seed', [
  74. '--database' => $database,
  75. '--class' => $this->option('seeder') ?: 'DatabaseSeeder',
  76. '--force' => $this->option('force'),
  77. ]);
  78. }
  79. /**
  80. * Get the console command options.
  81. *
  82. * @return array
  83. */
  84. protected function getOptions()
  85. {
  86. return [
  87. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
  88. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
  89. ['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed.'],
  90. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths.'],
  91. ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
  92. ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder.'],
  93. ];
  94. }
  95. }