Repository navigation
fix(tables): ensure_tables() compares key types, and tables created meanwhile count as existing - #123
Merged
kalaspuff merged 4 commits intoOct 31, 2026
Conversation
…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]>
…/9-ensure-tables-key-types
This comment has been minimized.
This comment has been minimized.
…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]>
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
ensure_tables()promises aTypeErrorwhen 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 withsensor: 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)orensure_tables()calls (two replicas starting together, pytest-xdist workers) raisedResourceInUseError: they check that a table exists and then sendCreateTable, and another process could create the table in between. Both now treat aResourceInUseErrorfromCreateTable(not from the TTL step after it) as such a table: they wait until it is active (withwait=True),create_tables(exist_ok=True)skips it, andensure_tables()checks and completes it like any existing table.create_tables()withoutexist_okstill 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 aResourceInUseErrorofUpdateTimeToLivestill raises.make checkpasses.Fixes #86
Checklist
make checkpasses locally (lint, types, clippy, consistency, tests)Worth confirming
ResourceInUseErrorfromCreateTableafter 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()andcreate_tables(exist_ok=True)treat tables that already exist or appear mid-call. The new checks only addTypeErrors 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 raisesTypeErrorwhen they differ, instead of only comparing names. A table that another writer creates between the existence check andCreateTableis now treated as an existing table byensure_tables()and bycreate_tables(exist_ok=True), and only aResourceInUseErrorraised byCreateTableitself 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
TypeErrorfromensure_tables()at startup where it passed before.fctl review's first look at
4ced2b4· 6 files, +129 −27 · Review agent settings