Skip to content

fix(catalog): make media_items scalar columns NOT NULL to stop NULL-scan crashes - #228

Merged
Quick104 merged 1 commit into
mainfrom
fix/media-items-nonnull-scalar-columns
Jun 26, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/media-items-nonnull-scalar-columns

Conversation

@Quick104

@Quick104 Quick104 commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

media_items has many columns the Go model (models.MediaItem) declares as non-pointer string/int fields, but migration 001 left them nullable. Five scanners across four packages read these straight into the non-pointer fields, so any NULL row panics with cannot scan NULL into *string (or *int):

  • internal/catalog/item_repo.go (scanItem/scanItems/scanItemsWithMangaCounts)
  • internal/catalog/browse.go (scanBrowseItems)
  • internal/jellycompat/batch_loaders.go (scanCompatMediaItems)
  • internal/sections/fetcher.go
  • internal/api/handlers/catalog.go

item_repo papers over a subset (poster/backdrop/logo/metadata paths) with COALESCE in its SELECT, but the other four scanners read the same columns raw and crash; sort_title/original_title/etc. aren't coalesced anywhere. The thumbhash columns are the worst case: SetLocalPoster/UpdateArtworkIfSourceMatches deliberately wrote NULL via NULLIF($,''), so browse/jellycompat could crash on real production rows, not just test data.

Reproduces today on main: internal/catalog TestFilterCollectionItemsByDisplayQuery fails with cannot scan NULL into *string because its seed inserts a minimal row (omitting the nullable columns).

Why this approach

Make the schema enforce the invariant the code already assumes: every media_items column the Go model treats as non-nullable is NOT NULL. This fixes all five scanners (and any future one) in one place instead of scattering COALESCE across four packages — the "duplicate logic across multiple files" smell.

Safe because:

  • No writer stores a meaningful NULL — every insert/upsert passes the Go field (''/0 at worst). The only deliberate NULL writers were the two thumbhash NULLIF sites, updated here.
  • ''/NULL are already treated identically everywhere — all sort/filter SQL uses COALESCE(NULLIF(BTRIM(sort_title), ''), title), and every IS NULL usage is (x IS NULL OR x = '') or guarded by <> ''. No IS NULL logic exists on year/runtime.
  • Mirrors what later migrations already did for original_language, show_status, default_metadata_language, and the *_source_path columns (all NOT NULL DEFAULT '').

Changes

  • Migration 20260626190132_media_items_nonnull_scalar_columns.sql: backfill existing NULLs, then NOT NULL DEFAULT '' on 15 text columns (sort_title, original_title, content_rating, overview, tagline, imdb_id/tmdb_id/tvdb_id, poster_path, poster_thumbhash, backdrop_path, backdrop_thumbhash, logo_path, metadata_s3_path, metadata_etag) and NOT NULL DEFAULT 0 on year/runtime. Single ALTER TABLE (one ACCESS EXCLUSIVE lock); reversible Down.
  • internal/catalog/item_repo.go: SetLocalPoster and UpdateArtworkIfSourceMatches store '' for empty thumbhashes (was NULLIF($,'')), matching the upsert path.

Genuinely-nullable pointer fields (ratings, dates, season_count, air_*) and genres (NULL arrays scan fine) are left untouched.

Verification

Against a fresh, fully-migrated Postgres:

  • TestFilterCollectionItemsByDisplayQuery passes; full catalog suite passes (DB-backed)
  • scanner + metadata writer tests pass (cover the NULLIF→direct change); sections passes
  • go build ./..., gofmt, go vet clean
  • Down/Up rollback cycle clean both directions
  • Minimal raw insert (the production NULL scenario) now yields ''/0 for every column

Risks / follow-up

  • Low risk; SET NOT NULL does a one-time validating scan (small on current data).
  • Out of scope (harmless): the now-NOT-NULL path columns still sit in nullableStringItemColumns, so their SELECT COALESCE is now redundant — consistent with how *_source_path was already handled there.
  • Not linked to a capability epic — standalone correctness/reliability fix. Please add a Part of #NNN if there's a relevant scope item.

AI-use disclosure

Diagnosed, implemented, and verified with Claude Code (Claude Opus 4.8).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of artwork thumbnail data so empty values are stored consistently instead of being dropped.
    • Added a database update to normalize existing media item fields and enforce safer defaults for required values.

…can crashes

media_items had many columns the Go model (models.MediaItem) declares as
non-pointer string/int fields, but migration 001 left them nullable. Five
scanners (catalog item_repo, catalog browse, jellycompat, sections, and
the catalog API handler) read these straight into the non-pointer fields,
so a NULL row panics with "cannot scan NULL into *string" (or *int).
item_repo papers over a subset (poster/backdrop/logo/metadata paths) with
COALESCE in its SELECT, but the other four scanners read the same columns
raw and crash; sort_title/original_title/etc. are not coalesced anywhere.

No writer stores a meaningful NULL (every insert/upsert passes the Go
field, '' or 0 at worst) and all sort/filter SQL already collapses NULL
and '' (e.g. COALESCE(NULLIF(BTRIM(sort_title), ''), title); "poster_path
IS NULL OR poster_path = ''"). Enforce the invariant the code already
assumes at the schema level rather than scattering COALESCE across every
current and future scanner. Mirrors what later migrations already did for
original_language, show_status, default_metadata_language, and the
*_source_path columns (all NOT NULL DEFAULT '').

- Migration: backfill existing NULLs, then NOT NULL DEFAULT '' on 15 text
  columns (sort_title, original_title, content_rating, overview, tagline,
  imdb_id/tmdb_id/tvdb_id, poster_path, poster_thumbhash, backdrop_path,
  backdrop_thumbhash, logo_path, metadata_s3_path, metadata_etag) and
  NOT NULL DEFAULT 0 on year/runtime.
- item_repo: SetLocalPoster and UpdateArtworkIfSourceMatches now store ''
  for empty thumbhashes (was NULLIF($,'')) — the only deliberate NULL
  writers — matching the upsert path.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9ced00df-e3c8-4400-ae91-d7248de272e0

📥 Commits

Reviewing files that changed from the base of the PR and between 659e62a and 73be49f.

📒 Files selected for processing (2)
  • internal/catalog/item_repo.go
  • migrations/sql/20260626190132_media_items_nonnull_scalar_columns.sql

📝 Walkthrough

Walkthrough

This PR adds a migration that backfills and constrains media_items scalar columns to non-null defaults, and updates catalog thumbhash writes to store empty strings directly instead of converting them to NULL.

Changes

Media items nullability alignment

Layer / File(s) Summary
Backfill and constrain media_items scalars
migrations/sql/20260626190132_media_items_nonnull_scalar_columns.sql
Backfills NULL scalar values to empty strings or 0, then applies DEFAULT values and NOT NULL constraints in Up and removes them in Down.
Write empty thumbhash values directly
internal/catalog/item_repo.go
SetLocalPoster and UpdateArtworkIfSourceMatches write poster and backdrop thumbhash parameters directly instead of wrapping empty strings in NULLIF.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: enforcing non-null media_items scalar columns to prevent NULL-scan crashes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/media-items-nonnull-scalar-columns

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

@Quick104
Quick104 merged commit 0ff1872 into main Jun 26, 2026
1 check passed
@Quick104
Quick104 deleted the fix/media-items-nonnull-scalar-columns branch June 26, 2026 20:06
Quick104 added a commit to RXWatcher/silo-server that referenced this pull request Jul 10, 2026
…can crashes (Silo-Server#228)

media_items had many columns the Go model (models.MediaItem) declares as
non-pointer string/int fields, but migration 001 left them nullable. Five
scanners (catalog item_repo, catalog browse, jellycompat, sections, and
the catalog API handler) read these straight into the non-pointer fields,
so a NULL row panics with "cannot scan NULL into *string" (or *int).
item_repo papers over a subset (poster/backdrop/logo/metadata paths) with
COALESCE in its SELECT, but the other four scanners read the same columns
raw and crash; sort_title/original_title/etc. are not coalesced anywhere.

No writer stores a meaningful NULL (every insert/upsert passes the Go
field, '' or 0 at worst) and all sort/filter SQL already collapses NULL
and '' (e.g. COALESCE(NULLIF(BTRIM(sort_title), ''), title); "poster_path
IS NULL OR poster_path = ''"). Enforce the invariant the code already
assumes at the schema level rather than scattering COALESCE across every
current and future scanner. Mirrors what later migrations already did for
original_language, show_status, default_metadata_language, and the
*_source_path columns (all NOT NULL DEFAULT '').

- Migration: backfill existing NULLs, then NOT NULL DEFAULT '' on 15 text
  columns (sort_title, original_title, content_rating, overview, tagline,
  imdb_id/tmdb_id/tvdb_id, poster_path, poster_thumbhash, backdrop_path,
  backdrop_thumbhash, logo_path, metadata_s3_path, metadata_etag) and
  NOT NULL DEFAULT 0 on year/runtime.
- item_repo: SetLocalPoster and UpdateArtworkIfSourceMatches now store ''
  for empty thumbhashes (was NULLIF($,'')) — the only deliberate NULL
  writers — matching the upsert path.

Co-authored-by: Claude Opus 4.8 <[email protected]>
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