Repository navigation
fix(repository): unique models check delete conditions on missing items, and stale deletes and updates read again - #110
Merged
kalaspuff merged 9 commits intoOct 31, 2026
Conversation
…ms, and stale deletes and updates read again Deletes of models with unique attributes read the stored item for its guards, and returned None when there was none, without sending the caller's condition; in a transaction the step was dropped, so a delete used as the guard of a transaction let the rest commit. Without unique attributes, DynamoDB checks the condition on the missing item. Such a delete now sends the condition with "still no item stored", so it holds or fails as for other models; when an item was stored after the read, delete() plans again from it. A delete with nothing to delete and nothing to check runs no request but is finalized like other models' (after_delete hooks, the instance unloaded). Deletes and updates of loaded instances of unversioned unique models raised an unexplained ConditionalCheckFailedError when another writer had changed a unique value since the instance was loaded, and delete() failed the same way after another writer deleted the item. Like put(), they now read the stored item and plan again once; transactions, which cannot read again, say to refresh() the instance and retry. Upserts are left out: one planned again after a delete would create the item without the unique values the actions do not set. delete() of an instance without unique values set, whose item another writer deleted, returned the item as loaded; it returns None now. Refs #81 Co-authored-by: Carl Oscar Aaro <[email protected]>
…hooks Co-authored-by: Carl Oscar Aaro <[email protected]>
This comment has been minimized.
This comment has been minimized.
…stance Upserts of a loaded instance whose unique values another writer changed are not written again (planned again after a delete elsewhere, one would create the item without the unique values its actions do not set), so they raised the unexplained ConditionalCheckFailedError, also in transactions. Their plan is now marked stale like other updates', so the error says to refresh() the instance and retry; update() still does not plan them again. The guides said every unversioned update() writes again: they now name the exception. Co-authored-by: Carl Oscar Aaro <[email protected]>
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
kalaspuff
deleted the
fctl/59-https-github-com-kalaspuff-dynamoxy-issu
branch
October 31, 2026 00:19
4 tasks done
kalaspuff
added a commit
that referenced
this pull request
Oct 31, 2026
…ack, and collections refused (#129) ## What and why Three ways unique guards got out of step with their items (#82): - **Upserts that race with creates:** an upsert that set a unique value read the item and, finding none, wrote without requiring that it was still missing (`put()` already adds `partition.not_exists()` in this case). When another writer created the item between the read and the write, the upsert overwrote its unique value and never deleted the other writer's guard, which stayed taken forever. The upsert now requires that the item is still missing. When that fails because an item appeared, it reads the item again and writes once more, freeing the replaced value's guard. This is the replan that conditional deletes got in #110. In a transaction it cancels with a `ConditionalCheckFailedError` that says another writer stored the item and to retry. Upserts of stale loaded instances still raise as before. The same condition covers upserts that read the item to render a derived attribute (`Player.score.add(1)` with `rank="SCORE#{score:08d}"`): they wrote a rank rendered from the missing item over the created one (score 6, rank `SCORE#00000001`), and now render it from the created item. - **Unique floats could not be written back:** `_plan_guards` compared the stored `Decimal("0.1")` with the float `0.1` using `==`, so the plan deleted and put the same guard item, which DynamoDB refuses. `save()`, `put()` and `update(Place.latitude.set(0.1))` of an instance read back failed with the value unchanged. Guards now compare stored identities, as keys already do. - **Collections were accepted as unique:** the guard key is the value's text, and a set's text depends on the process's hash seed. `unique=True` on a set, list, map or document field is now a `TypeError` when the model's schema is built, and a collection stored in a unique `Any` field raises `ValueError` before any write. Verified with integration tests on the in-memory backend and MiniStack, each failing without the fix: a middleware that creates the item right after the upsert's `GetItem` (for a unique value, standalone and in a transaction, and for a derived rank), a float field saved, put and updated after a read, and the refused collection fields and values. `make check` passes. Fixes #82 ## Breaking change `unique=True` on a set, list, map or document field now raises `TypeError` when the model is used. Such guards never had one text per value, so they did not guarantee uniqueness. Drop `unique=True` from the field, or keep a canonical string of the value (`",".join(sorted(tags))`) in a unique field of its own. ## Checklist - [x] Tests cover the change (in-memory backend and MiniStack) - [x] Docs updated (constraints guide, AGENT_GUIDE, changelog) - [x] `make check` passes locally (lint, types, clippy, consistency, tests) - [x] Breaking change? Marked with `!` in the title and explained above ## Worth confirming - **Decision:** collections are refused rather than given a canonical text, as the issue offered both. The refusal is a `TypeError` for declared collection types and a `ValueError` for collection values of `Any` fields; booleans stay allowed. - **Decision:** an upsert that loses the race to a create is planned again from the created item, so it updates that item and replaces its unique value, instead of failing. [fctl session](https://app.fctl.sh/s/ses_01M67V41FM3GB8WN7TS9ZMAKDD) <!-- FCTL_REVIEW_SUMMARY --> --- > [!NOTE] > **Medium risk.** It changes how upserts and writes of models with unique fields plan their conditions and guard items, which every such write goes through, and it makes models that declare unique collection fields fail where they were accepted before. > > **What it does.** Fixes three ways unique guard items could get out of step with their items. An upsert that finds no item now requires that the item is still missing, and when another writer created it in the meantime, it reads the item again and writes once more, so the replaced value's guard is freed and derived keys such as `rank` are rendered from the created item. Guards of unique floats are compared as stored values, so a float read back no longer deletes and puts the same guard item. Unique set, list and map fields are refused with a `TypeError` when the schema is built, and collection values in unique `Any` fields raise a `ValueError` before any write. > > **Breaking change.** Models that declare `unique=True` on a set, list, map or document field now raise a `TypeError` as soon as their schema is used, so applications with such models stop working until they remove the option or keep a canonical string in a unique field of its own. > > <sub>[fctl review](https://app.fctl.sh/s/ses_01M67XAJZV10F0QA39N1RB4D8K)'s first look at [`3ba79de`](3ba79de) · 8 files, +185 −12 · [Review agent settings](https://app.fctl.sh/p/dynamoxy?tab=reviews)</sub> <!-- /FCTL_REVIEW_SUMMARY --> --------- Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com> Co-authored-by: Carl Oscar Aaro <[email protected]>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What and why
Two gaps in deletes and updates of models with unique attributes.
Conditional deletes of missing items. Such a delete reads the stored item for its guards, and when none was stored it returned
Nonewithout sending the caller's condition; in a transaction the step was dropped, sotx.delete(User.key(...), model=User, condition=...)used as a guard let the rest of the transaction commit. Without unique attributes, DynamoDB checks the condition on the missing item. Such a delete now sends aDeletethat requires the caller's condition and that no item is stored, so it fails (ConditionalCheckFailedError, or a canceled transaction) or holds as it does for other models. A delete of a missing item without a condition still sends nothing, but is now finalized like other models' deletes:after_deletehooks run and the instance is unloaded.Stale instances of unversioned models.
delete()andupdate(..., Profile.handle.set(...))of a loaded instance raised an unexplainedConditionalCheckFailedError("the condition of delete failed", with no condition given) when another writer had changed a unique value since the load, anddelete()failed the same way when another writer had deleted the item, where other models returnNone. Likeput(), they now read the stored item and plan again, once. Transactions cannot read again, and upserts are not planned again, so their error says torefresh()the instance and retry. Also,delete()of an instance with no unique values set, whose item another writer deleted, returned the item as loaded; it now returnsNone.Verified with new integration tests on the in-memory backend and MiniStack, which fail without the fix: conditional deletes of missing items alone and in transactions, an item stored between the read and the delete (a middleware writes it right after the
GetItem), stale deletes and updates with their hooks counted, stale upserts, and the transaction messages.make checkpasses.Fixes #81
Checklist
AGENT_GUIDE.md, changelog)make checkpasses locally (lint, types, clippy, consistency, Rust and Python tests)Worth confirming
delete()reads again and deletes the item with its guards; a transaction is canceled ("another writer stored the item after it was read: retry") rather than leaving guards without their item.after_deletehooks and leaves the instance unloaded and bound, as for models without unique attributes. Before, onlybefore_deleteran.update(..., upsert=True)of a loaded instance) are not planned again: they raiseConditionalCheckFailedErrorwith therefresh()hint, alone and in transactions. If another writer had deleted the item, planning again would create it without the unique values the upsert's actions do not set.refresh()hint), where a model without unique attributes would commit. Committing instead would need a read at plan time.fctl session
Note
Medium risk. It changes how deletes and updates of models with unique attributes are planned, retried and checked inside transactions, so conditions, hooks and error behaviour that callers rely on shift.
What it does. Deletes of models with unique attributes now send the caller's condition (or the version check) when no item is stored, so a conditional delete of a missing item fails as it does for other models, and a transaction that uses one as a guard no longer commits. Stale
delete()andupdate()calls on loaded unversioned instances now read the stored item and plan once more, likeput(), while transactions and upserts raiseConditionalCheckFailedErrorwith a hint torefresh()the instance and retry. A delete of a missing item without a condition still writes nothing but now runsafter_deletehooks and unloads the instance.Behaviour change. A conditional delete of a missing item in a model with unique attributes now raises
ConditionalCheckFailedErroror cancels the transaction where it returnedNoneor was skipped, and deletes of missing items now runafter_deletehooks.fctl review's first look at
500c168· 8 files, +343 −54 · Review agent settings