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