MigrateCommand.php 3.1KB

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