StatusCommand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Illuminate\Database\Console\Migrations;
  3. use Illuminate\Database\Migrations\Migrator;
  4. use Illuminate\Support\Collection;
  5. use Symfony\Component\Console\Attribute\AsCommand;
  6. use Symfony\Component\Console\Input\InputOption;
  7. #[AsCommand(name: 'migrate:status')]
  8. class StatusCommand extends BaseCommand
  9. {
  10. /**
  11. * The console command name.
  12. *
  13. * @var string
  14. */
  15. protected $name = 'migrate:status';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Show the status of each migration';
  22. /**
  23. * The migrator instance.
  24. *
  25. * @var \Illuminate\Database\Migrations\Migrator
  26. */
  27. protected $migrator;
  28. /**
  29. * Create a new migration rollback command instance.
  30. *
  31. * @param \Illuminate\Database\Migrations\Migrator $migrator
  32. * @return void
  33. */
  34. public function __construct(Migrator $migrator)
  35. {
  36. parent::__construct();
  37. $this->migrator = $migrator;
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return int|null
  43. */
  44. public function handle()
  45. {
  46. return $this->migrator->usingConnection($this->option('database'), function () {
  47. if (! $this->migrator->repositoryExists()) {
  48. $this->components->error('Migration table not found.');
  49. return 1;
  50. }
  51. $ran = $this->migrator->getRepository()->getRan();
  52. $batches = $this->migrator->getRepository()->getMigrationBatches();
  53. $migrations = $this->getStatusFor($ran, $batches)
  54. ->when($this->option('pending') !== false, fn ($collection) => $collection->filter(function ($migration) {
  55. return str($migration[1])->contains('Pending');
  56. }));
  57. if (count($migrations) > 0) {
  58. $this->newLine();
  59. $this->components->twoColumnDetail('<fg=gray>Migration name</>', '<fg=gray>Batch / Status</>');
  60. $migrations
  61. ->each(
  62. fn ($migration) => $this->components->twoColumnDetail($migration[0], $migration[1])
  63. );
  64. $this->newLine();
  65. } elseif ($this->option('pending') !== false) {
  66. $this->components->info('No pending migrations');
  67. } else {
  68. $this->components->info('No migrations found');
  69. }
  70. if ($this->option('pending') && $migrations->some(fn ($m) => str($m[1])->contains('Pending'))) {
  71. return $this->option('pending');
  72. }
  73. });
  74. }
  75. /**
  76. * Get the status for the given run migrations.
  77. *
  78. * @param array $ran
  79. * @param array $batches
  80. * @return \Illuminate\Support\Collection
  81. */
  82. protected function getStatusFor(array $ran, array $batches)
  83. {
  84. return Collection::make($this->getAllMigrationFiles())
  85. ->map(function ($migration) use ($ran, $batches) {
  86. $migrationName = $this->migrator->getMigrationName($migration);
  87. $status = in_array($migrationName, $ran)
  88. ? '<fg=green;options=bold>Ran</>'
  89. : '<fg=yellow;options=bold>Pending</>';
  90. if (in_array($migrationName, $ran)) {
  91. $status = '['.$batches[$migrationName].'] '.$status;
  92. }
  93. return [$migrationName, $status];
  94. });
  95. }
  96. /**
  97. * Get an array of all of the migration files.
  98. *
  99. * @return array
  100. */
  101. protected function getAllMigrationFiles()
  102. {
  103. return $this->migrator->getMigrationFiles($this->getMigrationPaths());
  104. }
  105. /**
  106. * Get the console command options.
  107. *
  108. * @return array
  109. */
  110. protected function getOptions()
  111. {
  112. return [
  113. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
  114. ['pending', null, InputOption::VALUE_OPTIONAL, 'Only list pending migrations', false],
  115. ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to use'],
  116. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
  117. ];
  118. }
  119. }