Skip to content

Migrate Bestbook FastAPI queries to async DB - #14112

Merged
RayBB merged 6 commits into
internetarchive:masterfrom
Sanket17052006:13826/feature/bestbook-awards-async-db
Oct 27, 2026
Merged

RayBB merged 6 commits into
internetarchive:masterfrom
Sanket17052006:13826/feature/bestbook-awards-async-db

Conversation

@Sanket17052006

@Sanket17052006 Sanket17052006 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Part of #13826

This PR migrates the Bestbook awards FastAPI paths to async database access.

Technical

  • Migrate Bestbook award reads, counts, adds, and removals to the async database helpers.
  • Make award validation, including the Bookshelves read-status check, async.
  • Keep a get_count_sync bridge for its legacy caller (Work.resolve_redirect_chain); remove the unused Work.get_awards, Work.check_if_user_awarded and Work.get_award_by_username helpers rather than bridging them.
  • Handle concurrent duplicate submissions while preserving the existing award validation errors.
  • Normalize numeric work IDs and OLIDs for the awards count endpoint, and return 422 (instead of 500) for a malformed work_id.
  • A successful POST /works/OL{id}W/awards.json now also returns the new award's award id.
  • Bookshelves.user_has_read_work is async and uses the single-work get_users_read_status_of_work.
  • Tests live in openlibrary/tests/core/test_bestbook.py (one test file per module).

Testing

  • docker compose run --rm home pytest -q openlibrary/tests/fastapi/ -x — Passed: 648 tests.
  • docker compose run --rm home pytest -q openlibrary/tests/core/test_bestbook.py openlibrary/tests/core/test_bookshelves.py openlibrary/tests/fastapi/test_bestbook.py openlibrary/tests/core/test_models.py — Passed; integration tests deselected by default.
  • uv run --with-requirements requirements_test.txt pytest -q -m integration openlibrary/tests/core/test_bestbook.py openlibrary/tests/core/test_bookshelves.py — Passed: PostgreSQL integration tests.
  • curl -i 'http://localhost:8080/awards/count.json?work_id=123' and curl -i 'http://localhost:8080/awards/count.json?work_id=OL123W' — Passed: both returned HTTP 200.

Screenshot

N/A

Stakeholders

@RayBB

@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR, @Sanket17052006!

A reviewer must first be assigned. There are currently 124 open PRs of equal or higher priority ahead of yours.

PR triage checklist (maintainers / Pierre)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — no failing check-runs
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

Note

This comment was automatically generated by Pierre, Open Library's PR first-touch assistant, on behalf of @mekarpeles. This is NOT a code review or sign-off.

RayBB added 5 commits October 27, 2026 13:53
work_id values like "abc", "OLxyzW" or "" raised ValueError/IndexError
while parsing and returned 500 (master too). Validate the parameter as a
numeric work ID or work OLID so bad input gets a 422.
The removed tests asserted exact SQL strings or substrings, or exercised
get_awards_sync through a fake async_bridge; the Postgres integration test
runs the same queries for real (and now also checks get_count with an
OLID). Keep the concurrent-conflict recheck test, which the integration
test can't reach, and test_remove_requires_a_filter.
Work.get_awards, Work.check_if_user_awarded and Work.get_award_by_username
have no callers anywhere (Python, templates, macros, JS), and they were
the only users of the new get_awards_sync bridge. Drop all four.
Resolve the Bookshelves.user_has_read_work conflict with internetarchive#14069: keep it
async (its only caller, Bestbook._check_award_conditions, awaits it) and
use the single-work get_users_read_status_of_work that master now has,
instead of going through the batch method. Drop the unit test that
patched the batch method; the bestbook integration test covers both
branches against Postgres.
One test file per module: the Bestbook model's unit and Postgres
integration tests live in tests/core/test_bestbook.py, not a separate
_async file.

@RayBB RayBB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Sanket17052006, this is a clean migration! ON CONFLICT DO NOTHING plus re-validation is exactly right: 30 identical award requests in parallel gave one row, 29 of the usual error messages and no 500s, and /awards/count.json now accepts OL123W (it 500'd on master). I pushed a few small commits on top: a 422 for malformed work_ids on the count endpoint, removed get_awards_sync and the three unused Work award helpers it served, merged master and made user_has_read_work use the new async get_users_read_status_of_work from #14069, trimmed the SQL-string unit tests the integration test covers, and moved the tests into test_bestbook.py. 🎉

@RayBB
RayBB merged commit cdd1f0d into internetarchive:master Oct 27, 2026
5 checks passed
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.

3 participants

⌂ Proxy