|
| 1 | +# CockroachDB Integration Tests Implementation Plan |
| 2 | + |
| 3 | +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. |
| 4 | +
|
| 5 | +**Goal:** Replace PostgreSQL with CockroachDB in the integration test Docker Compose so the test environment matches production. |
| 6 | + |
| 7 | +**Architecture:** Swap the `postgres:alpine` container for `cockroachdb/cockroach:latest` in single-node insecure mode with in-memory storage. Add a one-shot init container to create the database (CockroachDB has no `POSTGRES_DB` env var equivalent). Update connection strings and seed service. |
| 8 | + |
| 9 | +**Tech Stack:** Docker Compose, CockroachDB (single-node), GORM (postgres driver — wire-compatible) |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## File Map |
| 14 | + |
| 15 | +| File | Action | Responsibility | |
| 16 | +| -------------------------- | ------ | ------------------------------------------------------------------------- | |
| 17 | +| `tests/docker-compose.yml` | Modify | Replace postgres service, add cockroachdb + init, update seed and api | |
| 18 | +| `tests/.env.test` | Modify | Update DATABASE_URL connection strings, add migration fix flag | |
| 19 | +| `tests/README.md` | Modify | Update architecture diagram and references from PostgreSQL to CockroachDB | |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +### Task 1: Replace PostgreSQL with CockroachDB in Docker Compose |
| 24 | + |
| 25 | +**Files:** |
| 26 | + |
| 27 | +- Modify: `tests/docker-compose.yml` |
| 28 | + |
| 29 | +- [ ] **Step 1: Replace the `postgres` service with `cockroachdb`** |
| 30 | + |
| 31 | +Replace lines 1–15 of `tests/docker-compose.yml` (the `postgres` service) with: |
| 32 | + |
| 33 | +```yaml |
| 34 | +services: |
| 35 | + cockroachdb: |
| 36 | + image: cockroachdb/cockroach:latest |
| 37 | + command: start-single-node --insecure --store=type=mem,size=256MiB |
| 38 | + ports: |
| 39 | + - "26257:26257" |
| 40 | + - "8081:8080" |
| 41 | + healthcheck: |
| 42 | + test: ["CMD", "curl", "-f", "http://localhost:8080/health?ready=1"] |
| 43 | + interval: 5s |
| 44 | + timeout: 5s |
| 45 | + retries: 10 |
| 46 | + start_period: 10s |
| 47 | +``` |
| 48 | +
|
| 49 | +- [ ] **Step 2: Add `cockroachdb-init` service after `cockroachdb`** |
| 50 | + |
| 51 | +Add this service immediately after the `cockroachdb` service: |
| 52 | + |
| 53 | +```yaml |
| 54 | +cockroachdb-init: |
| 55 | + image: cockroachdb/cockroach:latest |
| 56 | + depends_on: |
| 57 | + cockroachdb: |
| 58 | + condition: service_healthy |
| 59 | + entrypoint: |
| 60 | + [ |
| 61 | + "cockroach", |
| 62 | + "sql", |
| 63 | + "--insecure", |
| 64 | + "--host=cockroachdb", |
| 65 | + "--execute=CREATE DATABASE IF NOT EXISTS httpsms;", |
| 66 | + ] |
| 67 | + restart: "no" |
| 68 | +``` |
| 69 | + |
| 70 | +- [ ] **Step 3: Update `api` service `depends_on`** |
| 71 | + |
| 72 | +Replace the `depends_on` block of the `api` service. Change: |
| 73 | + |
| 74 | +```yaml |
| 75 | +depends_on: |
| 76 | + postgres: |
| 77 | + condition: service_healthy |
| 78 | + redis: |
| 79 | + condition: service_healthy |
| 80 | + wiremock: |
| 81 | + condition: service_healthy |
| 82 | + mongodb: |
| 83 | + condition: service_healthy |
| 84 | +``` |
| 85 | + |
| 86 | +To: |
| 87 | + |
| 88 | +```yaml |
| 89 | +depends_on: |
| 90 | + cockroachdb-init: |
| 91 | + condition: service_completed_successfully |
| 92 | + redis: |
| 93 | + condition: service_healthy |
| 94 | + wiremock: |
| 95 | + condition: service_healthy |
| 96 | + mongodb: |
| 97 | + condition: service_healthy |
| 98 | +``` |
| 99 | + |
| 100 | +- [ ] **Step 4: Update `seed` service** |
| 101 | + |
| 102 | +Replace the entire `seed` service. Change: |
| 103 | + |
| 104 | +```yaml |
| 105 | +seed: |
| 106 | + image: postgres:alpine |
| 107 | + depends_on: |
| 108 | + api: |
| 109 | + condition: service_healthy |
| 110 | + environment: |
| 111 | + PGPASSWORD: dbpassword |
| 112 | + volumes: |
| 113 | + - ./seed.sql:/seed.sql:ro |
| 114 | + entrypoint: |
| 115 | + [ |
| 116 | + "psql", |
| 117 | + "-h", |
| 118 | + "postgres", |
| 119 | + "-U", |
| 120 | + "dbusername", |
| 121 | + "-d", |
| 122 | + "httpsms", |
| 123 | + "-f", |
| 124 | + "/seed.sql", |
| 125 | + ] |
| 126 | + restart: "no" |
| 127 | +``` |
| 128 | + |
| 129 | +To: |
| 130 | + |
| 131 | +```yaml |
| 132 | +seed: |
| 133 | + image: cockroachdb/cockroach:latest |
| 134 | + depends_on: |
| 135 | + api: |
| 136 | + condition: service_healthy |
| 137 | + volumes: |
| 138 | + - ./seed.sql:/seed.sql:ro |
| 139 | + entrypoint: |
| 140 | + [ |
| 141 | + "cockroach", |
| 142 | + "sql", |
| 143 | + "--insecure", |
| 144 | + "--host=cockroachdb", |
| 145 | + "--database=httpsms", |
| 146 | + "--file=/seed.sql", |
| 147 | + ] |
| 148 | + restart: "no" |
| 149 | +``` |
| 150 | + |
| 151 | +- [ ] **Step 5: Verify the final `docker-compose.yml` structure** |
| 152 | + |
| 153 | +The final file should contain these services in order: |
| 154 | + |
| 155 | +1. `cockroachdb` — database server |
| 156 | +2. `cockroachdb-init` — creates the database (one-shot) |
| 157 | +3. `redis` — unchanged |
| 158 | +4. `mongodb` — unchanged |
| 159 | +5. `wiremock` — unchanged |
| 160 | +6. `api` — depends on `cockroachdb-init` instead of `postgres` |
| 161 | +7. `seed` — uses `cockroach sql` instead of `psql` |
| 162 | + |
| 163 | +- [ ] **Step 6: Commit** |
| 164 | + |
| 165 | +```bash |
| 166 | +git add tests/docker-compose.yml |
| 167 | +git commit -m "test: replace postgres with cockroachdb in integration tests |
| 168 | +
|
| 169 | +Use cockroachdb/cockroach:latest in single-node insecure mode with |
| 170 | +in-memory storage. Add cockroachdb-init service to create the database |
| 171 | +and update the seed service to use cockroach sql CLI." |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +### Task 2: Update Environment Variables |
| 177 | + |
| 178 | +**Files:** |
| 179 | + |
| 180 | +- Modify: `tests/.env.test` |
| 181 | + |
| 182 | +- [ ] **Step 1: Update DATABASE_URL** |
| 183 | + |
| 184 | +Change line 11: |
| 185 | + |
| 186 | +``` |
| 187 | +DATABASE_URL=postgresql://dbusername:dbpassword@postgres:5432/httpsms |
| 188 | +``` |
| 189 | + |
| 190 | +To: |
| 191 | + |
| 192 | +``` |
| 193 | +DATABASE_URL=postgresql://root@cockroachdb:26257/httpsms?sslmode=disable |
| 194 | +``` |
| 195 | + |
| 196 | +- [ ] **Step 2: Update DATABASE_URL_DEDICATED** |
| 197 | + |
| 198 | +Change line 12: |
| 199 | + |
| 200 | +``` |
| 201 | +DATABASE_URL_DEDICATED=postgresql://dbusername:dbpassword@postgres:5432/httpsms |
| 202 | +``` |
| 203 | + |
| 204 | +To: |
| 205 | + |
| 206 | +``` |
| 207 | +DATABASE_URL_DEDICATED=postgresql://root@cockroachdb:26257/httpsms?sslmode=disable |
| 208 | +``` |
| 209 | + |
| 210 | +- [ ] **Step 3: Add DATABASE_MIGRATION_CONSTRAINT_FIX** |
| 211 | + |
| 212 | +Add the following line after `DATABASE_URL_DEDICATED`: |
| 213 | + |
| 214 | +``` |
| 215 | +DATABASE_MIGRATION_CONSTRAINT_FIX=1 |
| 216 | +``` |
| 217 | + |
| 218 | +This enables the GORM migration workaround for CockroachDB constraint handling (already used in production, see `api/pkg/di/container.go:369-376`). |
| 219 | + |
| 220 | +- [ ] **Step 4: Commit** |
| 221 | + |
| 222 | +```bash |
| 223 | +git add tests/.env.test |
| 224 | +git commit -m "test: update env vars for cockroachdb connection |
| 225 | +
|
| 226 | +Point DATABASE_URL at cockroachdb:26257 with root user (insecure mode). |
| 227 | +Enable DATABASE_MIGRATION_CONSTRAINT_FIX for CockroachDB compatibility." |
| 228 | +``` |
| 229 | + |
| 230 | +--- |
| 231 | + |
| 232 | +### Task 3: Update README Documentation |
| 233 | + |
| 234 | +**Files:** |
| 235 | + |
| 236 | +- Modify: `tests/README.md` |
| 237 | + |
| 238 | +- [ ] **Step 1: Update architecture diagram** |
| 239 | + |
| 240 | +Replace the ASCII diagram (lines 7–25) — change `PostgreSQL` to `CockroachDB` and port `5435` to `26257`: |
| 241 | + |
| 242 | +``` |
| 243 | +┌──────────────┐ HTTP ┌──────────────┐ |
| 244 | +│ Test Runner │─────────────▶│ API (Go) │ |
| 245 | +│ (Go test) │ │ Port 8000 │ |
| 246 | +└──────────────┘ └──────┬───────┘ |
| 247 | + │ |
| 248 | + FCM Push │ Events |
| 249 | + (HTTP) │ (HTTP) |
| 250 | + ▼ |
| 251 | + ┌──────────────┐ |
| 252 | + │ Emulator │ |
| 253 | + │ (Fiber v3) │ |
| 254 | + │ Port 9090 │ |
| 255 | + └──────────────┘ |
| 256 | + │ |
| 257 | + ┌──────┴───────┐ |
| 258 | + │ CockroachDB │ │ Redis │ |
| 259 | + │ Port 26257 │ │ Port 6379 │ |
| 260 | + └──────────────┘ └─────────────┘ |
| 261 | +``` |
| 262 | + |
| 263 | +- [ ] **Step 2: Update Components table** |
| 264 | + |
| 265 | +Change the row: |
| 266 | + |
| 267 | +```markdown |
| 268 | +| **PostgreSQL** | Database for the API | |
| 269 | +``` |
| 270 | + |
| 271 | +To: |
| 272 | + |
| 273 | +```markdown |
| 274 | +| **CockroachDB** | Database for the API (single-node, insecure mode) | |
| 275 | +``` |
| 276 | + |
| 277 | +And change: |
| 278 | + |
| 279 | +```markdown |
| 280 | +| **Seed** | One-shot container that seeds test data into PostgreSQL | |
| 281 | +``` |
| 282 | + |
| 283 | +To: |
| 284 | + |
| 285 | +```markdown |
| 286 | +| **Seed** | One-shot container that seeds test data into CockroachDB | |
| 287 | +``` |
| 288 | + |
| 289 | +- [ ] **Step 3: Update "Start the Stack" section** |
| 290 | + |
| 291 | +In the "3. Start the Stack" section (line 85-89), the command stays the same (`docker compose up -d --build --wait`) but update the description: |
| 292 | + |
| 293 | +Change: |
| 294 | + |
| 295 | +``` |
| 296 | +This starts PostgreSQL, Redis, the API, and the emulator. The `--wait` flag blocks until all health checks pass. |
| 297 | +``` |
| 298 | + |
| 299 | +To: |
| 300 | + |
| 301 | +``` |
| 302 | +This starts CockroachDB, Redis, the API, and the emulator. The `--wait` flag blocks until all health checks pass. |
| 303 | +``` |
| 304 | + |
| 305 | +- [ ] **Step 4: Update "Wait for Seeding" description** |
| 306 | + |
| 307 | +Change line 98: |
| 308 | + |
| 309 | +``` |
| 310 | +The seed container inserts test users, phones, and API keys into PostgreSQL after the API has run its GORM migrations. |
| 311 | +``` |
| 312 | + |
| 313 | +To: |
| 314 | + |
| 315 | +``` |
| 316 | +The seed container inserts test users, phones, and API keys into CockroachDB after the API has run its GORM migrations. |
| 317 | +``` |
| 318 | + |
| 319 | +- [ ] **Step 5: Update Troubleshooting section** |
| 320 | + |
| 321 | +In "API fails to start" common issues (line 185), change: |
| 322 | + |
| 323 | +``` |
| 324 | +- PostgreSQL not ready (increase `start_period` in healthcheck) |
| 325 | +``` |
| 326 | + |
| 327 | +To: |
| 328 | + |
| 329 | +``` |
| 330 | +- CockroachDB not ready (increase `start_period` in healthcheck) |
| 331 | +``` |
| 332 | + |
| 333 | +- [ ] **Step 6: Commit** |
| 334 | + |
| 335 | +```bash |
| 336 | +git add tests/README.md |
| 337 | +git commit -m "docs: update integration test README for cockroachdb |
| 338 | +
|
| 339 | +Replace all PostgreSQL references with CockroachDB in the architecture |
| 340 | +diagram, components table, and troubleshooting section." |
| 341 | +``` |
| 342 | + |
| 343 | +--- |
| 344 | + |
| 345 | +### Task 4: Validate the Stack |
| 346 | + |
| 347 | +- [ ] **Step 1: Generate Firebase credentials (if not already present)** |
| 348 | + |
| 349 | +```bash |
| 350 | +cd tests && bash generate-firebase-credentials.sh |
| 351 | +``` |
| 352 | + |
| 353 | +- [ ] **Step 2: Set the environment variable** |
| 354 | + |
| 355 | +```bash |
| 356 | +export FIREBASE_CREDENTIALS=$(jq -c . firebase-credentials.json) |
| 357 | +``` |
| 358 | + |
| 359 | +- [ ] **Step 3: Start the stack** |
| 360 | + |
| 361 | +```bash |
| 362 | +docker compose up -d --build --wait |
| 363 | +``` |
| 364 | + |
| 365 | +Expected: All services start. CockroachDB reports healthy. cockroachdb-init exits with code 0. API starts and passes healthcheck. |
| 366 | + |
| 367 | +- [ ] **Step 4: Verify seed ran successfully** |
| 368 | + |
| 369 | +```bash |
| 370 | +docker compose wait seed && docker compose logs seed |
| 371 | +``` |
| 372 | + |
| 373 | +Expected: Output shows `INSERT` statements succeeded (no errors). |
| 374 | + |
| 375 | +- [ ] **Step 5: Run integration tests** |
| 376 | + |
| 377 | +```bash |
| 378 | +go test -v -timeout 120s ./... |
| 379 | +``` |
| 380 | + |
| 381 | +Expected: All tests pass (same tests as before — no test code changed). |
| 382 | + |
| 383 | +- [ ] **Step 6: Tear down** |
| 384 | + |
| 385 | +```bash |
| 386 | +docker compose down -v |
| 387 | +``` |
| 388 | + |
| 389 | +- [ ] **Step 7: Final commit (if any fixes were needed)** |
| 390 | + |
| 391 | +If any adjustments were required during validation, commit them: |
| 392 | + |
| 393 | +```bash |
| 394 | +git add -A |
| 395 | +git commit -m "test: fix integration test issues found during validation" |
| 396 | +``` |
0 commit comments