Skip to content

Commit

Permalink
MigrationPhase: convert to native enum
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Dec 3, 2024
1 parent d7ecd33 commit 67b98c3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/Command/MigrationCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function checkMigrationsExecuted(OutputInterface $output): int
$exitCode = self::EXIT_OK;
$migrationsDir = $this->migrationService->getConfig()->getMigrationsDirectory();

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

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

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

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

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

Expand Down
20 changes: 10 additions & 10 deletions src/Command/MigrationRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function configure(): void
{
$this
->setDescription('Run all not executed migrations with specified phase')
->addArgument(self::ARGUMENT_PHASE, InputArgument::REQUIRED, MigrationPhase::BEFORE . '|' . MigrationPhase::AFTER . '|' . self::PHASE_BOTH);
->addArgument(self::ARGUMENT_PHASE, InputArgument::REQUIRED, MigrationPhase::BEFORE->value . '|' . MigrationPhase::AFTER->value . '|' . self::PHASE_BOTH);
}

public function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -64,26 +64,26 @@ public function execute(InputInterface $input, OutputInterface $output): int
}

/**
* @param string[] $phases
* @param list<MigrationPhase> $phases
*/
private function executeMigrations(OutputInterface $output, array $phases): bool
{
$executed = [];

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

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

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

foreach ($preparedVersions as $version) {
foreach ($phases as $phase) {
if (isset($executed[$phase][$version])) {
if (isset($executed[$phase->value][$version])) {
continue;
}

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

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

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

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

/**
* @return list<string>
* @return list<MigrationPhase>
*/
private function getPhasesToRun(string $phaseArgument): array
{
if ($phaseArgument === MigrationPhase::BEFORE) {
if ($phaseArgument === MigrationPhase::BEFORE->value) {
return [MigrationPhase::BEFORE];
}

if ($phaseArgument === MigrationPhase::AFTER) {
if ($phaseArgument === MigrationPhase::AFTER->value) {
return [MigrationPhase::AFTER];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Command/MigrationSkipCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
{
$skipped = false;

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

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

$this->migrationService->markMigrationExecuted($run);
$output->writeln("Migration {$version} phase {$phase} skipped.");
$output->writeln("Migration {$version} phase {$phase->value} skipped.");
$skipped = true;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/MigrationPhase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ShipMonk\Doctrine\Migration;

interface MigrationPhase
enum MigrationPhase: string
{

public const BEFORE = 'before';
public const AFTER = 'after';
case BEFORE = 'before';
case AFTER = 'after';

}
6 changes: 3 additions & 3 deletions src/MigrationRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class MigrationRun

private string $version;

private string $phase;
private MigrationPhase $phase;

private DateTimeImmutable $startedAt;

private DateTimeImmutable $finishedAt;

public function __construct(
string $version,
string $phase,
MigrationPhase $phase,
DateTimeImmutable $startedAt,
DateTimeImmutable $finishedAt
)
Expand All @@ -33,7 +33,7 @@ public function getVersion(): string
return $this->version;
}

public function getPhase(): string
public function getPhase(): MigrationPhase
{
return $this->phase;
}
Expand Down
21 changes: 9 additions & 12 deletions src/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function getMigration(string $version): Migration
return new $fqn();
}

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

Expand All @@ -79,17 +79,14 @@ public function executeMigration(string $version, string $phase): MigrationRun
return $run;
}

private function doExecuteMigration(Migration $migration, string $version, string $phase): MigrationRun
private function doExecuteMigration(Migration $migration, string $version, MigrationPhase $phase): MigrationRun
{
$startTime = new DateTimeImmutable();

if ($phase === MigrationPhase::BEFORE) {
$migration->before($this->executor);
} elseif ($phase === MigrationPhase::AFTER) {
$migration->after($this->executor);
} else {
throw new LogicException("Invalid phase {$phase} given!");
}
match ($phase) {
MigrationPhase::BEFORE => $migration->before($this->executor),
MigrationPhase::AFTER => $migration->after($this->executor),
};

$endTime = new DateTimeImmutable();
$run = new MigrationRun($version, $phase, $startTime, $endTime);
Expand Down Expand Up @@ -132,13 +129,13 @@ public function getPreparedVersions(): array
/**
* @return array<string, string>
*/
public function getExecutedVersions(string $phase): array
public function getExecutedVersions(MigrationPhase $phase): array
{
/** @var list<array{version: mixed}> $result */
$result = $this->connection->executeQuery(
'SELECT version FROM migration WHERE phase = :phase',
[
'phase' => $phase,
'phase' => $phase->value,
],
)->fetchAllAssociative();

Expand All @@ -164,7 +161,7 @@ public function markMigrationExecuted(MigrationRun $run): void
$microsecondsFormat = 'Y-m-d H:i:s.u';
$this->connection->insert($this->config->getMigrationTableName(), [
'version' => $run->getVersion(),
'phase' => $run->getPhase(),
'phase' => $run->getPhase()->value,
'started_at' => $run->getStartedAt()->format($microsecondsFormat),
'finished_at' => $run->getFinishedAt()->format($microsecondsFormat),
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/Command/MigrationRunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function testBasicRun(): void

$command = new MigrationRunCommand($migrationService);

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

public function testRunBoth(): void
Expand All @@ -51,7 +51,7 @@ public function testRunBoth(): void
$executeCallsMatcher = self::exactly(4);
$migrationService->expects($executeCallsMatcher)
->method('executeMigration')
->willReturnCallback(function (string $version, string $phase) use ($executeCallsMatcher): MigrationRun {
->willReturnCallback(function (string $version, MigrationPhase $phase) use ($executeCallsMatcher): MigrationRun {
match ($executeCallsMatcher->numberOfInvocations()) {
1 => self::assertEquals(['version1', MigrationPhase::BEFORE], [$version, $phase]),
2 => self::assertEquals(['version1', MigrationPhase::AFTER], [$version, $phase]),
Expand Down
2 changes: 1 addition & 1 deletion tests/MigrationRunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testGetDuration(): void
{
$migrationRun = new MigrationRun(
'version',
'phase',
MigrationPhase::AFTER,
new DateTimeImmutable('2021-01-01 00:00:00.000000'),
new DateTimeImmutable('2021-01-01 00:00:01.000001'),
);
Expand Down

0 comments on commit 67b98c3

Please sign in to comment.