Add unique constraint to prevent duplicate (author, chapter, page) submissions

Adds a PostgreSQL partial unique index on (author, chapter, page) where all
three fields are non-null, and returns HTTP 409 when a duplicate is detected.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Aaron Roberts
2026-06-09 18:19:54 +01:00
parent cb704a2f27
commit 1d15b5f0c1
2 changed files with 13 additions and 0 deletions

View File

@@ -45,6 +45,13 @@ def init_db():
cur.execute(""" cur.execute("""
CREATE INDEX IF NOT EXISTS ocr_jobs_submitted_at_idx ON ocr_jobs(submitted_at DESC) CREATE INDEX IF NOT EXISTS ocr_jobs_submitted_at_idx ON ocr_jobs(submitted_at DESC)
""") """)
# Unique constraint: prevent duplicate (author, chapter, page) submissions.
# Applies only when all three fields are non-null.
cur.execute("""
CREATE UNIQUE INDEX IF NOT EXISTS ocr_jobs_author_chapter_page_unique
ON ocr_jobs (author, chapter, page)
WHERE author IS NOT NULL AND chapter IS NOT NULL AND page IS NOT NULL
""")
conn.commit() conn.commit()
print("Database initialized.") print("Database initialized.")
except Exception as exc: except Exception as exc:

View File

@@ -675,6 +675,12 @@ async def commit_job(
os.remove(image_path) os.remove(image_path)
except Exception: except Exception:
pass pass
# Unique constraint violation (author + chapter + page already exists)
if getattr(exc, 'pgcode', None) == '23505':
raise HTTPException(
status_code=409,
detail="A job with this Author, Chapter, and Page already exists."
)
print(f"Job commit DB error: {exc}") print(f"Job commit DB error: {exc}")
raise HTTPException(status_code=500, detail="Failed to save job to database.") raise HTTPException(status_code=500, detail="Failed to save job to database.")