-
Notifications
You must be signed in to change notification settings - Fork 73
/
IndexManager.php
274 lines (228 loc) · 7.21 KB
/
IndexManager.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Db;
use Doctrine\DBAL\Schema\Exception\IndexDoesNotExist;
use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Migration\TableSchema;
use OCP\IConfig;
class IndexManager {
private string $dbPrefix;
/**
* @psalm-suppress PossiblyUnusedMethod
*/
public function __construct(
private IConfig $config,
private Schema $schema,
) {
$this->setUp();
}
private function setUp(): void {
$this->dbPrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
}
public function setSchema(Schema &$schema): void {
$this->schema = $schema;
}
/**
* Create all indices
*
* @return string[] logged messages
*/
public function createIndices(): array {
$messages = [];
foreach (TableSchema::UNIQUE_INDICES as $tableName => $values) {
$messages[] = $this->createIndex($tableName, $values['name'], $values['columns'], $values['unique']);
}
foreach (TableSchema::COMMON_INDICES as $index) {
$messages[] = $this->createIndex($index['table'], $index['name'], $index['columns'], $index['unique']);
}
return $messages;
}
/**
* add on delete fk contraints to all tables referencing the main polls table
*
* @return string[] logged messages
*/
public function createForeignKeyConstraints(): array {
$messages = [];
foreach (TableSchema::FK_CHILD_TABLES as $childTable) {
$messages[] = $this->createForeignKeyConstraint(TableSchema::FK_PARENT_TABLE, $childTable);
}
return $messages;
}
/**
* add an on delete fk contraint
*
* @param string $parentTableName name of reffered table
* @param string $childTableName name of referring table
* @return string log message
*/
public function createForeignKeyConstraint(string $parentTableName, string $childTableName): string {
$parentTableName = $this->dbPrefix . $parentTableName;
$childTableName = $this->dbPrefix . $childTableName;
$parentTable = $this->schema->getTable($parentTableName);
$childTable = $this->schema->getTable($childTableName);
$childTable->addForeignKeyConstraint($parentTable, ['poll_id'], ['id'], ['onDelete' => 'CASCADE']);
return 'Added ' . $parentTableName . '[\'poll_id\'] <- ' . $childTableName . '[\'id\']';
}
/**
* Create named index for table
*
* @param string $tableName name of table to add the index to
* @param string $indexName index name
* @param string[] $columns columns to inclue to the index
* @param bool $unique create a unique index
* @return string log message
*/
public function createIndex(string $tableName, string $indexName, array $columns, bool $unique = false): string {
$tableName = $this->dbPrefix . $tableName;
if ($this->schema->hasTable($tableName)) {
$table = $this->schema->getTable($tableName);
if (!$table->hasIndex($indexName)) {
if ($unique) {
$table->addUniqueIndex($columns, $indexName);
return 'Added unique index ' . $indexName . ' for ' . json_encode($columns) . ' to ' . $tableName;
} else {
$table->addIndex($columns, $indexName);
return 'Added index ' . $indexName . ' for ' . json_encode($columns) . ' to ' . $tableName;
}
}
return 'Index ' . $indexName . ' already exists in ' . $tableName;
}
return 'Table ' . $tableName . ' does not exist';
}
/**
* remove all foreign keys
*
* @return string[] logged messages
*/
public function removeAllForeignKeyConstraints(): array {
$messages = [];
foreach (TableSchema::FK_CHILD_TABLES as $tableName) {
$messages = array_merge($messages, $this->removeForeignKeysFromTable($tableName));
}
return $messages;
}
/**
* remove all generic indices
*
* @return string[] logged messages
*/
public function removeAllGenericIndices(): array {
$messages = [];
foreach (TableSchema::FK_CHILD_TABLES as $tableName) {
$messages = array_merge($messages, $this->removeGenericIndicesFromTable($tableName));
}
return $messages;
}
/**
* remove all generic indices
*
* @return string[] logged messages
*/
public function removeNamedIndices(): array {
$messages = [];
foreach (TableSchema::COMMON_INDICES as $index) {
$message = $this->removeNamedIndexFromTable($index['table'], $index['name']);
if ($message !== null && $message !== '') {
$messages[] = $message;
}
}
return $messages;
}
/**
* remove all unique indices
*
* @return string[] logged messages
*/
public function removeAllUniqueIndices(): array {
$messages = [];
foreach (array_keys(TableSchema::UNIQUE_INDICES) as $tableName) {
$messages = array_merge($messages, $this->removeUniqueIndicesFromTable($tableName));
}
return $messages;
}
/**
* remove all foreign keys from $tableName
*
* @param string $tableName name of table to remove fk from
* @return string[] logged messages
*/
public function removeForeignKeysFromTable(string $tableName): array {
$messages = [];
$tableName = $this->dbPrefix . $tableName;
if ($this->schema->hasTable($tableName)) {
$table = $this->schema->getTable($tableName);
foreach ($table->getForeignKeys() as $foreignKey) {
$table->removeForeignKey($foreignKey->getName());
$messages[] = 'Removed ' . $foreignKey->getName() . ' from ' . $tableName;
}
}
return $messages;
}
/**
* remove all UNIQUE indices from $table
*
* @param string $tableName table name of table to remove unique incices from
* @return string[] logged messages
*/
public function removeUniqueIndicesFromTable(string $tableName): array {
$messages = [];
$tableName = $this->dbPrefix . $tableName;
if ($this->schema->hasTable($tableName)) {
$table = $this->schema->getTable($tableName);
foreach ($table->getIndexes() as $index) {
if (strpos($index->getName(), 'UNIQ_') === 0) {
$table->dropIndex($index->getName());
$messages[] = 'Removed ' . $index->getName() . ' from ' . $tableName;
}
}
}
return $messages;
}
/**
* remove all generic indices from $table
*
* @param string $tableName table name of table to remove incices from
* @return string[] logged messages
*/
public function removeGenericIndicesFromTable(string $tableName): array {
$messages = [];
$tableName = $this->dbPrefix . $tableName;
if ($this->schema->hasTable($tableName)) {
$table = $this->schema->getTable($tableName);
foreach ($table->getIndexes() as $index) {
if (strpos($index->getName(), 'IDX_') === 0) {
$table->dropIndex($index->getName());
$messages[] = 'Removes ' . $index->getName() . ' from ' . $tableName;
}
}
}
return $messages;
}
/**
* remove all generic indices from $table
*
* @param string $tableName table name of table to remove the index from
* @param string $indexName name of index to remove
*
* @return null|string
*/
public function removeNamedIndexFromTable(string $tableName, string $indexName): string|null {
$tableName = $this->dbPrefix . $tableName;
$message = null;
try {
if ($this->schema->hasTable($tableName)) {
$table = $this->schema->getTable($tableName);
$table->dropIndex($indexName);
$message = 'Removed ' . $indexName . ' from ' . $tableName;
}
} catch (IndexDoesNotExist $e) {
// common index does not exist, skip it
}
return $message;
}
}