|
| 1 | +# Replace PostgreSQL with CockroachDB in Integration Tests |
| 2 | + |
| 3 | +**Date:** 2026-05-18 |
| 4 | +**Status:** Approved |
| 5 | +**Goal:** Align the integration test database with production (CockroachDB). |
| 6 | + |
| 7 | +## Context |
| 8 | + |
| 9 | +Production httpSMS uses CockroachDB. The integration test suite (`tests/docker-compose.yml`) currently uses `postgres:alpine`. This mismatch means tests don't exercise CockroachDB-specific behavior (e.g., serializable isolation by default, distributed SQL quirks). The API's GORM `postgres` driver is already wire-compatible with CockroachDB, so no application code changes are needed. |
| 10 | + |
| 11 | +## Design |
| 12 | + |
| 13 | +### Docker Compose Changes (`tests/docker-compose.yml`) |
| 14 | + |
| 15 | +1. **Replace `postgres` service with `cockroachdb`:** |
| 16 | + |
| 17 | + - Image: `cockroachdb/cockroach:latest` |
| 18 | + - Command: `start-single-node --insecure --store=type=mem,size=256MiB` |
| 19 | + - Ports: `26257:26257` (SQL), `8081:8080` (admin UI — remapped to avoid wiremock conflict) |
| 20 | + - Healthcheck: `curl -f http://localhost:8080/health?ready=1` |
| 21 | + |
| 22 | +2. **Add `cockroachdb-init` service** (runs once after cockroachdb is healthy): |
| 23 | + |
| 24 | + - Uses same CockroachDB image |
| 25 | + - Creates the `httpsms` database: `cockroach sql --insecure --host=cockroachdb --execute="CREATE DATABASE IF NOT EXISTS httpsms;"` |
| 26 | + |
| 27 | +3. **Update `seed` service:** |
| 28 | + |
| 29 | + - Image: `cockroachdb/cockroach:latest` (instead of `postgres:alpine`) |
| 30 | + - Entrypoint: `cockroach sql --insecure --host=cockroachdb --database=httpsms --file=/seed.sql` |
| 31 | + - Depends on `api` (healthy) so GORM migrations run first |
| 32 | + |
| 33 | +4. **Update `api` service:** |
| 34 | + - `depends_on`: replace `postgres` with `cockroachdb-init` |
| 35 | + |
| 36 | +### Environment Changes (`tests/.env.test`) |
| 37 | + |
| 38 | +``` |
| 39 | +DATABASE_URL=postgresql://root@cockroachdb:26257/httpsms?sslmode=disable |
| 40 | +DATABASE_URL_DEDICATED=postgresql://root@cockroachdb:26257/httpsms?sslmode=disable |
| 41 | +``` |
| 42 | + |
| 43 | +### Seed SQL (`tests/seed.sql`) |
| 44 | + |
| 45 | +No changes required. `INSERT ... ON CONFLICT ... DO NOTHING` and `NOW()` are supported by CockroachDB. |
| 46 | + |
| 47 | +### What Does NOT Change |
| 48 | + |
| 49 | +- API application code (GORM postgres driver works with CockroachDB) |
| 50 | +- Integration test Go code |
| 51 | +- Redis, MongoDB, Wiremock services |
| 52 | +- API Dockerfile |
| 53 | + |
| 54 | +## Key Decisions |
| 55 | + |
| 56 | +| Decision | Rationale | |
| 57 | +| ----------------------------------- | --------------------------------------------------------------------------------------------- | |
| 58 | +| Single-node insecure mode | Simplest for tests; no TLS cert management | |
| 59 | +| In-memory store (`type=mem`) | Faster startup, no persistent volume needed for tests | |
| 60 | +| Separate `cockroachdb-init` service | CockroachDB doesn't support `POSTGRES_DB`-style env vars; database must be created explicitly | |
| 61 | +| Remap admin UI to 8081 | Avoids port conflict with wiremock on 8080 | |
| 62 | +| Use `cockroach sql` for seeding | Native client; avoids pulling a separate postgres image | |
| 63 | + |
| 64 | +## Risks |
| 65 | + |
| 66 | +- **GORM migration compatibility:** CockroachDB may handle some DDL differently (e.g., `ALTER TABLE` constraints). The existing `DATABASE_MIGRATION_CONSTRAINT_FIX` env var handles known issues — we'll enable it in `.env.test`. |
| 67 | +- **Startup time:** CockroachDB takes slightly longer to start than PostgreSQL (~5-10s). The healthcheck handles this. |
0 commit comments