Skip to content

Commit c753144

Browse files
committed
enh(occ): add possibility to edit indices
Signed-off-by: Johannes Merkel <mail@johannesgge.de>
1 parent 85f2885 commit c753144

2 files changed

Lines changed: 77 additions & 5 deletions

File tree

core/Command/Db/AddMissingIndices.php

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7373
$this->dispatcher->dispatchTyped($event);
7474

7575
$missingIndices = $event->getMissingIndices();
76-
if ($missingIndices !== []) {
76+
$toReplaceIndices = $event->toReplaceIndices();
77+
78+
if ($missingIndices !== [] || $toReplaceIndices !== []) {
7779
$schema = new SchemaWrapper($this->connection);
7880

7981
foreach ($missingIndices as $missingIndex) {
@@ -97,15 +99,59 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9799
$table->addIndex($missingIndex['columns'], $missingIndex['indexName'], [], $missingIndex['options']);
98100
}
99101

100-
101-
$sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
102-
if ($dryRun && $sqlQueries !== null) {
103-
$output->writeln($sqlQueries);
102+
if (!$dryRun) {
103+
$this->connection->migrateToSchema($schema->getWrappedSchema());
104104
}
105105
$output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>');
106106
}
107107
}
108108
}
109+
110+
foreach ($toReplaceIndices as $toReplaceIndex) {
111+
if ($schema->hasTable($toReplaceIndex['tableName'])) {
112+
$table = $schema->getTable($toReplaceIndex['tableName']);
113+
114+
$allOldIndicesExists = true;
115+
foreach ($toReplaceIndex['oldIndexNames'] as $oldIndexName) {
116+
if (!$table->hasIndex($oldIndexName)) {
117+
$allOldIndicesExists = false;
118+
}
119+
}
120+
121+
if (!$allOldIndicesExists) {
122+
continue;
123+
}
124+
125+
$output->writeln('<info>Adding additional ' . $toReplaceIndex['newIndexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>');
126+
127+
if ($toReplaceIndex['uniqueIndex']) {
128+
$table->addUniqueIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], $toReplaceIndex['options']);
129+
} else {
130+
$table->addIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], [], $toReplaceIndex['options']);
131+
}
132+
133+
if (!$dryRun) {
134+
$this->connection->migrateToSchema($schema->getWrappedSchema());
135+
}
136+
137+
foreach ($toReplaceIndex['oldIndexNames'] as $oldIndexName) {
138+
$output->writeln('<info>Removing ' . $oldIndexName . ' index from the ' . $table->getName() . ' table</info>');
139+
$table->dropIndex($oldIndexName);
140+
}
141+
142+
if (!$dryRun) {
143+
$this->connection->migrateToSchema($schema->getWrappedSchema());
144+
}
145+
$output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>');
146+
}
147+
}
148+
149+
if ($dryRun) {
150+
$sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
151+
if ($sqlQueries !== null) {
152+
$output->writeln($sqlQueries);
153+
}
154+
}
109155
}
110156

111157
return 0;

lib/public/DB/Events/AddMissingIndicesEvent.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class AddMissingIndicesEvent extends \OCP\EventDispatcher\Event {
3939
/** @var array<array-key, array{tableName: string, indexName: string, columns: string[], options: array{}, dropUnnamedIndex: bool, uniqueIndex: bool}> */
4040
private array $missingIndices = [];
4141

42+
/** @var array<array-key, array{tableName: string, oldIndexNames: array, newIndexName: string, columns: string[], uniqueIndex: bool, options: array{}}> */
43+
private array $toReplaceIndices = [];
44+
4245
/**
4346
* @param string[] $columns
4447
* @since 28.0.0
@@ -75,4 +78,27 @@ public function addMissingUniqueIndex(string $tableName, string $indexName, arra
7578
public function getMissingIndices(): array {
7679
return $this->missingIndices;
7780
}
81+
82+
/**
83+
* @param string[] $columns
84+
* @since 29.0.0
85+
*/
86+
public function replaceIndex(string $tableName, array $oldIndexNames, string $newIndexName, array $columns, bool $unique, array $options = []): void {
87+
$this->toReplaceIndices[] = [
88+
'tableName' => $tableName,
89+
'oldIndexNames' => $oldIndexNames,
90+
'newIndexName' => $newIndexName,
91+
'columns' => $columns,
92+
'uniqueIndex' => $unique,
93+
'options' => $options,
94+
];
95+
}
96+
97+
/**
98+
* @since 29.0.0
99+
* @return array<array-key, array{tableName: string, oldIndexNames: array, newIndexName: string, columns: string[], uniqueIndex: bool, options: array{}}>
100+
*/
101+
public function toReplaceIndices(): array {
102+
return $this->toReplaceIndices;
103+
}
78104
}

0 commit comments

Comments
 (0)