-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathStoreMigrateCommand.php
More file actions
89 lines (71 loc) · 2.53 KB
/
Copy pathStoreMigrateCommand.php
File metadata and controls
89 lines (71 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcingBundle\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;
/** @deprecated since version 3.15.0, to be removed in 4.0.0. Use the StoreMigrateCommand from the patchlevel/event-sourcing package instead. */
#[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,
);
}
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) {
continue;
}
$this->newStore->save(...$bufferedMessages);
$bufferedMessages = [];
$style->progressAdvance($buffer);
}
if (count($bufferedMessages) !== 0) {
$this->newStore->save(...$bufferedMessages);
$style->progressAdvance(count($bufferedMessages));
}
$style->progressFinish();
$style->success('Migration finished');
return 0;
}
}