-
-
Notifications
You must be signed in to change notification settings - Fork 45
Fix #1142: upsert to use first matching unique constraint #1144
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
Open
dadansatria
wants to merge
5
commits into
yiisoft:master
Choose a base branch
from
dadansatria:fix/upsert-unique-constraint-1142
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−37
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
98e39be
Fix #1142: upsert to use first matching unique constraint
dadansatria 45fcfa9
update changelog username
dadansatria 13c13c3
Add constraintColumns parameter to upsert methods for ON CONFLICT clause
dadansatria f3222cd
Apply PHP CS Fixer and Rector changes (CI)
dadansatria 677079a
Apply suggestion from @Copilot
Tigrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -25,12 +25,9 @@ | |||||
| use function array_combine; | ||||||
| use function array_diff; | ||||||
| use function array_fill_keys; | ||||||
| use function array_filter; | ||||||
| use function array_key_exists; | ||||||
| use function array_keys; | ||||||
| use function array_map; | ||||||
| use function array_merge; | ||||||
| use function array_unique; | ||||||
| use function array_values; | ||||||
| use function count; | ||||||
| use function get_object_vars; | ||||||
|
|
@@ -149,6 +146,7 @@ public function upsert( | |||||
| string $table, | ||||||
| array|QueryInterface $insertColumns, | ||||||
| array|bool $updateColumns = true, | ||||||
| ?array $constraintColumns = null, | ||||||
| array &$params = [], | ||||||
| ): string { | ||||||
| throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); | ||||||
|
|
@@ -159,6 +157,7 @@ public function upsertReturning( | |||||
| string $table, | ||||||
| array|QueryInterface $insertColumns, | ||||||
| array|bool $updateColumns = true, | ||||||
| ?array $constraintColumns = null, | ||||||
| ?array $returnColumns = null, | ||||||
| array &$params = [], | ||||||
| ): string { | ||||||
|
|
@@ -474,6 +473,8 @@ protected function prepareUpsertSets( | |||||
| * Prepare column names and constraints for "upsert" operation. | ||||||
| * | ||||||
| * @param Index[] $constraints | ||||||
| * @param string[]|null $constraintColumns The column names to use for the conflict clause. If `null`, | ||||||
| * the primary key or the first matching unique constraint will be used. | ||||||
| * | ||||||
| * @psalm-param array<string, mixed>|QueryInterface $insertColumns | ||||||
| * | ||||||
|
|
@@ -485,14 +486,21 @@ protected function prepareUpsertColumns( | |||||
| array|QueryInterface $insertColumns, | ||||||
| array|bool $updateColumns, | ||||||
| array &$constraints = [], | ||||||
| ?array $constraintColumns = null, | ||||||
| ): array { | ||||||
| if ($insertColumns instanceof QueryInterface) { | ||||||
| $insertNames = $this->getQueryColumnNames($insertColumns); | ||||||
| } else { | ||||||
| $insertNames = $this->getNormalizedColumnNames(array_keys($insertColumns)); | ||||||
| } | ||||||
|
|
||||||
| $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints); | ||||||
| // Use provided constraint columns or auto-detect from table schema | ||||||
| if ($constraintColumns !== null) { | ||||||
| $uniqueNames = $this->getNormalizedColumnNames($constraintColumns); | ||||||
| $constraints = []; | ||||||
| } else { | ||||||
| $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints); | ||||||
| } | ||||||
|
|
||||||
| if ($updateColumns === true) { | ||||||
| return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)]; | ||||||
|
|
@@ -537,43 +545,29 @@ protected function getNormalizedColumnNames(array $columns): array | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) | ||||||
| * for the named table removing constraints which didn't cover the specified column list. | ||||||
| * | ||||||
| * The column list will be unique by column names. | ||||||
| * Returns column names of the first constraint enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) | ||||||
| * for the named table that is covered by the specified column list. | ||||||
| * | ||||||
| * @param string $name The table name, may contain schema name if any. Don't quote the table name. | ||||||
| * @param string[] $columns Source column list. | ||||||
| * @param Index[] $indexes This parameter optionally receives a matched index list. | ||||||
| * The constraints will be unique by their column names. | ||||||
| * @param Index[] $indexes This parameter optionally receives the first matched index, | ||||||
| * or an empty array if no matching index is found. | ||||||
| * | ||||||
| * @return string[] The column names. | ||||||
| * @return string[] The column names of the first matching constraint. | ||||||
| */ | ||||||
| private function getTableUniqueColumnNames(string $name, array $columns, array &$indexes = []): array | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| $indexes = $this->schema->getTableUniques($name); | ||||||
| $columnNames = []; | ||||||
|
|
||||||
| // Remove all indexes which don't cover the specified column list. | ||||||
| $indexes = array_values( | ||||||
| array_filter( | ||||||
| $indexes, | ||||||
| static function (Index $index) use ($columns, &$columnNames): bool { | ||||||
| $result = empty(array_diff($index->columnNames, $columns)); | ||||||
|
|
||||||
| if ($result) { | ||||||
| $columnNames[] = $index->columnNames; | ||||||
| } | ||||||
|
|
||||||
| return $result; | ||||||
| }, | ||||||
| ), | ||||||
| ); | ||||||
|
|
||||||
| if (empty($columnNames)) { | ||||||
| return []; | ||||||
| // Find the first index that is fully covered by the specified column list. | ||||||
| foreach ($indexes as $index) { | ||||||
| if (empty(array_diff($index->columnNames, $columns))) { | ||||||
| $indexes = [$index]; | ||||||
| return $index->columnNames; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return array_unique(array_merge(...$columnNames)); | ||||||
| $indexes = []; | ||||||
| return []; | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.