Skip to content

fix(metadata): tolerate missing content item when recording stale IDs - #37

Merged
Quick104 merged 1 commit into
Silo-Server:mainfrom
fluxis:fix/stale-media-id-missing-content
Jun 6, 2026
Merged

Quick104 merged 1 commit into
Silo-Server:mainfrom
fluxis:fix/stale-media-id-missing-content

Conversation

@fluxis

@fluxis fluxis commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

StaleMediaIDRepository.Upsert inserts (content_id, provider, ...) into stale_media_ids, which has a foreign key to media_items.content_id. A metadata refresh can start for an item that is then deleted or merged away (e.g. by provider-ID canonicalization) before the provider 404 lands and triggers the upsert. The parent row is gone by then, so the insert fails with a 23503 foreign-key violation (stale_media_ids_content_id_fkey) and the refresh surfaces a spurious error — observed in production postgres logs.

Approach

  • When the referenced item no longer exists there is nothing left to track, so treat that specific FK violation as a logged no-op (at Info, with provider_id, so a deletion wave stays visible). All other errors still propagate.
  • The swallow decision is extracted to resolveStaleUpsertError so it is unit-testable without a database.
  • The errors.As(&pgconn.PgError) + Code/ConstraintName boilerplate is consolidated into a shared isPgConstraintViolation helper that the existing isProviderIDUniqueConflict now also uses.

Risks / follow-up

Low. The swallow is narrowly scoped to 23503 on stale_media_ids_content_id_fkey; unique violations and other FKs still surface. The constraint name is the Postgres-auto-generated name from migrations/036 (documented at the call site). A companion PR (record-stale-IDs-against-canonical-item) reduces how often this race fires by recording against the surviving canonical item.

Verification

go test ./internal/metadata/ and go vet ./internal/metadata/ pass. New tests cover the classifier and the swallow-vs-propagate decision (nil / FK / other-pg-error / generic).

AI-use disclosure

Implemented with Claude Code (Opus 4.8), TDD, after a multi-agent review of the change.

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Enhanced error handling to gracefully manage edge cases involving deleted or missing media items
    • Improved database constraint violation detection for more reliable data integrity checks
  • Tests

    • Added unit tests for error classification and handling logic

StaleMediaIDRepository.Upsert inserts (content_id, provider, ...) into
stale_media_ids, which has a foreign key to media_items.content_id. A
metadata refresh can start for an item that is then deleted or merged
away (e.g. by provider-ID canonicalization) before the provider 404
lands and triggers the upsert. The parent row is gone by then, so the
insert fails with a 23503 foreign-key violation
(stale_media_ids_content_id_fkey) and the refresh surfaces a spurious
error — observed in production postgres logs.

When the referenced item no longer exists there is nothing left to track,
so treat that specific FK violation as a logged no-op (at Info, with
provider_id, so a deletion wave stays visible). Other errors still
propagate. The swallow decision is extracted to resolveStaleUpsertError so
it is unit-testable without a database, and the pgconn error-classification
boilerplate is consolidated into a shared isPgConstraintViolation helper
that isProviderIDUniqueConflict now also uses.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 1, 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: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 19386976-3187-43af-bdf7-7efa87f1cf7d

📥 Commits

Reviewing files that changed from the base of the PR and between 4c5691f and c30abb2.

📒 Files selected for processing (4)
  • internal/metadata/pg_errors.go
  • internal/metadata/service.go
  • internal/metadata/stale_media_id_repo.go
  • internal/metadata/stale_media_id_repo_test.go

📝 Walkthrough

Walkthrough

This PR centralizes PostgreSQL constraint violation detection into a reusable helper function (isPgConstraintViolation), applies it to gracefully handle foreign-key violations in stale media upsert operations, and refactors service-layer code to eliminate direct pgconn dependencies.

Changes

PostgreSQL Error Handling Refactoring

Layer / File(s) Summary
PostgreSQL constraint violation helper
internal/metadata/pg_errors.go, internal/metadata/stale_media_id_repo_test.go
New isPgConstraintViolation helper unwraps error chains to detect PostgreSQL constraint violations matching a specific SQLSTATE code and constraint name. Tests verify behavior with nil input, wrapped errors, mismatched codes, and exact matches.
Stale media upsert foreign-key error handling
internal/metadata/stale_media_id_repo.go, internal/metadata/stale_media_id_repo_test.go
Stale media Upsert detects and gracefully resolves PostgreSQL foreign-key violations for missing content_id references, logging them as benign no-ops while propagating other errors. Tests cover the FK violation detector and error resolver.
Service layer refactoring
internal/metadata/service.go
Service layer removes direct pgconn import and simplifies isProviderIDUniqueConflict to use the new centralized isPgConstraintViolation helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A PostgreSQL error so grand,
Now handled with a helper's hand,
From constraint clashes caught with care,
To stale media fading in the air—
One function rules them all so fair! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(metadata): tolerate missing content item when recording stale IDs' accurately and concisely summarizes the main change: handling a specific foreign key violation by treating it as a no-op rather than propagating the error.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@Quick104
Quick104 merged commit 94216ca into Silo-Server:main Jun 6, 2026
1 check passed
Quick104 pushed a commit to thezak48/silo-server that referenced this pull request Jun 7, 2026
…Silo-Server#37)

StaleMediaIDRepository.Upsert inserts (content_id, provider, ...) into
stale_media_ids, which has a foreign key to media_items.content_id. A
metadata refresh can start for an item that is then deleted or merged
away (e.g. by provider-ID canonicalization) before the provider 404
lands and triggers the upsert. The parent row is gone by then, so the
insert fails with a 23503 foreign-key violation
(stale_media_ids_content_id_fkey) and the refresh surfaces a spurious
error — observed in production postgres logs.

When the referenced item no longer exists there is nothing left to track,
so treat that specific FK violation as a logged no-op (at Info, with
provider_id, so a deletion wave stays visible). Other errors still
propagate. The swallow decision is extracted to resolveStaleUpsertError so
it is unit-testable without a database, and the pgconn error-classification
boilerplate is consolidated into a shared isPgConstraintViolation helper
that isProviderIDUniqueConflict now also uses.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
@fluxis
fluxis deleted the fix/stale-media-id-missing-content branch June 9, 2026 17:32
Quick104 pushed a commit that referenced this pull request Jun 11, 2026
…#37)

StaleMediaIDRepository.Upsert inserts (content_id, provider, ...) into
stale_media_ids, which has a foreign key to media_items.content_id. A
metadata refresh can start for an item that is then deleted or merged
away (e.g. by provider-ID canonicalization) before the provider 404
lands and triggers the upsert. The parent row is gone by then, so the
insert fails with a 23503 foreign-key violation
(stale_media_ids_content_id_fkey) and the refresh surfaces a spurious
error — observed in production postgres logs.

When the referenced item no longer exists there is nothing left to track,
so treat that specific FK violation as a logged no-op (at Info, with
provider_id, so a deletion wave stays visible). Other errors still
propagate. The swallow decision is extracted to resolveStaleUpsertError so
it is unit-testable without a database, and the pgconn error-classification
boilerplate is consolidated into a shared isPgConstraintViolation helper
that isProviderIDUniqueConflict now also uses.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Quick104 pushed a commit that referenced this pull request Jun 17, 2026
…#37)

StaleMediaIDRepository.Upsert inserts (content_id, provider, ...) into
stale_media_ids, which has a foreign key to media_items.content_id. A
metadata refresh can start for an item that is then deleted or merged
away (e.g. by provider-ID canonicalization) before the provider 404
lands and triggers the upsert. The parent row is gone by then, so the
insert fails with a 23503 foreign-key violation
(stale_media_ids_content_id_fkey) and the refresh surfaces a spurious
error — observed in production postgres logs.

When the referenced item no longer exists there is nothing left to track,
so treat that specific FK violation as a logged no-op (at Info, with
provider_id, so a deletion wave stays visible). Other errors still
propagate. The swallow decision is extracted to resolveStaleUpsertError so
it is unit-testable without a database, and the pgconn error-classification
boilerplate is consolidated into a shared isPgConstraintViolation helper
that isProviderIDUniqueConflict now also uses.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Quick104 pushed a commit that referenced this pull request Jun 17, 2026
…#37)

StaleMediaIDRepository.Upsert inserts (content_id, provider, ...) into
stale_media_ids, which has a foreign key to media_items.content_id. A
metadata refresh can start for an item that is then deleted or merged
away (e.g. by provider-ID canonicalization) before the provider 404
lands and triggers the upsert. The parent row is gone by then, so the
insert fails with a 23503 foreign-key violation
(stale_media_ids_content_id_fkey) and the refresh surfaces a spurious
error — observed in production postgres logs.

When the referenced item no longer exists there is nothing left to track,
so treat that specific FK violation as a logged no-op (at Info, with
provider_id, so a deletion wave stays visible). Other errors still
propagate. The swallow decision is extracted to resolveStaleUpsertError so
it is unit-testable without a database, and the pgconn error-classification
boilerplate is consolidated into a shared isPgConstraintViolation helper
that isProviderIDUniqueConflict now also uses.

Co-authored-by: Claude Opus 4.8 (1M context) <[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.

2 participants