Skip to content

refactor(api): system/jobs/modules/instances SQL moves behind interfaces - #535

Open
chodeus wants to merge 3 commits into
mainfrom
refactor/sql-to-interfaces-system
Open

refactor(api): system/jobs/modules/instances SQL moves behind interfaces#535
chodeus wants to merge 3 commits into
mainfrom
refactor/sql-to-interfaces-system

Conversation

@chodeus

@chodeus chodeus commented Aug 14, 2026

Copy link
Copy Markdown
Owner

Stacked on #531 (the media_cache domain split) — this diff shows both until #531 merges, then collapses to G1c alone.

What

backend/api holds zero raw SQL after this. 23 sites moved: system.py's maintenance and health queries get DbMaintenance and SystemHealth owners; job queries land on DBWorker (table-allowlist guarded); counts move to their cache owners; the stray raw sqlite3.connect VACUUM gets an honest owner.

  • Bug found and fixed: /api/system/db/artwork-matches/reset counted user-locked rows (ignored=1 / user_confirmed=1) as deleted while preserving them — the pre-count's WHERE didn't match the DELETE's. The response now reports the DELETE's own rowcount, covered at both layers.
  • datetime.utcnow() leaves the repo (4 sites → datetime.now(timezone.utc)): the two cutoff comparisons are proven byte-equivalent by brute force against both stored timestamp shapes (0 mismatches across 1080×16 cases); the value sites render identically (verified through the frontend's date path).
  • Every ORDER BY under LIMIT carries an id tiebreaker; every moved query passed the campaign pre-screen list.

Verification

32 new tests, every one mutation-proven to redden (one test was dropped for being unable to discriminate — SQLite's index reverse-scan already returns the tied order — documented in-code); full suite green ×2 (1590 tests); ruff clean.

Flagged for later (ledgered)

media_cache.created_at cutoff separator mismatch (real pre-existing bug — same-day rows silently excluded from media_added); scheduler.py's two raw snapshot writes → SystemHealth owners; the db.worker lambda property; the cancel-endpoint TOCTOU wording.

Edit-history, metadata-completeness and library-stats move to sibling
modules as mixins; MediaCache keeps its exact class surface (dir() is
byte-identical against main) and every import path re-exports unchanged.
media_cache.py: 1791 -> 1254 lines. Move-only — each block byte-identical
to its origin.
backend/api holds zero raw SQL now. 23 sites: system.py's maintenance and
health queries get DbMaintenance and SystemHealth owners; job queries land
on DBWorker (table-allowlist guarded); counts move to their cache owners.
The artwork-matches reset now reports the DELETE's own rowcount — the old
pre-count claimed user-locked rows as deleted while preserving them.
datetime.utcnow() leaves the repo (4 sites, now(timezone.utc); cutoff
comparisons proven byte-equivalent against both stored shapes). VACUUM and
ping get honest owners; every ORDER BY under LIMIT carries an id tiebreaker.
@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 38 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bce296fe-4884-4cac-bcf2-ec284214b119

📥 Commits

Reviewing files that changed from the base of the PR and between 0f068df and 6821970.

📒 Files selected for processing (17)
  • backend/api/instances.py
  • backend/api/jobs.py
  • backend/api/media_api.py
  • backend/api/modules.py
  • backend/api/system.py
  • backend/util/database/__init__.py
  • backend/util/database/collection_cache.py
  • backend/util/database/maintenance.py
  • backend/util/database/media_asset_matches.py
  • backend/util/database/media_cache.py
  • backend/util/database/media_stats.py
  • backend/util/database/poster_cache.py
  • backend/util/database/system_health.py
  • backend/util/database/worker.py
  • backend/util/notification.py
  • tests/test_media_api_queries.py
  • tests/test_system_queries.py

Comment @coderabbitai help to get the list of available commands.

…faces-system

# Conflicts:
#	backend/util/database/media_stats.py
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