Skip to content

Commit d9a7704

Browse files
committed
add perpetual indicator in status command
1 parent 981ad3c commit d9a7704

File tree

5 files changed

+80
-33
lines changed

5 files changed

+80
-33
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.idea
33
composer.lock
44
.phpunit.result.cache
5+
.phpunit.cache

composer.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
"require-dev": {
1111
"roave/security-advisories": "dev-latest",
1212
"mockery/mockery": "^1.4.2",
13-
"phpunit/phpunit": "^9.3.3",
14-
"laravel/framework": "^9.0",
15-
"orchestra/testbench": "^7.1"
13+
"phpunit/phpunit": "^10.0",
14+
"orchestra/testbench": "^8.0.10"
1615
},
1716
"license": "MIT",
1817
"authors": [

phpunit.xml.dist

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
bootstrap="vendor/autoload.php"
4-
backupGlobals="false"
5-
backupStaticAttributes="false"
6-
beStrictAboutTestsThatDoNotTestAnything="false"
7-
colors="true"
8-
verbose="true"
9-
convertErrorsToExceptions="true"
10-
convertNoticesToExceptions="true"
11-
convertWarningsToExceptions="true"
12-
processIsolation="false"
13-
stopOnFailure="false"
14-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
15-
<coverage>
16-
<include>
17-
<directory suffix=".php">src/</directory>
18-
</include>
19-
</coverage>
20-
<testsuites>
21-
<testsuite name="Test Suite">
22-
<directory>tests</directory>
23-
</testsuite>
24-
</testsuites>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
2513
</phpunit>

src/Console/StatusCommand.php

+52-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
namespace Dentro\Patcher\Console;
44

5+
use Dentro\Patcher\Patcher;
56
use Illuminate\Database\Console\Migrations\StatusCommand as MigrationStatusCommand;
7+
use Illuminate\Support\Collection;
68

79
class StatusCommand extends MigrationStatusCommand
810
{
911
protected $name = 'patcher:status';
1012

1113
protected $description = 'Show the status of each patches.';
1214

15+
/**
16+
* @var Patcher
17+
*/
18+
protected $migrator;
19+
1320
public function handle()
1421
{
1522
return $this->migrator->usingConnection($this->option('database'), function () {
@@ -23,15 +30,57 @@ public function handle()
2330
$batches = $this->migrator->getRepository()->getMigrationBatches();
2431

2532
if (count($patches = $this->getStatusFor($ran, $batches)) > 0) {
26-
$this->table(['Ran?', 'Patch', 'Batch'], $patches);
33+
$this->newLine();
34+
35+
$this->components->twoColumnDetail('<fg=gray>Patcher name</>', '<fg=gray>Batch / Status</>');
36+
37+
$patches
38+
->when($this->option('pending'), fn ($collection) => $collection->filter(function ($migration) {
39+
return str($migration[1])->contains('Pending');
40+
}))
41+
->each(
42+
fn ($migration) => $this->components->twoColumnDetail($migration[0], $migration[1])
43+
);
44+
45+
$this->newLine();
2746
} else {
28-
$this->error('No patch found');
47+
$this->components->info('No patch found');
2948
}
3049
});
3150
}
3251

52+
protected function getStatusFor(array $ran, array $batches)
53+
{
54+
return Collection::make($this->getAllMigrationFiles())
55+
->map(function ($migration) use ($ran, $batches) {
56+
$migrationName = $this->migrator->getMigrationName($migration);
57+
58+
$defaultStatus = '<fg=yellow;options=bold>Pending</>';
59+
60+
$status = $defaultStatus;
61+
62+
if (in_array($migrationName, $ran)) {
63+
$status = '<fg=green;options=bold>Ran</>';
64+
}
65+
66+
if (in_array($migrationName, $ran)) {
67+
$status = '['.$batches[$migrationName].'] '.$status;
68+
}
69+
70+
if ($status === $defaultStatus) {
71+
$patch = $this->migrator->getPatcherObject($migration);
72+
73+
if ($patch->isPerpetual) {
74+
$status = '<fg=yellow;options=bold>Perpetual</>';
75+
}
76+
}
77+
78+
return [$migrationName, $status];
79+
});
80+
}
81+
3382
/**
34-
* Get migration path (either specified by '--path' option or default location).
83+
* Get a migration path (either specified by '--path' option or default location).
3584
*
3685
* @return string
3786
*/

src/Patcher.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
use Dentro\Patcher\Events\PatchEnded;
66
use Dentro\Patcher\Events\PatchStarted;
77
use Illuminate\Console\View\Components\Info;
8-
use Illuminate\Console\View\Components\Task;
98
use Illuminate\Console\View\Components\TwoColumnDetail;
10-
use Illuminate\Console\View\Components\Warn;
119
use Illuminate\Database\Migrations\Migrator;
10+
use InvalidArgumentException;
1211

1312
class Patcher extends Migrator
1413
{
@@ -52,7 +51,7 @@ public function runPending(array $migrations, array $options = []): void
5251
*/
5352
protected function patch(string $file, int $batch): void
5453
{
55-
$patch = $this->resolvePath($file);
54+
$patch = $this->getPatcherObject($file);
5655

5756
$name = $this->getMigrationName($file);
5857

@@ -68,7 +67,7 @@ protected function patch(string $file, int $batch): void
6867

6968
$startTime = microtime(true);
7069

71-
if ($patch instanceof Patch && $this->isEligible($patch)) {
70+
if ($this->isEligible($patch)) {
7271
$patch
7372
->setContainer(app())
7473
->setCommand(app('command.patcher'))
@@ -149,4 +148,15 @@ protected function runPatch(Patch $patch): void
149148

150149
$callback();
151150
}
151+
152+
public function getPatcherObject(string $path): Patch
153+
{
154+
$object = $this->resolvePath($path);
155+
156+
if (! $object instanceof Patch) {
157+
throw new InvalidArgumentException("Patch [{$path}] must extends ".Patch::class);
158+
}
159+
160+
return $object;
161+
}
152162
}

0 commit comments

Comments
 (0)