Skip to content

fix(repository): items stored before the model had a version can be saved - #68

Merged
kalaspuff merged 1 commit into
mainfrom
fixes-stack/07-unversioned-items
Oct 30, 2026
Merged

kalaspuff merged 1 commit into
mainfrom
fixes-stack/07-unversioned-items

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Adding version: Attr[int] = dynamoxy.version() to a model with stored items made those items impossible to save. An item written before the model had a version loads with version None. save() then treated the instance like a new one and sent attribute_not_exists(pk), which always failed because the item exists. The error was an unexplained ConditionalCheckFailedError rather than VersionConflictError, refresh() did not help, and tx.save() failed the same way. update() worked, because it checks a version only when there is one.

save() of a loaded instance whose item has no version now requires that the item still exists without a version (attribute_exists(pk) AND attribute_not_exists(version)), and stores version 1. If another writer saved the item first, the call raises VersionConflictError ("stored version 1, expected no version"), and if it was deleted meanwhile, "deleted after it was loaded", without creating it again. A new instance that was never loaded still saves like create(). Conflict messages now say "expected version 3" rather than "expected 3".

Verified with a new integration test on the in-memory backend and MiniStack (a first save, a stale instance, a deleted item and a transaction), and with the whole suite, ruff, pyrefly and the consistency checks.

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (constraints guide, changelog)
  • make check passes locally (lint, types, consistency, tests; no Rust change)
  • Breaking change? No (the message of VersionConflictError changes slightly)

fctl session


Note

Medium risk. It changes the condition that save() sends for loaded instances of versioned models, which every optimistic-locking write goes through, though only for items that have no version.

What it does. Fixes save() for items that were stored before their model got a version() field. Such an instance loads with version None, and save() used to treat it like a new one and demand that the item did not exist, so it always failed with an unexplained ConditionalCheckFailedError. It now requires that the item still exists without a version and stores version 1, and raises VersionConflictError if another writer saved or deleted the item in the meantime, also inside transactions.

Error message wording. VersionConflictError messages now read "expected version 3" instead of "expected 3", and "expected no version" for items that have none, so code or tests that match the old text need updating.

fctl review's first look at 7328e54 · 5 files, +67 −8 · Review agent settings

@kalaspuff
kalaspuff added this pull request to stack #71 October 30, 2026 21:37
@fctl-sh

fctl-sh Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Stack of 5 open pull requests (from a pass over the library for bugs; #61 to #65 are merged already), merged in this order with "Squash and merge":

  1. fix(client): writes whose retries run out after an uncertain attempt are unknown #67 fix(client): writes whose retries run out after an uncertain attempt are unknown
  2. fix(repository): items stored before the model had a version can be saved #68 fix(repository): items stored before the model had a version can be saved ← this pull request
  3. fix(models): members of mixin enums are the plain values of str and int fields #69 fix(models): members of mixin enums are the plain values of str and int fields
  4. fix(query): requests of growing reads ask for at most a page's worth of items #70 fix(query): requests of growing reads ask for at most a page's worth of items
  5. fix(errors): errors pickle, so they cross process boundaries #72 fix(errors): errors pickle, so they cross process boundaries

The findings that are not fixed here are filed as issues (see the next comment on #72).

…aved

A loaded instance whose item was stored without a version (written
before the model gained `version()`) has version None. save() treated
it like a new instance and required `attribute_not_exists(pk)`, so it
failed with an unexplained ConditionalCheckFailedError every time, after
refresh() too, and so did tx.save().

save() of such an instance now requires that the item still exists
without a version, and stores version 1. When another writer saved it
first, or deleted it, the call raises VersionConflictError ("stored
version 1, expected no version", "deleted after it was loaded"). The
conflict messages now say "expected version 1".

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff force-pushed the fixes-stack/07-unversioned-items branch from 42b6f82 to 7328e54 Compare October 30, 2026 21:40
@kalaspuff
kalaspuff merged commit 38caaf3 into main Oct 30, 2026
22 checks passed
@kalaspuff
kalaspuff deleted the fixes-stack/07-unversioned-items branch October 30, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant