Repository navigation
Bound card writes and ids, map missing decks to 404, cache the card update - #98
Merged
Merged
Conversation
…ard update Cap card lists at MAX_CARDS_PER_REQUEST and ids at bigint so oversized input is a 422 instead of a 500. Map ForeignKeyError to 404. Make LOG_LEVEL case-insensitive and pass it to app logging. Order deck detail cards by id. Send PUT cards through unnest so the statement is cached and prepared once. Add a (deck_id, id) index for card pagination.
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.
Fixes the confirmed 500s and two performance gaps that came up in a review of the template. Each fix has a test that failed on main first.
Bugs
PUT /decks/{id}/cards/sent four bind parameters per card, and asyncpg rejects more than 32767, so 8192 cards gave a 500. That fits well under the 1 MB body limit. POST and PUT now accept at mostMAX_CARDS_PER_REQUEST(1000) cards and return 422 before any query runs.afterandCardUpdate.idhad no upper bound, so2**63reached asyncpg and failed withOverflowErroras a 500. They are now bounded byschemas.MAX_IDand return 422. Path ids also requiregt=0now, so/decks/0/returns 422 instead of 404. Two existing tests that used id 0 for "not found" now use 999.ForeignKeyError, which had no handler, so it was a 500. It now maps to 404, the same as a missing deck everywhere else.LOG_LEVEL.LogLevels(settings.log_level)accepted only lowercase values, soLOG_LEVEL=INFOstopped the server at startup. The setting only reached Granian, soLOG_LEVEL=warningleft app logs at INFO. The field is now a case-insensitiveLogLevelsand also setslogging_log_levelthrough granian'slog_levels_map.cardsrelationship had noorder_by, so after an update the cards came back in heap order. The new test reproduced this on main. It now usesorder_by="Card.id", the same order as the list endpoints.Performance
VALUES. Incache_key.py,visit_dml_multi_valuessetsNO_CACHEwith the comment "multivalues are simply not cacheable right now". So every PUT compiled from scratch. The SQL text also changed with the number of cards, so each new count cost asyncpg a Parse round trip and a slot in its 100-entry statement cache. The statement now readsFROM unnest($ids::BIGINT[], $fronts::VARCHAR[], ...) AS updates(id, front, back, hint). That keeps one SQL text for any count and four parameters in total. A test checks that PUTs with one card and with two cards send identical SQL.list_for_deckrunsWHERE deck_id = ? AND id > ? ORDER BY id LIMIT n. The only indexes were the primary key and the unique(deck_id, front), so Postgres had to sort a whole deck or walk the primary key and filter. This addsix_cards_deck_id_idand a migration. The pytest-alembic suite checks that the model and the migration match.Small cleanups
update_cardsno longer setsupdated_at=sa.func.now()by hand. The column'sonupdatealready fires for a Core UPDATE, and deck updates rely on it, so both tables now takeupdated_atfrom the same clock.test_update_cards_moves_updated_atstill passes.DeckWithCardsdocstring said selectinload, but the code uses joinedload.pyproject.tomlexemptsapp/api/*.pyfrom ruff'sTCrules, because FastAPI reads route annotations at runtime and ruff would otherwise move the sharedPathIdimport underTYPE_CHECKING.