Skip to content

Commit 51c3e59

Browse files
authored
MigrationPhase: convert to native enum (#107)
1 parent a7cc803 commit 51c3e59

9 files changed

+37
-46
lines changed

src/Command/MigrationCheckCommand.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private function checkMigrationsExecuted(OutputInterface $output): int
6767
$exitCode = self::EXIT_OK;
6868
$migrationsDir = $this->migrationService->getConfig()->getMigrationsDirectory();
6969

70-
foreach ([MigrationPhase::BEFORE, MigrationPhase::AFTER] as $phase) {
70+
foreach (MigrationPhase::cases() as $phase) {
7171
$executed = $this->migrationService->getExecutedVersions($phase);
7272
$prepared = $this->migrationService->getPreparedVersions();
7373

@@ -76,16 +76,16 @@ private function checkMigrationsExecuted(OutputInterface $output): int
7676

7777
if (count($executedNotPresent) > 0) {
7878
$exitCode |= self::EXIT_UNKNOWN_MIGRATION;
79-
$output->writeln("<error>Phase $phase has executed migrations not present in {$migrationsDir}: " . implode(', ', $executedNotPresent) . '</error>');
79+
$output->writeln("<error>Phase $phase->value has executed migrations not present in {$migrationsDir}: " . implode(', ', $executedNotPresent) . '</error>');
8080
}
8181

8282
if (count($toBeExecuted) > 0) {
8383
$exitCode |= self::EXIT_AWAITING_MIGRATION;
84-
$output->writeln("<comment>Phase $phase not fully executed, awaiting migrations:" . PHP_EOL . ' > ' . implode(PHP_EOL . ' > ', $toBeExecuted) . '</comment>');
84+
$output->writeln("<comment>Phase $phase->value not fully executed, awaiting migrations:" . PHP_EOL . ' > ' . implode(PHP_EOL . ' > ', $toBeExecuted) . '</comment>');
8585
}
8686

8787
if (count($executedNotPresent) === 0 && count($toBeExecuted) === 0) {
88-
$output->writeln("<info>Phase $phase fully executed, no awaiting migrations</info>");
88+
$output->writeln("<info>Phase $phase->value fully executed, no awaiting migrations</info>");
8989
}
9090
}
9191

src/Command/MigrationRunCommand.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function configure(): void
4040
{
4141
$this
4242
->setDescription('Run all not executed migrations with specified phase')
43-
->addArgument(self::ARGUMENT_PHASE, InputArgument::REQUIRED, MigrationPhase::BEFORE . '|' . MigrationPhase::AFTER . '|' . self::PHASE_BOTH);
43+
->addArgument(self::ARGUMENT_PHASE, InputArgument::REQUIRED, MigrationPhase::BEFORE->value . '|' . MigrationPhase::AFTER->value . '|' . self::PHASE_BOTH);
4444
}
4545

4646
public function execute(InputInterface $input, OutputInterface $output): int
@@ -64,26 +64,26 @@ public function execute(InputInterface $input, OutputInterface $output): int
6464
}
6565

6666
/**
67-
* @param string[] $phases
67+
* @param list<MigrationPhase> $phases
6868
*/
6969
private function executeMigrations(OutputInterface $output, array $phases): bool
7070
{
7171
$executed = [];
7272

7373
if (in_array(MigrationPhase::BEFORE, $phases, true)) {
74-
$executed[MigrationPhase::BEFORE] = $this->migrationService->getExecutedVersions(MigrationPhase::BEFORE);
74+
$executed[MigrationPhase::BEFORE->value] = $this->migrationService->getExecutedVersions(MigrationPhase::BEFORE);
7575
}
7676

7777
if (in_array(MigrationPhase::AFTER, $phases, true)) {
78-
$executed[MigrationPhase::AFTER] = $this->migrationService->getExecutedVersions(MigrationPhase::AFTER);
78+
$executed[MigrationPhase::AFTER->value] = $this->migrationService->getExecutedVersions(MigrationPhase::AFTER);
7979
}
8080

8181
$preparedVersions = $this->migrationService->getPreparedVersions();
8282
$migratedSomething = false;
8383

8484
foreach ($preparedVersions as $version) {
8585
foreach ($phases as $phase) {
86-
if (isset($executed[$phase][$version])) {
86+
if (isset($executed[$phase->value][$version])) {
8787
continue;
8888
}
8989

@@ -95,9 +95,9 @@ private function executeMigrations(OutputInterface $output, array $phases): bool
9595
return $migratedSomething;
9696
}
9797

98-
private function executeMigration(OutputInterface $output, string $version, string $phase): void
98+
private function executeMigration(OutputInterface $output, string $version, MigrationPhase $phase): void
9999
{
100-
$output->write("Executing migration {$version} phase {$phase}... ");
100+
$output->write("Executing migration {$version} phase {$phase->value}... ");
101101

102102
$run = $this->migrationService->executeMigration($version, $phase);
103103

@@ -106,15 +106,15 @@ private function executeMigration(OutputInterface $output, string $version, stri
106106
}
107107

108108
/**
109-
* @return list<string>
109+
* @return list<MigrationPhase>
110110
*/
111111
private function getPhasesToRun(string $phaseArgument): array
112112
{
113-
if ($phaseArgument === MigrationPhase::BEFORE) {
113+
if ($phaseArgument === MigrationPhase::BEFORE->value) {
114114
return [MigrationPhase::BEFORE];
115115
}
116116

117-
if ($phaseArgument === MigrationPhase::AFTER) {
117+
if ($phaseArgument === MigrationPhase::AFTER->value) {
118118
return [MigrationPhase::AFTER];
119119
}
120120

src/Command/MigrationSkipCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
3636
{
3737
$skipped = false;
3838

39-
foreach ([MigrationPhase::BEFORE, MigrationPhase::AFTER] as $phase) {
39+
foreach (MigrationPhase::cases() as $phase) {
4040
$executed = $this->migrationService->getExecutedVersions($phase);
4141
$prepared = $this->migrationService->getPreparedVersions();
4242

@@ -45,7 +45,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
4545
$run = new MigrationRun($version, $phase, $now, $now);
4646

4747
$this->migrationService->markMigrationExecuted($run);
48-
$output->writeln("Migration {$version} phase {$phase} skipped.");
48+
$output->writeln("Migration {$version} phase {$phase->value} skipped.");
4949
$skipped = true;
5050
}
5151
}

src/MigrationPhase.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace ShipMonk\Doctrine\Migration;
44

5-
interface MigrationPhase
5+
enum MigrationPhase: string
66
{
77

8-
public const BEFORE = 'before';
9-
public const AFTER = 'after';
8+
case BEFORE = 'before';
9+
case AFTER = 'after';
1010

1111
}

src/MigrationRun.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class MigrationRun
99

1010
private string $version;
1111

12-
private string $phase;
12+
private MigrationPhase $phase;
1313

1414
private DateTimeImmutable $startedAt;
1515

1616
private DateTimeImmutable $finishedAt;
1717

1818
public function __construct(
1919
string $version,
20-
string $phase,
20+
MigrationPhase $phase,
2121
DateTimeImmutable $startedAt,
2222
DateTimeImmutable $finishedAt
2323
)
@@ -33,7 +33,7 @@ public function getVersion(): string
3333
return $this->version;
3434
}
3535

36-
public function getPhase(): string
36+
public function getPhase(): MigrationPhase
3737
{
3838
return $this->phase;
3939
}

src/MigrationService.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private function getMigration(string $version): Migration
6363
return new $fqn();
6464
}
6565

66-
public function executeMigration(string $version, string $phase): MigrationRun
66+
public function executeMigration(string $version, MigrationPhase $phase): MigrationRun
6767
{
6868
$migration = $this->getMigration($version);
6969

@@ -83,17 +83,14 @@ public function executeMigration(string $version, string $phase): MigrationRun
8383
return $run;
8484
}
8585

86-
private function doExecuteMigration(Migration $migration, string $version, string $phase): MigrationRun
86+
private function doExecuteMigration(Migration $migration, string $version, MigrationPhase $phase): MigrationRun
8787
{
8888
$startTime = new DateTimeImmutable();
8989

90-
if ($phase === MigrationPhase::BEFORE) {
91-
$migration->before($this->executor);
92-
} elseif ($phase === MigrationPhase::AFTER) {
93-
$migration->after($this->executor);
94-
} else {
95-
throw new LogicException("Invalid phase {$phase} given!");
96-
}
90+
match ($phase) {
91+
MigrationPhase::BEFORE => $migration->before($this->executor),
92+
MigrationPhase::AFTER => $migration->after($this->executor),
93+
};
9794

9895
$endTime = new DateTimeImmutable();
9996
$run = new MigrationRun($version, $phase, $startTime, $endTime);
@@ -136,13 +133,13 @@ public function getPreparedVersions(): array
136133
/**
137134
* @return array<string, string>
138135
*/
139-
public function getExecutedVersions(string $phase): array
136+
public function getExecutedVersions(MigrationPhase $phase): array
140137
{
141138
/** @var list<array{version: mixed}> $result */
142139
$result = $this->connection->executeQuery(
143140
'SELECT version FROM migration WHERE phase = :phase',
144141
[
145-
'phase' => $phase,
142+
'phase' => $phase->value,
146143
],
147144
)->fetchAllAssociative();
148145

@@ -168,7 +165,7 @@ public function markMigrationExecuted(MigrationRun $run): void
168165
$microsecondsFormat = 'Y-m-d H:i:s.u';
169166
$this->connection->insert($this->config->getMigrationTableName(), [
170167
'version' => $run->getVersion(),
171-
'phase' => $run->getPhase(),
168+
'phase' => $run->getPhase()->value,
172169
'started_at' => $run->getStartedAt()->format($microsecondsFormat),
173170
'finished_at' => $run->getFinishedAt()->format($microsecondsFormat),
174171
]);

src/Statement.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ class Statement
77

88
public readonly string $sql;
99

10-
/**
11-
* @var MigrationPhase::*|null
12-
*/
13-
public readonly ?string $phase;
10+
public readonly ?MigrationPhase $phase;
1411

15-
/**
16-
* @param MigrationPhase::*|null $phase
17-
*/
18-
public function __construct(string $sql, ?string $phase = null)
12+
public function __construct(string $sql, ?MigrationPhase $phase = null)
1913
{
2014
$this->sql = $sql;
2115
$this->phase = $phase;

tests/Command/MigrationRunCommandTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function testBasicRun(): void
3333

3434
$command = new MigrationRunCommand($migrationService);
3535

36-
self::assertSame('No migration executed (phase before).' . PHP_EOL, $this->runPhase($command, MigrationPhase::BEFORE));
37-
self::assertSame('Executing migration fakeversion phase after... done, 1.000 s elapsed.' . PHP_EOL, $this->runPhase($command, MigrationPhase::AFTER));
36+
self::assertSame('No migration executed (phase before).' . PHP_EOL, $this->runPhase($command, MigrationPhase::BEFORE->value));
37+
self::assertSame('Executing migration fakeversion phase after... done, 1.000 s elapsed.' . PHP_EOL, $this->runPhase($command, MigrationPhase::AFTER->value));
3838
}
3939

4040
public function testRunBoth(): void
@@ -51,7 +51,7 @@ public function testRunBoth(): void
5151
$executeCallsMatcher = self::exactly(4);
5252
$migrationService->expects($executeCallsMatcher)
5353
->method('executeMigration')
54-
->willReturnCallback(function (string $version, string $phase) use ($executeCallsMatcher): MigrationRun {
54+
->willReturnCallback(function (string $version, MigrationPhase $phase) use ($executeCallsMatcher): MigrationRun {
5555
match ($executeCallsMatcher->numberOfInvocations()) {
5656
1 => self::assertEquals(['version1', MigrationPhase::BEFORE], [$version, $phase]),
5757
2 => self::assertEquals(['version1', MigrationPhase::AFTER], [$version, $phase]),

tests/MigrationRunTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testGetDuration(): void
1212
{
1313
$migrationRun = new MigrationRun(
1414
'version',
15-
'phase',
15+
MigrationPhase::AFTER,
1616
new DateTimeImmutable('2021-01-01 00:00:00.000000'),
1717
new DateTimeImmutable('2021-01-01 00:00:01.000001'),
1818
);

0 commit comments

Comments
 (0)