Skip to content

feat(persistence): Enforce one DrillItem per Challenge; handle concurrency and race conditions #40

@ArchILLtect

Description

@ArchILLtect

Problem / context

Under concurrent load, duplicate DrillItem rows could be created for the same Challenge, and concurrent updates might overwrite streak/nextDueAt values (lost updates).

Proposed solution

Uniqueness & race handling

  • Enforce one DrillItem per Challenge via a unique constraint (drill_items.challenge_id).
  • Update getOrCreateDrillItem to catch DataIntegrityViolationException and re-fetch the existing record.

Optimistic locking

  • Utilize the @Version field added in MVP.

Tests

  • Add DAO/service concurrency tests for create/update race scenarios.

Index

  • Add unique index on drill_items.challenge_id in Flyway/Liquibase migration.

In scope / Out of scope

In Scope

  • Service/DAO concurrency handling and tests.
  • Schema constraint via migration (Week 8 milestone).

Out of Scope

  • Refactoring unrelated domain entities.
  • Non-persistence optimizations.

Acceptance criteria

  • Unique constraint prevents duplicate DrillItem per Challenge.
  • Concurrent updates use optimistic locking (@Version).
  • Concurrency tests confirm correctness and stability.
  • All existing tests remain green.

Area

area:persistence

Dependencies / related issues

No response

Checklist

  • I’ve checked existing issues for duplicates.
  • I can help implement this and open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:persistenceEntities, DAOs, JPA/Hibernate mappings, and database concerns.enhancementNew feature or requestpost-mvpUse for all issues/PRs that do not belong to the MVP release. Will be implement post-MVP.priority:P3-lowNice-to-have or non-blocking; schedule as time permits.status:triageNewly filed or uncategorized. Needs initial review, labeling, and priority assignment.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions