MigrateCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Illuminate\Database\Console\Migrations;
  3. use Illuminate\Console\ConfirmableTrait;
  4. use Illuminate\Database\Migrations\Migrator;
  5. class MigrateCommand extends BaseCommand
  6. {
  7. use ConfirmableTrait;
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrate {--database= : The database connection to use.}
  14. {--force : Force the operation to run when in production.}
  15. {--path= : The path of migrations files to be executed.}
  16. {--pretend : Dump the SQL queries that would be run.}
  17. {--seed : Indicates if the seed task should be re-run.}
  18. {--step : Force the migrations to be run so they can be rolled back individually.}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Run the database migrations';
  25. /**
  26. * The migrator instance.
  27. *
  28. * @var \Illuminate\Database\Migrations\Migrator
  29. */
  30. protected $migrator;
  31. /**
  32. * Create a new migration command instance.
  33. *
  34. * @param \Illuminate\Database\Migrations\Migrator $migrator
  35. * @return void
  36. */
  37. public function __construct(Migrator $migrator)
  38. {
  39. parent::__construct();
  40. $this->migrator = $migrator;
  41. }
  42. /**
  43. * Execute the console command.
  44. *
  45. * @return void
  46. */
  47. public function handle()
  48. {
  49. if (! $this->confirmToProceed()) {
  50. return;
  51. }
  52. $this->prepareDatabase();
  53. // Next, we will check to see if a path option has been defined. If it has
  54. // we will use the path relative to the root of this installation folder
  55. // so that migrations may be run for any path within the applications.
  56. $this->migrator->run($this->getMigrationPaths(), [
  57. 'pretend' => $this->option('pretend'),
  58. 'step' => $this->option('step'),
  59. ]);
  60. // Once the migrator has run we will grab the note output and send it out to
  61. // the console screen, since the migrator itself functions without having
  62. // any instances of the OutputInterface contract passed into the class.
  63. foreach ($this->migrator->getNotes() as $note) {
  64. $this->output->writeln($note);
  65. }
  66. // Finally, if the "seed" option has been given, we will re-run the database
  67. // seed task to re-populate the database, which is convenient when adding
  68. // a migration and a seed at the same time, as it is only this command.
  69. if ($this->option('seed')) {
  70. $this->call('db:seed', ['--force' => true]);
  71. }
  72. }
  73. /**
  74. * Prepare the migration database for running.
  75. *
  76. * @return void
  77. */
  78. protected function prepareDatabase()
  79. {
  80. $this->migrator->setConnection($this->option('database'));
  81. if (! $this->migrator->repositoryExists()) {
  82. $this->call(
  83. 'migrate:install', ['--database' => $this->option('database')]
  84. );
  85. }
  86. }
  87. }