Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Keep unchanged indices and foreign keys instead of recreating #3689

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/Db/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Migration/RepairSteps/RemoveIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading