You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrateCommand.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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->setOutput($this->output)
  58. ->run($this->getMigrationPaths(), [
  59. 'pretend' => $this->option('pretend'),
  60. 'step' => $this->option('step'),
  61. ]);
  62. // Finally, if the "seed" option has been given, we will re-run the database
  63. // seed task to re-populate the database, which is convenient when adding
  64. // a migration and a seed at the same time, as it is only this command.
  65. if ($this->option('seed') && ! $this->option('pretend')) {
  66. $this->call('db:seed', ['--force' => true]);
  67. }
  68. }
  69. /**
  70. * Prepare the migration database for running.
  71. *
  72. * @return void
  73. */
  74. protected function prepareDatabase()
  75. {
  76. $this->migrator->setConnection($this->option('database'));
  77. if (! $this->migrator->repositoryExists()) {
  78. $this->call('migrate:install', array_filter([
  79. '--database' => $this->option('database'),
  80. ]));
  81. }
  82. }
  83. }