Skip to content

Migration 0039: harden non-comic cleanup, reorder ACL index, drop dead FTS dedupe#693

Merged
ajslater merged 1 commit intov1.11-performancefrom
claude/vibrant-greider-8b29f2
May 2, 2026
Merged

Migration 0039: harden non-comic cleanup, reorder ACL index, drop dead FTS dedupe#693
ajslater merged 1 commit intov1.11-performancefrom
claude/vibrant-greider-8b29f2

Conversation

@ajslater
Copy link
Copy Markdown
Owner

@ajslater ajslater commented May 2, 2026

Summary

Three review-driven cleanups to migration 0039_metron_age_rating_default_view_and_performance.py — no functional changes outside the migration, just safety and efficiency improvements to make it harder to fail or misbehave on real installs.

  • Hardened _remove_non_comic_comics with two independent defenses against an unmounted comics volume turning the cleanup into a mass-delete:

    1. Sentinel stat probe — a new _comics_fs_reachable() helper probes up to 10 known comic-suffix paths via Path.is_file(). If none stat as files, the cleanup logs a warning and skips entirely.
    2. page_count == 0 predicate — the deletable queryset is now restricted to phantom-row signature only (the original importer bug always left page_count = 0). A real comic with an unusual filename has at least one page and is never a candidate.
  • Reordered the composite ACL index codex_comic_lib_ari_idx to be built after _backfill_age_rating_metron_index so SQLite does one sorted index build instead of incrementally maintaining the index during the bulk UPDATE on codex_comic.

  • Removed the FTS dedupe RunSQL (and the _FTS_DEDUPE_SQL constant). It ran immediately before DROP TABLE codex_comicfts, so it was dead code — pure overhead and one more statement that could fail mid-migration.

  • Swapped print() for loguru.logger in the cleanup function — loguru is already imported at the top of the file and works fine at migration time.

Why

A review of the 0039 migration flagged three concerns: the FTS dedupe was unreachable code, the ACL index was being maintained row-by-row during a bulk UPDATE instead of built once at the end, and _remove_non_comic_comics relied entirely on Path.is_file() to protect legitimate comics — meaning a misconfigured volume mount could escalate into mass deletion. Page-count + sentinel together make it virtually impossible for the cleanup to delete a real comic regardless of FS state.

Test plan

  • uv run python bin/manage.py migrate applies cleanly through 0039 on a fresh DB; sentinel correctly logs "no comic-suffix paths" and bails out instead of deleting.
  • ruff check passes on the migration file.
  • pytest tests/ — 26 passed.
  • Apply on a v1.10-era DB with phantom rows present and confirm the cleanup deletes them and logs through loguru.
  • Apply with the comics volume deliberately unmounted and confirm the migration completes without deleting anything.

🤖 Generated with Claude Code

…d FTS dedupe

Three review-driven cleanups to the 0039 migration so it's safer and
faster on real-world installs:

- ``_remove_non_comic_comics`` now refuses to delete unless it can
  stat-probe at least one comic-suffix path successfully, and the
  deletable queryset is restricted to ``page_count == 0`` rows (the
  phantom-row signature from the original importer bug). Together
  these mean a misconfigured Docker volume can no longer turn the
  cleanup into a mass-delete: a real comic with an unusual name has
  ``page_count >= 1`` and is never a candidate, and an unmounted
  filesystem trips the sentinel and skips the step entirely.
- The composite ACL index ``codex_comic_lib_ari_idx`` is now built
  *after* ``_backfill_age_rating_metron_index`` so SQLite does one
  sorted index build instead of incrementally maintaining the index
  during the bulk UPDATE.
- The FTS dedupe ``RunSQL`` (and its ``_FTS_DEDUPE_SQL`` constant)
  has been removed — it ran immediately before
  ``DROP TABLE codex_comicfts``, so it was dead code on this path.

Also swap the ``print()`` calls in the cleanup function for the
``loguru.logger`` already imported at the top of the file.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ajslater ajslater merged commit b8a62c5 into v1.11-performance May 2, 2026
1 check failed
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