Repository navigation
fix(catalog): make media_items scalar columns NOT NULL to stop NULL-scan crashes - #228
Merged
Merged
Conversation
…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]>
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR adds a migration that backfills and constrains ChangesMedia items nullability alignment
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
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]>
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.
Problem
media_itemshas many columns the Go model (models.MediaItem) declares as non-pointerstring/intfields, but migration 001 left them nullable. Five scanners across four packages read these straight into the non-pointer fields, so any NULL row panics withcannot 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.gointernal/api/handlers/catalog.goitem_repopapers over a subset (poster/backdrop/logo/metadata paths) withCOALESCEin 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/UpdateArtworkIfSourceMatchesdeliberately wrote NULL viaNULLIF($,''), so browse/jellycompat could crash on real production rows, not just test data.Reproduces today on
main:internal/catalogTestFilterCollectionItemsByDisplayQueryfails withcannot scan NULL into *stringbecause 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_itemscolumn the Go model treats as non-nullable isNOT NULL. This fixes all five scanners (and any future one) in one place instead of scatteringCOALESCEacross four packages — the "duplicate logic across multiple files" smell.Safe because:
''/0at worst). The only deliberate NULL writers were the two thumbhashNULLIFsites, updated here.''/NULL are already treated identically everywhere — all sort/filter SQL usesCOALESCE(NULLIF(BTRIM(sort_title), ''), title), and everyIS NULLusage is(x IS NULL OR x = '')or guarded by<> ''. NoIS NULLlogic exists onyear/runtime.original_language,show_status,default_metadata_language, and the*_source_pathcolumns (allNOT NULL DEFAULT '').Changes
20260626190132_media_items_nonnull_scalar_columns.sql: backfill existing NULLs, thenNOT 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) andNOT NULL DEFAULT 0onyear/runtime. SingleALTER TABLE(oneACCESS EXCLUSIVElock); reversibleDown.internal/catalog/item_repo.go:SetLocalPosterandUpdateArtworkIfSourceMatchesstore''for empty thumbhashes (wasNULLIF($,'')), matching theupsertpath.Genuinely-nullable pointer fields (ratings, dates,
season_count,air_*) andgenres(NULL arrays scan fine) are left untouched.Verification
Against a fresh, fully-migrated Postgres:
TestFilterCollectionItemsByDisplayQuerypasses; fullcatalogsuite passes (DB-backed)scanner+metadatawriter tests pass (cover theNULLIF→direct change);sectionspassesgo build ./...,gofmt,go vetclean''/0for every columnRisks / follow-up
SET NOT NULLdoes a one-time validating scan (small on current data).nullableStringItemColumns, so their SELECTCOALESCEis now redundant — consistent with how*_source_pathwas already handled there.Part of #NNNif 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