Skip to content
This repository was archived by the owner on Oct 18, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
branches: [master, dev]
pull_request:
branches: [master, dev]

jobs:
quality-gates:
runs-on: ubuntu-latest
env:
# Placeholder URLs: test modules import PrismaClient at load time; the DB-backed
# tests are skipped via CI_SKIP_DB so no connection is ever made.
DATABASE_URL: postgresql://ci:ci@localhost:5432/ci
DIRECT_URL: postgresql://ci:ci@localhost:5432/ci
CI_SKIP_DB: "true"
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- name: Schema migration guard
run: bash scripts/check-schema-migration.sh origin/master
- run: npm test
- run: npm run build
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
- always write what you changed into HANDOFF.md
- prisma/schema.prisma changes must ship with a migration under prisma/migrations/
(enforced by scripts/check-schema-migration.sh in CI); record the DB apply step in HANDOFF.md.
42 changes: 40 additions & 2 deletions HANDOFF.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
# HANDOFF

## 2026-10-11 — CI schema-migration guard + quality gates

### What changed
- **New `scripts/check-schema-migration.sh [base]`** (default `origin/master`): fails if
`prisma/schema.prisma` changed vs the base ref and nothing under `prisma/migrations/` changed.
Message tells you to add `prisma/migrations/<timestamp>_<name>/migration.sql` and record the DB
apply step in HANDOFF.md.
- **New `.github/workflows/ci.yml`**: on push/PR to `master` + `dev` (Node 22, `fetch-depth: 0`),
runs schema guard → `npm test` → `npm run build`.
- **DB-backed suites skip in CI** via `{ skip: process.env.CI_SKIP_DB === 'true' }`
(`test/ingest-utc.test.js` "Database …" block, `test/challenge-sync.test.js`). The workflow sets
placeholder `DATABASE_URL`/`DIRECT_URL` so `PrismaClient` can still be imported; no connection is made.
- **`AGENTS.md`**: added the rule that `prisma/schema.prisma` changes must ship with a migration.
- **New migration** `prisma/migrations/20261011000000_add_is_leave_to_study_logs_v2/migration.sql`:
`ALTER TABLE "study_logs_v2" ADD COLUMN IF NOT EXISTS "isLeave" boolean NOT NULL DEFAULT false;`

### DB apply step
- The `isLeave` migration is a **no-op in production** (the column already exists; `IF NOT EXISTS`).
Apply via `prisma migrate deploy` or the raw SQL where a DB lacks it. Nothing in the build pipeline
syncs the schema — apply manually.

### Verification
- Guard paths verified in a throwaway repo: pass (no schema change), fail (schema change w/o
migration), pass (with migration), and a clear error for a missing base ref.
- The guard currently **fails on `dev`** because commit `303bbbf` changed `prisma/schema.prisma`
with no migration — expected; it passes once this migration commit lands in the branch range.
- `CI_SKIP_DB=true DATABASE_URL=… npm test` → 32 pass / 0 fail, DB suites `SKIP`.
- `npm run build` → exit 0 with the placeholder env.

### Open / follow-ups
- Live drift detection (`prisma/schema.prisma` vs the actual DB) is **not** implemented — would need a
DB secret in Actions.
- Commit `303bbbf` (winston logging + day buckets) is on `dev` ahead of `master`; open a PR to `master`
when ready (the guard will require this migration to be included).

---

## 2026-10-11 — Centralized winston logging (for incident diagnosis)

### What changed
Expand Down Expand Up @@ -74,8 +111,9 @@ Requested follow-up (this change): **ignore the YPT day; use the product's day b
previous UTC calendar date for all sampled instants → **no data migration/backfill needed**.

### Open items / next steps
- **Not done:** monotonic/zero-overwrite guard (a 0 YPT sample can still overwrite a day's value).
Recommended for a follow-up.
- **Rejected (2026-10-11):** monotonic/zero-overwrite guard. A `max(existing, incoming)` write would
also block legitimate downward corrections (e.g. a user removing hours in YPT). Do not re-add it
unless real decreases can be distinguished from glitchy `0` samples.
- Minor data inconsistency (flagged, not fixed): 3 rows in `study_logs_v2` still have
`status='Offline'` (wrong casing; 2026-10-09). Normalize to `'OFFLINE'` if desired.
- **Done (2026-10-11):** fixed the flaky `test/session-cap.test.js` exact-ms assertion — the "~2h"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Adds the isLeave column to study_logs_v2 so prisma/schema.prisma matches the push-managed prod DB.
-- The column already exists in production; IF NOT EXISTS makes this a no-op there.
-- DB apply step is recorded in HANDOFF.md (2026-10-11 — CI schema-migration guard).
ALTER TABLE "study_logs_v2" ADD COLUMN IF NOT EXISTS "isLeave" boolean NOT NULL DEFAULT false;
32 changes: 32 additions & 0 deletions scripts/check-schema-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Fails if prisma/schema.prisma changed without a migration under prisma/migrations/.
# Usage: scripts/check-schema-migration.sh [base-ref] (default: origin/master)
set -euo pipefail

BASE="${1:-origin/master}"

if ! git rev-parse --verify --quiet "$BASE" >/dev/null; then
echo "ERROR: base ref '$BASE' not found — fetch it first (CI uses actions/checkout fetch-depth: 0)." >&2
exit 1
fi

CHANGED="$(git diff --name-only "$BASE"...HEAD || true)"

schema_changed=false
migrations_changed=false
if printf '%s\n' "$CHANGED" | grep -q '^prisma/schema\.prisma$'; then schema_changed=true; fi
if printf '%s\n' "$CHANGED" | grep -q '^prisma/migrations/'; then migrations_changed=true; fi

if [ "$schema_changed" = true ] && [ "$migrations_changed" = false ]; then
cat >&2 <<'EOF'
ERROR: prisma/schema.prisma changed but no migration was added under prisma/migrations/.

The production database is push-managed (nothing in the build pipeline syncs the schema),
so a bare schema change with no migration is how drift ships.

Add prisma/migrations/<timestamp>_<name>/migration.sql and record the DB apply step in HANDOFF.md.
EOF
exit 1
fi

echo "OK: schema/migration guard passed."
2 changes: 1 addition & 1 deletion test/challenge-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('formatUtcDateString', () => {
});
});

describe('syncChallengeFromMonday', () => {
describe('syncChallengeFromMonday', { skip: process.env.CI_SKIP_DB === 'true' }, () => {
test('syncs historical days from Monday (2026-10-05) into study_logs_v2 with override preservation', async () => {
const testNow = new Date('2026-10-10T12:00:00.000Z');
const result = await syncChallengeFromMonday({
Expand Down
2 changes: 1 addition & 1 deletion test/ingest-utc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('deriveStudyStatus', () => {
});
});

describe('Database Active Challenge & Participant Resolution (UTC)', () => {
describe('Database Active Challenge & Participant Resolution (UTC)', { skip: process.env.CI_SKIP_DB === 'true' }, () => {
test('dynamically finds active challenge within date range in UTC', async () => {
// October Team Battle is 2026-10-05T00:00:00Z to 2026-10-12T00:00:00Z
const testUtcTime = new Date('2026-10-08T12:00:00.000Z');
Expand Down
Loading