From 048071e383df94318df9c33e4a0a9991147e5411 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Fri, 12 May 2023 17:46:04 -0400 Subject: [PATCH] Increase the number of applicable indexes when primary key might change --- src/Query/Query.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index c0b438a..9eae679 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -293,11 +293,20 @@ protected function applySet( $set_clauses[] = shape('column' => $column, 'expression' => $right); } + $primary_key_columns = $table_schema?->getPrimaryKeyColumns() ?? keyset[]; + $primary_key_changed = false; + + foreach ($set_clauses as $clause) { + if (C\contains_key($primary_key_columns, $clause['column'])) { + $primary_key_changed = true; + } + } + $applicable_indexes = vec[]; if ($table_schema is nonnull) { foreach ($table_schema->indexes as $index) { - if (Keyset\intersect($index->fields, $columns) !== keyset[]) { + if ($primary_key_changed || Keyset\intersect($index->fields, $columns) !== keyset[]) { $applicable_indexes[] = $index; } } @@ -320,11 +329,8 @@ protected function applySet( } } - list($unique_index_ref_deletes, $index_ref_deletes) = self::getIndexRemovalsForRow( - $applicable_indexes, - $row_id, - $row, - ); + list($unique_index_ref_deletes, $index_ref_deletes) = + self::getIndexRemovalsForRow($applicable_indexes, $row_id, $row); foreach ($set_clauses as $clause) { $existing_value = $row[$clause['column']] ?? null; @@ -362,10 +368,8 @@ protected function applySet( } } - list($unique_index_ref_additions, $index_ref_additions) = self::getIndexAdditionsForRow( - $applicable_indexes, - $row, - ); + list($unique_index_ref_additions, $index_ref_additions) = + self::getIndexAdditionsForRow($applicable_indexes, $row); } if ($changes_found) {