-
-
Notifications
You must be signed in to change notification settings - Fork 963
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(identity): slow query performance on MySQL
Closes #2278
- Loading branch information
Showing
4 changed files
with
38 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"github.com/ory/x/randx" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
@@ -567,10 +568,25 @@ func TestPool(ctx context.Context, conf *config.Config, p interface { | |
require.Equal(t, sqlcon.ErrNoRows, errorsx.Cause(err)) | ||
}) | ||
|
||
transform := func(k int, value string) string { | ||
switch k % 5 { | ||
case 0: | ||
value = strings.ToLower(value) | ||
case 1: | ||
value = strings.ToUpper(value) | ||
case 2: | ||
value = " " + value | ||
case 3: | ||
value = value + " " | ||
} | ||
return value | ||
} | ||
|
||
t.Run("case=create and find", func(t *testing.T) { | ||
addresses := make([]identity.VerifiableAddress, 15) | ||
for k := range addresses { | ||
addresses[k] = createIdentityWithAddresses(t, "recovery.TestPersister.Create"+strconv.Itoa(k)+"@ory.sh") | ||
value := randx.MustString(16, randx.AlphaLowerNum) + "@ory.sh" | ||
addresses[k] = createIdentityWithAddresses(t, transform(k, value)) | ||
require.NotEmpty(t, addresses[k].ID) | ||
} | ||
|
||
|
@@ -579,19 +595,20 @@ func TestPool(ctx context.Context, conf *config.Config, p interface { | |
actual.UpdatedAt = actual.UpdatedAt.UTC().Truncate(time.Hour * 24) | ||
expected.CreatedAt = expected.CreatedAt.UTC().Truncate(time.Hour * 24) | ||
expected.UpdatedAt = expected.UpdatedAt.UTC().Truncate(time.Hour * 24) | ||
expected.Value = strings.TrimSpace(strings.ToLower(expected.Value)) | ||
assert.EqualValues(t, expected, actual) | ||
} | ||
|
||
for k, expected := range addresses { | ||
t.Run("method=FindVerifiableAddressByValue", func(t *testing.T) { | ||
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { | ||
actual, err := p.FindVerifiableAddressByValue(ctx, expected.Via, expected.Value) | ||
actual, err := p.FindVerifiableAddressByValue(ctx, expected.Via, transform(k+1, expected.Value)) | ||
require.NoError(t, err) | ||
compare(t, expected, *actual) | ||
|
||
t.Run("not if on another network", func(t *testing.T) { | ||
_, p := testhelpers.NewNetwork(t, ctx, p) | ||
_, err := p.FindVerifiableAddressByValue(ctx, expected.Via, expected.Value) | ||
_, err := p.FindVerifiableAddressByValue(ctx, expected.Via, transform(k+1, expected.Value)) | ||
require.ErrorIs(t, err, sqlcon.ErrNoRows) | ||
}) | ||
}) | ||
|
@@ -600,9 +617,9 @@ func TestPool(ctx context.Context, conf *config.Config, p interface { | |
}) | ||
|
||
t.Run("case=update", func(t *testing.T) { | ||
address := createIdentityWithAddresses(t, "[email protected]") | ||
address := createIdentityWithAddresses(t, "[email protected] ") | ||
|
||
address.Value = "new-code" | ||
address.Value = "new-codE " | ||
require.NoError(t, p.UpdateVerifiableAddress(ctx, &address)) | ||
|
||
t.Run("not if on another network", func(t *testing.T) { | ||
|
@@ -648,7 +665,7 @@ func TestPool(ctx context.Context, conf *config.Config, p interface { | |
actual, err := p.FindVerifiableAddressByValue(ctx, identity.VerifiableAddressTypeEmail, "[email protected]") | ||
require.NoError(t, err) | ||
assert.Equal(t, identity.VerifiableAddressTypeEmail, actual.Via) | ||
assert.Equal(t, "verification.TestPersister.Update-Identity[email protected]", actual.Value) | ||
assert.Equal(t, "verification.testpersister.update-identity[email protected]", actual.Value) | ||
|
||
t.Run("can not find if on another network", func(t *testing.T) { | ||
_, p := testhelpers.NewNetwork(t, ctx, p) | ||
|
@@ -690,7 +707,7 @@ func TestPool(ctx context.Context, conf *config.Config, p interface { | |
actual, err := p.FindVerifiableAddressByValue(ctx, identity.VerifiableAddressTypeEmail, strings.ToUpper("verification.TestPersister.Update-Identity-case-insensitive-next@ory.sh")) | ||
require.NoError(t, err) | ||
assert.Equal(t, identity.VerifiableAddressTypeEmail, actual.Via) | ||
assert.Equal(t, "verification.TestPersister.Update-Identity[email protected]", actual.Value) | ||
assert.Equal(t, "verification.testpersister.update-identity[email protected]", actual.Value) | ||
|
||
t.Run("can not find if on another network", func(t *testing.T) { | ||
_, p := testhelpers.NewNetwork(t, ctx, p) | ||
|
@@ -775,7 +792,7 @@ func TestPool(ctx context.Context, conf *config.Config, p interface { | |
actual, err := p.FindRecoveryAddressByValue(ctx, identity.RecoveryAddressTypeEmail, "[email protected]") | ||
require.NoError(t, err) | ||
assert.Equal(t, identity.RecoveryAddressTypeEmail, actual.Via) | ||
assert.Equal(t, "recovery.TestPersister.Update[email protected]", actual.Value) | ||
assert.Equal(t, "recovery.testpersister.update[email protected]", actual.Value) | ||
|
||
t.Run("can not find if on another network", func(t *testing.T) { | ||
_, p := testhelpers.NewNetwork(t, ctx, p) | ||
|
@@ -811,7 +828,7 @@ func TestPool(ctx context.Context, conf *config.Config, p interface { | |
actual, err := p.FindRecoveryAddressByValue(ctx, identity.RecoveryAddressTypeEmail, strings.ToUpper("[email protected]")) | ||
require.NoError(t, err) | ||
assert.Equal(t, identity.RecoveryAddressTypeEmail, actual.Via) | ||
assert.Equal(t, "recovery.TestPersister.Update[email protected]", actual.Value) | ||
assert.Equal(t, "recovery.testpersister.update[email protected]", actual.Value) | ||
|
||
t.Run("can not find if on another network", func(t *testing.T) { | ||
_, p := testhelpers.NewNetwork(t, ctx, p) | ||
|
Empty file.
2 changes: 2 additions & 0 deletions
2
persistence/sql/migrations/templates/20220301102702_identity_address_performance.up.sql
This file contains 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,2 @@ | ||
UPDATE identity_recovery_addresses SET value = LOWER(value) WHERE TRUE; | ||
UPDATE identity_verification_addresses SET value = LOWER(value) WHERE TRUE; |
This file contains 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