StatusCommand.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Illuminate\Database\Console\Migrations;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Database\Migrations\Migrator;
  5. use Symfony\Component\Console\Input\InputOption;
  6. class StatusCommand extends BaseCommand
  7. {
  8. /**
  9. * The console command name.
  10. *
  11. * @var string
  12. */
  13. protected $name = 'migrate:status';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Show the status of each migration';
  20. /**
  21. * The migrator instance.
  22. *
  23. * @var \Illuminate\Database\Migrations\Migrator
  24. */
  25. protected $migrator;
  26. /**
  27. * Create a new migration rollback command instance.
  28. *
  29. * @param \Illuminate\Database\Migrations\Migrator $migrator
  30. * @return void
  31. */
  32. public function __construct(Migrator $migrator)
  33. {
  34. parent::__construct();
  35. $this->migrator = $migrator;
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $this->migrator->setConnection($this->option('database'));
  45. if (! $this->migrator->repositoryExists()) {
  46. return $this->error('No migrations found.');
  47. }
  48. $ran = $this->migrator->getRepository()->getRan();
  49. $batches = $this->migrator->getRepository()->getMigrationBatches();
  50. if (count($migrations = $this->getStatusFor($ran, $batches)) > 0) {
  51. $this->table(['Ran?', 'Migration', 'Batch'], $migrations);
  52. } else {
  53. $this->error('No migrations found');
  54. }
  55. }
  56. /**
  57. * Get the status for the given ran migrations.
  58. *
  59. * @param array $ran
  60. * @param array $batches
  61. * @return \Illuminate\Support\Collection
  62. */
  63. protected function getStatusFor(array $ran, array $batches)
  64. {
  65. return Collection::make($this->getAllMigrationFiles())
  66. ->map(function ($migration) use ($ran, $batches) {
  67. $migrationName = $this->migrator->getMigrationName($migration);
  68. return in_array($migrationName, $ran)
  69. ? ['<info>Y</info>', $migrationName, $batches[$migrationName]]
  70. : ['<fg=red>N</fg=red>', $migrationName];
  71. });
  72. }
  73. /**
  74. * Get an array of all of the migration files.
  75. *
  76. * @return array
  77. */
  78. protected function getAllMigrationFiles()
  79. {
  80. return $this->migrator->getMigrationFiles($this->getMigrationPaths());
  81. }
  82. /**
  83. * Get the console command options.
  84. *
  85. * @return array
  86. */
  87. protected function getOptions()
  88. {
  89. return [
  90. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
  91. ['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to use.'],
  92. ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths.'],
  93. ];
  94. }
  95. }