Skip to content

fix(tables): ensure_tables() compares key types, and tables created meanwhile count as existing - #123

Merged
kalaspuff merged 4 commits into
issues-stack/8-close-waits-for-hooksfrom
issues-stack/9-ensure-tables-key-types
Oct 31, 2026
Merged

kalaspuff merged 4 commits into
issues-stack/8-close-waits-for-hooksfrom
issues-stack/9-ensure-tables-key-types

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

What and why

ensure_tables() promises a TypeError when a table's key differs from the models, but it compared the names of the key attributes only. A table created with ("sensor", "N"), ("seq", "S") passed for a model with sensor: str, seq: int, and every later write failed with "Type mismatch for key". It now compares the types too, from the table description's attribute definitions, for the table's key and for the keys of indexes with the same name. The messages name each attribute with its type (has the key sensor (N), seq (S), the models expect sensor (S), seq (N)).

Concurrent create_tables(..., exist_ok=True) or ensure_tables() calls (two replicas starting together, pytest-xdist workers) raised ResourceInUseError: they check that a table exists and then send CreateTable, and another process could create the table in between. Both now treat a ResourceInUseError from CreateTable (not from the TTL step after it) as such a table: they wait until it is active (with wait=True), create_tables(exist_ok=True) skips it, and ensure_tables() checks and completes it like any existing table. create_tables() without exist_ok still raises.

Verified with integration tests on the in-memory backend and MiniStack, which fail without the fix: key attributes of other types on the table and on a global index raise TypeError, and a middleware that creates the table right after the existence check shows both calls succeed; a unit test shows that a ResourceInUseError of UpdateTimeToLive still raises. make check passes.

Fixes #86

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (tables guide, docstrings, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No: what now raises failed at every write before

Worth confirming

  • Assumption: a ResourceInUseError from CreateTable after the existence check means another process created the table. DynamoDB also answers it for a table that is still being deleted; in that case the wait for the table to become active runs until its timeout.

fctl session


Note

Low risk. It changes how ensure_tables() and create_tables(exist_ok=True) treat tables that already exist or appear mid-call. The new checks only add TypeErrors for key type mismatches and retry-free handling of one race.

What it does. ensure_tables() now compares the key attribute types of the table and of its global secondary indexes with the models, and raises TypeError when they differ, instead of only comparing names. A table that another writer creates between the existence check and CreateTable is now treated as an existing table by ensure_tables() and by create_tables(exist_ok=True), and only a ResourceInUseError raised by CreateTable itself counts as that race. Tests, the tables guide and the changelog are updated.

Stricter ensure_tables(). Applications whose existing tables have key attributes of another type than their models now get a TypeError from ensure_tables() at startup where it passed before.

fctl review's first look at 4ced2b4 · 6 files, +129 −27 · Review agent settings

fctl-sh Bot and others added 2 commits October 31, 2026 01:12
…eanwhile count as existing

sync_with compared the names of key attributes only, so a table whose
key attributes have other types than the models passed ensure_tables()
and every write failed later with "Type mismatch for key". It now
compares the types of the table's and its indexes' keys, which the
description's attribute definitions hold.

create_tables(exist_ok=True) and ensure_tables() checked that a table
exists and then sent CreateTable: a table that another process created
in between raised ResourceInUseError. They now wait for that table and
treat it as existing.

Fixes #86

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/client.py Resolved
fctl-sh Bot and others added 2 commits October 31, 2026 01:25
…created meanwhile

create_from() also enables TTL after CreateTable, and a
ResourceInUseError from UpdateTimeToLive (a table still CREATING with
wait=False) was taken for a lost race: create_tables(exist_ok=True)
skipped the table and never enabled TTL. The handlers now check that
the error came from CreateTable.

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff added this pull request to stack #124 October 31, 2026 01:30
@kalaspuff
kalaspuff merged commit 9414542 into main Oct 31, 2026
23 checks passed
@kalaspuff
kalaspuff deleted the issues-stack/9-ensure-tables-key-types branch October 31, 2026 01:34
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.

ensure_tables() ignores key types, and concurrent table creation raises ResourceInUseError

1 participant