Skip to content

fix(scanner): repair invalid CTE that made UpdateEpisodeLink fail on every call - #321

Merged
Quick104 merged 1 commit into
mainfrom
fix/episode-link-invalid-cte
Jul 6, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/episode-link-invalid-cte

Conversation

@Quick104

@Quick104 Quick104 commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #320.

Problem

UpdateEpisodeLink ends its update+insert statement with SELECT COUNT(*) FROM inserted, but the inserted CTE has no RETURNING clause. PostgreSQL rejects referencing a data-modifying CTE without RETURNING (SQLSTATE 0A000), so every call has failed at runtime since #283. The sole caller (internal/metadata/service.go episode file linking) logs a warning and continues, so the metadata-side single-file relink silently did nothing: media_files.episode_id stayed stale, episode_libraries rows were never inserted, and latest_episode_added_at never bumped through this path. BulkLinkEpisodesBySeries uses the correct CTE pattern (its inserted CTE has RETURNING), which masked the breakage.

Fix

Make the INSERT … ON CONFLICT DO NOTHING the top-level statement of the WITH; the trailing count was unused. One statement, identical semantics, now valid SQL.

Verification

Against a migrated PostgreSQL scratch database: TestEpisodeLinkMaintainsLatestEpisodeAdded (the existing skip-gated DB test at episode_added_denorm_test.go) fails on main with the exact production error and passes with this change — verified in both directions. go build ./..., the scanner unit suite, and golangci-lint --new-from-rev=origin/main are clean. No CI runs DB-backed Go tests, which is why this survived since #283; worth considering a self-hosted DB test job as follow-up.

Found while running the database suites for #319.

AI-use disclosure

Diagnosed and fixed with Claude Code (Claude Fable 5); human-directed.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue that could cause episode link updates to fail due to an invalid database query.
    • Improved reliability when saving library links so updates complete successfully without triggering errors.

…every call

The statement ended with SELECT COUNT(*) FROM inserted, but the inserted
CTE has no RETURNING clause — PostgreSQL rejects referencing such a CTE
(SQLSTATE 0A000), so every UpdateEpisodeLink call has errored since #283.
The sole caller logs a warning and continues, so the metadata-side
single-file episode relink silently did nothing: episode_id stayed stale,
episode_libraries rows were not inserted, latest_episode_added_at was not
bumped. Bulk linking uses the correct pattern and masked the breakage.

The INSERT is now the top-level statement; the count was unused.
Verified against a migrated PostgreSQL scratch database:
TestEpisodeLinkMaintainsLatestEpisodeAdded fails on main with the exact
error and passes with this change.

Fixes #320

Co-Authored-By: Claude Fable 5 <[email protected]>
@Quick104
Quick104 merged commit 1c8084c into main Jul 6, 2026
1 check was pending
@Quick104
Quick104 deleted the fix/episode-link-invalid-cte branch July 6, 2026 16:23
@coderabbitai

coderabbitai Bot commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6b0cb5a9-0b9f-461b-bf90-866622794c28

📥 Commits

Reviewing files that changed from the base of the PR and between a5b23da and 8e2ac92.

📒 Files selected for processing (1)
  • internal/scanner/file_repo.go

📝 Walkthrough

Walkthrough

Fixed a SQL statement bug in UpdateEpisodeLink where a data-modifying CTE was referenced via SELECT COUNT(*) FROM inserted without a RETURNING clause, causing every invocation to fail. The statement now inserts directly from the updated CTE with ON CONFLICT DO NOTHING.

Changes

UpdateEpisodeLink SQL Fix

Layer / File(s) Summary
Fix invalid CTE query
internal/scanner/file_repo.go
Restructures the transaction SQL to insert into episode_libraries directly from the updated CTE with ON CONFLICT (episode_id, media_folder_id) DO NOTHING, removing the invalid inserted CTE and trailing SELECT COUNT(*), and adds clarifying comments.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Related issues: #320 — UpdateEpisodeLink fails on every call due to invalid CTE reference without RETURNING clause.

Suggested labels: bug, database

Suggested reviewers: none identified from provided context

Poem

A CTE once tried to count and fail,
No RETURNING clause to tell its tale,
Now it inserts, direct and true,
Straight from updated, nothing left askew,
The rabbit hops on, bug fixed, all's well. 🐇

✨ 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/episode-link-invalid-cte

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

Quick104 added a commit that referenced this pull request Jul 10, 2026
…every call (#321)

The statement ended with SELECT COUNT(*) FROM inserted, but the inserted
CTE has no RETURNING clause — PostgreSQL rejects referencing such a CTE
(SQLSTATE 0A000), so every UpdateEpisodeLink call has errored since #283.
The sole caller logs a warning and continues, so the metadata-side
single-file episode relink silently did nothing: episode_id stayed stale,
episode_libraries rows were not inserted, latest_episode_added_at was not
bumped. Bulk linking uses the correct pattern and masked the breakage.

The INSERT is now the top-level statement; the count was unused.
Verified against a migrated PostgreSQL scratch database:
TestEpisodeLinkMaintainsLatestEpisodeAdded fails on main with the exact
error and passes with this change.

Fixes #320

Co-authored-by: Claude Fable 5 <[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.

UpdateEpisodeLink fails on every call: invalid CTE reference without RETURNING

1 participant