Skip to content

Commit

Permalink
fix: drop trigram index on identifiers (#3827)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Mar 22, 2024
1 parent cd92f2a commit 8f8fd90
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 9 deletions.
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
Empty file.
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);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX identity_credential_identifiers_nid_identifier_gin;
Empty file.
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))
})
}
}

0 comments on commit 8f8fd90

Please sign in to comment.