-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathCreateUniqueIndices.php
More file actions
74 lines (60 loc) · 2.43 KB
/
Copy pathCreateUniqueIndices.php
File metadata and controls
74 lines (60 loc) · 2.43 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Migration\RepairSteps;
use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V9\IndexManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
class CreateUniqueIndices implements IRepairStep {
public function __construct(
private IndexManager $indexManager,
private IDBConnection $connection,
private Schema $schema,
) {
}
public function getName() {
return 'Polls - Create all unique indices';
}
public function run(IOutput $output): void {
$messages = [];
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$messages = array_merge($messages, $this->indexManager->createUniqueIndices());
try {
$this->connection->migrateToSchema($this->schema);
} catch (\Exception $e) {
// Hard fallback!
// Recreating indices can affect system performance on some db engines with large datasets.
// But the app relies on these indices to function properly, so we have to ensure they are created.
// If for any reasons the unique indices cannot be created, we remove all unique indices and create them again.
// This is a workaround for index conflicts that might occur during migration.
$output->warning('Polls - Exception during index migration: ' . $e->getMessage() . "\n" . $e->getTraceAsString());
if (str_contains($e->getMessage(), 'already exists') || str_contains($e->getMessage(), '42P07')) {
$output->warning('Polls - Index conflict detected, rebuilding unique indices.');
if ($this->connection->inTransaction()) {
$this->connection->rollBack();
}
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$messages = array_merge($messages, $this->indexManager->repairPrimaryKeys());
$messages = array_merge($messages, $this->indexManager->removeAllUniqueIndices());
$this->connection->migrateToSchema($this->schema);
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$messages = array_merge($messages, $this->indexManager->createUniqueIndices());
$this->connection->migrateToSchema($this->schema);
} else {
throw $e;
}
}
foreach ($messages as $message) {
$output->info($message);
}
$output->info('Polls - Indices created.');
}
}