Add support for handling existing records with version 0 #6643
+215
−25
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.
Notes
This PR is part of a two part approach on a feature branch. The current PR focuses specifically on unblocking existing SDK v1 records by resolving the ambiguity for update operations, while a followup PR will address the remaining gaps to allow NEW records to be created with explicit version=0 values.
Context And Problem
In July 2024, PR #6019 introduced custom
startAtandincrementByfunctionality to the VersionedRecordExtension, allowing users to configure initial version values and increment amounts. However, this inadvertently created a bug in theisInitialVersion()method that incorrectly identifies existing records with version = startAt as initial versions, causing them to be treated withattribute_not_exists.This bug became a significant blocker for SDK v1 to v2 migrations. In SDK v1, the SDK allows the option to start at version 0, meaning some existing production records might have version = 0. When migrating to SDK v2 with the default startAt = 0 configuration, these existing records are incorrectly identified as initial versions, causing update operations to fail with condition check exceptions.
The problem:
This is caused by an ambiguity problem where the client side code cannot distinguish between a new record where the user explicitly sets version = startAt vs an existing record that happens to have version = startAt. The isInitialVersion() method made incorrect assumptions by treating both cases identically, leading to the wrong DynamoDB condition expressions being generated.
The fix:
We resolve the ambiguity by pushing the disambiguation logic to DynamoDB using an OR condition.
Key Changes:
Handle the ambiguous case with OR condition:
This way we can account for the 3 possible scenarios:
By using the OR operator in the DDB expression, we allow the DDB server to disambiguate this for us.