-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocumentMigrationOrchestrator.php
82 lines (68 loc) · 2.32 KB
/
DocumentMigrationOrchestrator.php
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
<?php
declare(strict_types=1);
namespace MeiliSearchBundle\Document;
use MeiliSearchBundle\Dump\DumpOrchestratorInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;
use function sprintf;
/**
* @author Guillaume Loulier <[email protected]>
*/
final class DocumentMigrationOrchestrator implements DocumentMigrationOrchestratorInterface
{
/**
* @var DocumentEntryPointInterface
*/
private $documentEntryPoint;
/**
* @var DumpOrchestratorInterface
*/
private $dumpOrchestrator;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param DocumentEntryPointInterface $documentEntryPoint
* @param DumpOrchestratorInterface $dumpOrchestrator
* @param LoggerInterface|null $logger
*/
public function __construct(
DocumentEntryPointInterface $documentEntryPoint,
DumpOrchestratorInterface $dumpOrchestrator,
?LoggerInterface $logger = null
) {
$this->documentEntryPoint = $documentEntryPoint;
$this->dumpOrchestrator = $dumpOrchestrator;
$this->logger = $logger ?: new NullLogger();
}
/**
* {@inheritdoc}
*/
public function migrate(string $oldIndexUid, string $newIndexUid, bool $removeOldIndexDocuments = false): void
{
$oldIndexDocuments = $this->documentEntryPoint->getDocuments($oldIndexUid);
if (empty($oldIndexDocuments)) {
$this->logger->info(sprintf('The documents from "%s" cannot be migrated as the document list is empty', $oldIndexUid));
return;
}
$this->dumpOrchestrator->create();
try {
$this->documentEntryPoint->addDocuments($newIndexUid, $oldIndexDocuments);
} catch (Throwable $throwable) {
$this->logger->critical('The documents cannot be migrated, a dump has been created before trying to add the new documents', [
'error' => $throwable->getMessage(),
'index' => $newIndexUid,
]);
throw $throwable;
}
$this->logger->info('The documents have been migrated', [
'index' => $oldIndexUid,
'nextIndex' => $newIndexUid,
]);
if ($removeOldIndexDocuments) {
$this->documentEntryPoint->removeDocuments($oldIndexUid);
}
}
}