-
-
Notifications
You must be signed in to change notification settings - Fork 964
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: do not roll back transaction on partial identity insert error #4211
Merged
zepatrik
merged 6 commits into
master
from
fix/batch-identity-insert-persists-on-failure
Nov 14, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
de962ce
fix: do not roll back transaction on partial insert error
zepatrik 3f45a98
revert: assertion update
zepatrik 32ceb02
test: mysql specific assertion when parts of patch are invalid
zepatrik 630f9a9
Merge remote-tracking branch 'origin/master' into fix/batch-identity-…
zepatrik 914f572
test: delete identities to avoid side effects on other cases
zepatrik 86aea3b
Merge remote-tracking branch 'origin/master' into fix/batch-identity-…
zepatrik 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 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 |
---|---|---|
|
@@ -754,7 +754,7 @@ func TestHandler(t *testing.T) { | |
}) | ||
|
||
t.Run("suite=PATCH identities", func(t *testing.T) { | ||
t.Run("case=fails on > 100 identities", func(t *testing.T) { | ||
t.Run("case=fails with too many patches", func(t *testing.T) { | ||
tooMany := make([]*identity.BatchIdentityPatch, identity.BatchPatchIdentitiesLimit+1) | ||
for i := range tooMany { | ||
tooMany[i] = &identity.BatchIdentityPatch{Create: validCreateIdentityBody("too-many-patches", i)} | ||
|
@@ -767,8 +767,8 @@ func TestHandler(t *testing.T) { | |
t.Run("case=fails some on a bad identity", func(t *testing.T) { | ||
// Test setup: we have a list of valid identitiy patches and a list of invalid ones. | ||
// Each run adds one invalid patch to the list and sends it to the server. | ||
// --> we expect the server to fail all patches in the list. | ||
// Finally, we send just the valid patches | ||
// --> we expect the server to fail only the bad patches in the list. | ||
// Finally, we send just valid patches | ||
// --> we expect the server to succeed all patches in the list. | ||
|
||
t.Run("case=invalid patches fail", func(t *testing.T) { | ||
|
@@ -782,24 +782,23 @@ func TestHandler(t *testing.T) { | |
{Create: &identity.CreateIdentityBody{Traits: json.RawMessage(`"invalid traits"`)}}, // <-- invalid traits | ||
{Create: validCreateIdentityBody("valid", 4)}, | ||
} | ||
expectedToPass := []*identity.BatchIdentityPatch{patches[0], patches[1], patches[3], patches[5], patches[7]} | ||
|
||
// Create unique IDs for each patch | ||
var patchIDs []string | ||
patchIDs := make([]string, len(patches)) | ||
for i, p := range patches { | ||
id := uuid.NewV5(uuid.Nil, fmt.Sprintf("%d", i)) | ||
p.ID = &id | ||
patchIDs = append(patchIDs, id.String()) | ||
patchIDs[i] = id.String() | ||
} | ||
|
||
req := &identity.BatchPatchIdentitiesBody{Identities: patches} | ||
body := send(t, adminTS, "PATCH", "/identities", http.StatusOK, req) | ||
var actions []string | ||
for _, a := range body.Get("identities.#.action").Array() { | ||
actions = append(actions, a.String()) | ||
} | ||
assert.Equal(t, | ||
require.NoErrorf(t, json.Unmarshal(([]byte)(body.Get("identities.#.action").Raw), &actions), "%s", body) | ||
assert.Equalf(t, | ||
[]string{"create", "create", "error", "create", "error", "create", "error", "create"}, | ||
actions, body) | ||
actions, "%s", body) | ||
|
||
// Check that all patch IDs are returned | ||
for i, gotPatchID := range body.Get("identities.#.patch_id").Array() { | ||
|
@@ -811,6 +810,27 @@ func TestHandler(t *testing.T) { | |
assert.Equal(t, "Conflict", body.Get("identities.4.error.status").String()) | ||
assert.Equal(t, "Bad Request", body.Get("identities.6.error.status").String()) | ||
|
||
var identityIDs []uuid.UUID | ||
require.NoErrorf(t, json.Unmarshal(([]byte)(body.Get("identities.#.identity").Raw), &identityIDs), "%s", body) | ||
|
||
actualIdentities, _, err := reg.Persister().ListIdentities(ctx, identity.ListIdentityParameters{IdsFilter: identityIDs}) | ||
require.NoError(t, err) | ||
actualIdentityIDs := make([]uuid.UUID, len(actualIdentities)) | ||
for i, id := range actualIdentities { | ||
actualIdentityIDs[i] = id.ID | ||
} | ||
assert.ElementsMatchf(t, identityIDs, actualIdentityIDs, "%s", body) | ||
|
||
expectedTraits := make(map[string]string, len(expectedToPass)) | ||
for i, p := range expectedToPass { | ||
expectedTraits[identityIDs[i].String()] = string(p.Create.Traits) | ||
} | ||
actualTraits := make(map[string]string, len(actualIdentities)) | ||
for _, id := range actualIdentities { | ||
actualTraits[id.ID.String()] = string(id.Traits) | ||
} | ||
|
||
assert.Equal(t, expectedTraits, actualTraits) | ||
}) | ||
|
||
t.Run("valid patches succeed", func(t *testing.T) { | ||
|
@@ -1928,7 +1948,7 @@ func validCreateIdentityBody(prefix string, i int) *identity.CreateIdentityBody | |
identity.VerifiableAddressStatusCompleted, | ||
} | ||
|
||
for j := 0; j < 4; j++ { | ||
for j := range 4 { | ||
email := fmt.Sprintf("%s-%d-%[email protected]", prefix, i, j) | ||
traits.Emails = append(traits.Emails, email) | ||
verifiableAddresses = append(verifiableAddresses, identity.VerifiableAddress{ | ||
|
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that rolling back the transaction is unwanted, but we do want to return information about which identity inserts failed. Don't we lose that information here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, I misread, looks good!