diff --git a/lib/Db/IndexManager.php b/lib/Db/IndexManager.php index 045d9c7ad..67cfed68e 100644 --- a/lib/Db/IndexManager.php +++ b/lib/Db/IndexManager.php @@ -122,11 +122,11 @@ public function createIndex(string $tableName, string $indexName, array $columns * * @return string[] logged messages */ - public function removeAllForeignKeyConstraints(): array { + public function removeIncorrectForeignKeyConstraints(): array { $messages = []; foreach (TableSchema::FK_CHILD_TABLES as $tableName) { - $messages = array_merge($messages, $this->removeForeignKeysFromTable($tableName)); + $messages = array_merge($messages, $this->removeIncorrectForeignKeysFromTable($tableName)); } return $messages; @@ -169,11 +169,11 @@ public function removeNamedIndices(): array { * * @return string[] logged messages */ - public function removeAllUniqueIndices(): array { + public function removeChangedUniqueIndices(): array { $messages = []; - foreach (array_keys(TableSchema::UNIQUE_INDICES) as $tableName) { - $messages = array_merge($messages, $this->removeUniqueIndicesFromTable($tableName)); + foreach (TableSchema::UNIQUE_INDICES as $tableName => $values) { + $messages = array_merge($messages, $this->removeChangedUniqueIndicesFromTable($tableName, $values)); } return $messages; @@ -185,7 +185,7 @@ public function removeAllUniqueIndices(): array { * @param string $tableName name of table to remove fk from * @return string[] logged messages */ - public function removeForeignKeysFromTable(string $tableName): array { + public function removeIncorrectForeignKeysFromTable(string $tableName): array { $messages = []; $tableName = $this->dbPrefix . $tableName; @@ -194,6 +194,10 @@ public function removeForeignKeysFromTable(string $tableName): array { $table = $this->schema->getTable($tableName); foreach ($table->getForeignKeys() as $foreignKey) { + if ($foreignKey->getForeignTableName() === $this->dbPrefix . TableSchema::FK_PARENT_TABLE) { + $messages[] = 'Kept unchanged ' . $foreignKey->getName() . ' from ' . $tableName; + continue; + } $table->removeForeignKey($foreignKey->getName()); $messages[] = 'Removed ' . $foreignKey->getName() . ' from ' . $tableName; } @@ -208,7 +212,7 @@ public function removeForeignKeysFromTable(string $tableName): array { * @param string $tableName table name of table to remove unique incices from * @return string[] logged messages */ - public function removeUniqueIndicesFromTable(string $tableName): array { + public function removeChangedUniqueIndicesFromTable(string $tableName, array $values): array { $messages = []; $tableName = $this->dbPrefix . $tableName; @@ -218,6 +222,14 @@ public function removeUniqueIndicesFromTable(string $tableName): array { foreach ($table->getIndexes() as $index) { if (strpos($index->getName(), 'UNIQ_') === 0) { + if ( + ($index->getName() === $values['name']) && + ($index->isUnique() === $values['unique']) && + ($index->getColumns() === $values['columns']) + ) { + $messages[] = 'Kept unchanged ' . $index->getName() . ' from ' . $tableName; + continue; + } $table->dropIndex($index->getName()); $messages[] = 'Removed ' . $index->getName() . ' from ' . $tableName; } diff --git a/lib/Migration/RepairSteps/RemoveIndices.php b/lib/Migration/RepairSteps/RemoveIndices.php index 2569e9def..1f396a3b5 100644 --- a/lib/Migration/RepairSteps/RemoveIndices.php +++ b/lib/Migration/RepairSteps/RemoveIndices.php @@ -43,12 +43,12 @@ public function run(IOutput $output): void { $this->schema = $this->connection->createSchema(); $this->indexManager->setSchema($this->schema); - $messages = $this->indexManager->removeAllForeignKeyConstraints(); + $messages = $this->indexManager->removeIncorrectForeignKeyConstraints(); foreach ($messages as $message) { $output->info($message); } - $messages = $this->indexManager->removeAllUniqueIndices(); + $messages = $this->indexManager->removeChangedUniqueIndices(); foreach ($messages as $message) { $output->info($message); }