Skip to content

feat: guarantee reservation consistency under concurrent transactions#11

Merged
heitorrsdev merged 2 commits intomainfrom
test/reservation-concurrency-and-testing-docs
Feb 27, 2026
Merged

feat: guarantee reservation consistency under concurrent transactions#11
heitorrsdev merged 2 commits intomainfrom
test/reservation-concurrency-and-testing-docs

Conversation

@heitorrsdev
Copy link
Owner

@heitorrsdev heitorrsdev commented Feb 26, 2026

Description

This PR strengthens concurrency guarantees in the reservation system by introducing transactional repository support and validating database-level invariant enforcement under real concurrent execution.

It ensures that overlapping reservations are prevented at the PostgreSQL level via exclusion constraints and verifies this behavior through deterministic concurrency integration tests.

Additionally, it standardizes repository typing to support transactions cleanly and formalizes the project’s testing model in documentation.


What was done

  • Introduced DrizzleClient abstraction to allow repositories to operate with both root database instances and transactions
  • Refactored repositories to support transactional execution without unsafe type casts
  • Made connection pool size explicit and configurable
  • Added concurrency barrier utility for synchronized parallel execution in tests
  • Implemented integration test to validate prevention of double booking under real concurrent transactions
  • Verified correct mapping of PostgreSQL exclusion constraint violations to ReservationConflictError
  • Added comprehensive testing.md documentation describing the testing strategy and invariant enforcement model
  • Removed .env file dependency from Makefile targets
  • Simplified CI workflow to rely exclusively on GitHub Secrets for DATABASE_URL
  • Eliminated redundant migration step from CI pipeline
  • Removed dotenv-cli usage in favor of native environment variable handling
  • Removed unused .env files
  • Configured Jest to run with maxWorkers: 1 to prevent concurrency issues
  • Closed database pool properly after tests to fix CI leaks

Why

  • To guarantee that reservation conflicts are prevented under real concurrent execution
  • To move concurrency enforcement fully to the database layer instead of relying on application-level checks
  • To improve repository flexibility and future-proof transactional workflows
  • To eliminate unsafe type assertions (as any) and ensure strict typing with Drizzle
  • To document the testing and concurrency strategy as part of the architectural foundation
  • To unify local and CI environment handling under a single environment-variable-based model
  • To remove filesystem coupling and simplify pipeline determinism
  • To stabilize CI and prevent resource leaks during test execution

Notes

  • Concurrency protection relies on PostgreSQL EXCLUDE USING gist constraints
  • Tests use synchronized transactions to simulate real race conditions
  • This PR focuses on infrastructure correctness and invariant enforcement, not new business features
  • README updates will be introduced in a separate PR to keep scope isolated

@heitorrsdev heitorrsdev force-pushed the test/reservation-concurrency-and-testing-docs branch from 341a20d to 3d657cc Compare February 26, 2026 21:13
…eline

chore: add default test time constant

refactor: isolate user and barber factories responsabilites

refactor: rename isUniqueViolation to isExclusionViolation

chore: make pool max connections explicit

refactor: expose Transaction and DrizzleClient types

refactor: explicitly type createDatabase return value

refactor: allow repositories to accept transactions via DrizzleClient

test: create reservation factory for database seeding

test: add barrier helper for concurrency tests

test: prevent double booking under concurrent transactions

fix: ensure CI uses ci-test Makefile target

chore: remove .env file dependency and simplify CI environment handling

chore: remove unused dotenv-cli dependency

chore: remove unused environment files

test: log concurrency results to debug CI

chore: remove unused global Jest setup

chore: define maxWorkers to 1 in jest config

fix: close database pool after test suite
@heitorrsdev heitorrsdev force-pushed the test/reservation-concurrency-and-testing-docs branch from 6731d7c to 001687b Compare February 27, 2026 15:11
@heitorrsdev heitorrsdev merged commit 30b71e6 into main Feb 27, 2026
1 check passed
@heitorrsdev heitorrsdev deleted the test/reservation-concurrency-and-testing-docs branch February 27, 2026 15:15
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.

1 participant