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

fix: drop trigram index on identifiers #3827

Merged
merged 4 commits into from
Mar 22, 2024
Merged
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
1 change: 1 addition & 0 deletions internal/client-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
12 changes: 3 additions & 9 deletions persistence/sql/identity/persister_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ INNER JOIN identity_credentials
AND identity_credentials.identity_credential_type_id = (
SELECT id
FROM identity_credential_types
WHERE name = ?
WHERE name = ?
)
WHERE identity_credentials.config ->> '%s' = ?
AND identities.nid = ?
Expand Down Expand Up @@ -824,14 +824,8 @@ func (p *IdentityPersister) ListIdentities(ctx context.Context, params identity.
identifier := params.CredentialsIdentifier
identifierOperator := "="
if identifier == "" && params.CredentialsIdentifierSimilar != "" {
identifier = params.CredentialsIdentifierSimilar
identifierOperator = "%"
switch con.Dialect.Name() {
case "postgres", "cockroach":
default:
identifier = "%" + identifier + "%"
identifierOperator = "LIKE"
}
identifier = x.EscapeLikePattern(params.CredentialsIdentifierSimilar) + "%"
identifierOperator = "LIKE"
}

if len(identifier) > 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS btree_gin;

CREATE INDEX identity_credential_identifiers_nid_identifier_gin ON identity_credential_identifiers USING GIN (nid, identifier gin_trgm_ops);
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX identity_credential_identifiers_nid_identifier_gin;
10 changes: 10 additions & 0 deletions x/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package x

import "strings"

func EscapeLikePattern(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "\\", "\\\\"), "%", "\\%"), "_", "\\_")
}
34 changes: 34 additions & 0 deletions x/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package x

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestEscapeLikePattern(t *testing.T) {
for name, tc := range map[string]struct {
input string
expected string
}{
"empty": {
input: "",
expected: "",
},
"no escape": {
input: "foo",
expected: "foo",
},
"escape": {
input: "foo%bar_baz\\",
expected: "foo\\%bar\\_baz\\\\",
},
} {
t.Run(name, func(t *testing.T) {
require.Equal(t, tc.expected, EscapeLikePattern(tc.input))
})
}
}
Loading