Repository navigation
fix(postgres): keep case-variant id deletes consistent and share id collation #1066
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
62f8215
fix(documents): delete and purge a document by its stored id
HarshMN2345 ccb5569
fix(postgres): compare collection creators by their exact id
HarshMN2345 27bc55c
refactor(builder): share id collation between SQLite and Postgres
HarshMN2345 68931e3
test(postgres): cover id collation in builder filters and joins
HarshMN2345 e3075ee
test(postgres): cover case-variant ids and the _uid index in both tab…
HarshMN2345 22c0a1f
fix(documents): purge the stored id only when it differs from the req…
HarshMN2345 8874099
test(postgres): rename an index whose collection id differs only by case
HarshMN2345 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Database\Builder; | ||
|
|
||
| use Utopia\Query\Builder\JoinBuilder; | ||
| use Utopia\Query\Method; | ||
| use Utopia\Query\Query; | ||
|
|
||
| /** | ||
| * Document ids are unique in the collation of their unique indexes, and an engine only uses an index for a | ||
| * comparison made in the index's collation, so equality on an id column, and a join ON an id column, compares in | ||
| * that collation. The using builder names the collation in COLLATION and decides which columns, and which joins, | ||
| * are collated. | ||
| */ | ||
| trait CollatesDocumentIds | ||
| { | ||
| abstract private function isCollated(string $column): bool; | ||
|
|
||
| abstract private function collatesJoin(string $left, string $right): bool; | ||
|
|
||
| #[\Override] | ||
| public function compileJoin(Query $query): string | ||
| { | ||
| return $this->collateJoin(parent::compileJoin($query), $this->onComparisons($query->getJoinOnQueries())); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function compileJoinWithBuilder(Query $query, JoinBuilder $joinBuilder): string | ||
| { | ||
| $comparisons = []; | ||
| foreach ($joinBuilder->ons as $on) { | ||
| $comparisons[] = [$on->left, $on->operator, $on->right]; | ||
| } | ||
|
|
||
| return $this->collateJoin(parent::compileJoinWithBuilder($query, $joinBuilder), $comparisons); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $values | ||
| */ | ||
| #[\Override] | ||
| protected function compileIn(string $attribute, array $values, ?string $column = null): string | ||
| { | ||
| return parent::compileIn($this->collate($attribute, $column), $values, $column); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $values | ||
| */ | ||
| #[\Override] | ||
| protected function compileNotIn(string $attribute, array $values, ?string $column = null): string | ||
| { | ||
| return parent::compileNotIn($this->collate($attribute, $column), $values, $column); | ||
| } | ||
|
|
||
| private function collate(string $attribute, ?string $column): string | ||
| { | ||
| return $column !== null && $this->isCollated($column) ? $attribute.' COLLATE '.self::COLLATION : $attribute; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<array{string, string, string}> $comparisons | ||
| */ | ||
| private function collateJoin(string $sql, array $comparisons): string | ||
| { | ||
| foreach ($comparisons as [$left, $operator, $right]) { | ||
| if (! \in_array($operator, ['=', '!=', '<>'], true) || ! $this->collatesJoin($left, $right)) { | ||
| continue; | ||
| } | ||
|
|
||
| $comparison = ' '.$operator.' '.$this->resolveAndWrap($right); | ||
| $wrappedLeft = $this->resolveAndWrap($left); | ||
| $sql = \str_replace($wrappedLeft.$comparison, $wrappedLeft.' COLLATE '.self::COLLATION.$comparison, $sql); | ||
| } | ||
|
|
||
| return $sql; | ||
| } | ||
|
|
||
| /** | ||
| * The last segment of a column, after the attribute hooks resolve it. | ||
| */ | ||
| private function columnName(string $column): string | ||
| { | ||
| $resolved = $this->resolveAttribute($column); | ||
| $separator = \strrpos($resolved, '.'); | ||
|
|
||
| return $separator === false ? $resolved : \substr($resolved, $separator + 1); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $queries | ||
| * @return list<array{string, string, string}> | ||
| */ | ||
| private function onComparisons(array $queries): array | ||
| { | ||
| $comparisons = []; | ||
| foreach ($queries as $query) { | ||
| if (! $query instanceof Query) { | ||
| continue; | ||
| } | ||
|
|
||
| $comparisons = match ($query->getMethod()) { | ||
| Method::On => [...$comparisons, ...$this->comparison($query->getValues())], | ||
| Method::And, Method::Or => [...$comparisons, ...$this->onComparisons($query->getValues())], | ||
| default => $comparisons, | ||
| }; | ||
| } | ||
|
|
||
| return $comparisons; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $values | ||
| * @return list<array{string, string, string}> | ||
| */ | ||
| private function comparison(array $values): array | ||
| { | ||
| [$left, $operator, $right] = [$values[0] ?? null, $values[1] ?? null, $values[2] ?? null]; | ||
| if (! \is_string($left) || ! \is_string($operator) || ! \is_string($right) || $left === '' || $right === '') { | ||
| return []; | ||
| } | ||
|
|
||
| return [[$left, $operator, $right]]; | ||
| } | ||
| } |
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.
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.