Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/Console/Command/StoreMigrateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Console\Command;

use Patchlevel\EventSourcing\Console\InputHelper;
use Patchlevel\EventSourcing\Console\OutputStyle;
use Patchlevel\EventSourcing\Message\Pipe;
use Patchlevel\EventSourcing\Message\Translator\Translator;
use Patchlevel\EventSourcing\Store\Store;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function count;

#[AsCommand(
'event-sourcing:store:migrate',
'migrate events from one store to another',
)]
final class StoreMigrateCommand extends Command
{
/** @param iterable<int, Translator> $translators */
public function __construct(
private readonly Store $store,
private readonly Store $newStore,
private readonly iterable $translators = [],
) {
parent::__construct();
}

protected function configure(): void
{
$this
->addOption(
'buffer',
null,
InputOption::VALUE_REQUIRED,
'How many messages should be buffered',
1_000,

Check warning on line 43 in src/Console/Command/StoreMigrateCommand.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ null, InputOption::VALUE_REQUIRED, 'How many messages should be buffered', - 1_000, + 999, ); }

Check warning on line 43 in src/Console/Command/StoreMigrateCommand.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ null, InputOption::VALUE_REQUIRED, 'How many messages should be buffered', - 1_000, + 1001, ); }
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$buffer = InputHelper::positiveIntOrZero($input->getOption('buffer'));
$style = new OutputStyle($input, $output);

$style->info('Migration initialization...');

$count = $this->store->count();
$messages = $this->store->load();

$style->progressStart($count);

$bufferedMessages = [];

$pipe = new Pipe(
$messages,
...$this->translators,
);

foreach ($pipe as $message) {
$bufferedMessages[] = $message;

if (count($bufferedMessages) < $buffer) {

Check warning on line 69 in src/Console/Command/StoreMigrateCommand.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "LessThanNegotiation": @@ @@ foreach ($pipe as $message) { $bufferedMessages[] = $message; - if (count($bufferedMessages) < $buffer) { + if (count($bufferedMessages) >= $buffer) { continue; }

Check warning on line 69 in src/Console/Command/StoreMigrateCommand.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "LessThan": @@ @@ foreach ($pipe as $message) { $bufferedMessages[] = $message; - if (count($bufferedMessages) < $buffer) { + if (count($bufferedMessages) <= $buffer) { continue; }
continue;
}

$this->newStore->save(...$bufferedMessages);
$bufferedMessages = [];
$style->progressAdvance($buffer);

Check warning on line 75 in src/Console/Command/StoreMigrateCommand.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ $this->newStore->save(...$bufferedMessages); $bufferedMessages = []; - $style->progressAdvance($buffer); + } if (count($bufferedMessages) !== 0) {
}

if (count($bufferedMessages) !== 0) {
$this->newStore->save(...$bufferedMessages);
$style->progressAdvance(count($bufferedMessages));

Check warning on line 80 in src/Console/Command/StoreMigrateCommand.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ if (count($bufferedMessages) !== 0) { $this->newStore->save(...$bufferedMessages); - $style->progressAdvance(count($bufferedMessages)); + } $style->progressFinish();
}

$style->progressFinish();

Check warning on line 83 in src/Console/Command/StoreMigrateCommand.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ $style->progressAdvance(count($bufferedMessages)); } - $style->progressFinish(); + $style->success('Migration finished'); return 0;
$style->success('Migration finished');

return 0;
}
}
Loading
Loading