Skip to content

Commit

Permalink
feat: Keep unchanged indices and foreign keys instead of recreating
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Sep 3, 2024
1 parent 574bd6f commit 666dffe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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

0 comments on commit 666dffe

Please sign in to comment.