Skip to content

TL-M0-18 · CI database job

Why: Until now, CI never touched the database. This step adds a database job to CI. On every pull request that changes the database side, it starts the real local Supabase stack, resets it, runs every pgTAP test, regenerates the types and the reference pages, and fails if any generated file differs from what is committed (D12, D19, D27, D33). A path filter decides when it runs, not Turborepo's affected graph, because supabase/ is not a package (D12). After the job has run green once, the ruleset makes it required, so a pull request that breaks a test or forgets to regenerate the types can't merge.

ID TL-M0-18 · Level L0 · Run order row 23 · Issue #15

CI has three jobs: checks, changes and database. The database job runs on pull requests that touch the database side and on every push to main, and it passes. A pull request that touches nothing on the database side shows database as skipped. The ruleset on main requires checks, pr-title and database. Since #732, the push to main skips the database and e2e jobs: the pull request into main has already run them.

  • Row 22, BE pack E (#14), is Done: pnpm db:types and pnpm docs:reference exist, and the committed types and reference pages come from the current schema.
  • You are on main in ~/opleet-v2/opleet, up to date, and git status --short prints nothing.
  • Docker Desktop is running.

1 · Check that main is clean the way CI will check it

Section titled “1 · Check that main is clean the way CI will check it”

This runs the database job's commands on your Mac first. If main's committed files are already stale, CI fails on its first run, and this block shows why before you push anything.

Terminal window
git switch main && git pull
pnpm supabase start
pnpm supabase db reset
pnpm supabase test db
pnpm db:types
pnpm docs:reference
git status --short -- packages/db-types apps/docs/src/content/docs/reference

This replaces .github/workflows/ci.yml as a whole. The checks job is unchanged; changes and database are new.

Terminal window
git switch -c ci/database
cat > .github/workflows/ci.yml <<'EOF'
name: CI
on:
pull_request:
push:
branches: [main]
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
env:
NEXT_TELEMETRY_DISABLED: 1
ASTRO_TELEMETRY_DISABLED: 1
TURBO_TELEMETRY_DISABLED: 1
jobs:
checks:
name: checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
node-version-file: .node-version
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm biome ci .
- run: pnpm docs:rules
- run: pnpm turbo run typecheck build ${{ github.event_name == 'pull_request' && '--affected' || '' }}
# Decides whether the database job runs: a path filter, not Turborepo's affected graph,
# because supabase/ is not a package (D12). The filter lives in a job rather than in
# on.paths, so the database check always reports: skipped counts as passing in the ruleset.
changes:
name: changes
runs-on: ubuntu-latest
outputs:
database: ${{ steps.filter.outputs.database }}
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- id: filter
name: Did this change touch the database side?
env:
EVENT: ${{ github.event_name }}
BASE: ${{ github.event.pull_request.base.sha }}
HEAD: ${{ github.event.pull_request.head.sha }}
run: |
if [ "$EVENT" != "pull_request" ]; then
echo "database=true" >> "$GITHUB_OUTPUT"
exit 0
fi
paths='^(supabase/|tools/db/|packages/db-types/|apps/docs/src/content/docs/reference/|\.github/workflows/ci\.yml$|package\.json$|pnpm-lock\.yaml$)'
if git diff --name-only "$BASE...$HEAD" | grep -Eq "$paths"; then
echo "database=true" >> "$GITHUB_OUTPUT"
else
echo "database=false" >> "$GITHUB_OUTPUT"
fi
database:
name: database
needs: changes
if: needs.changes.outputs.database == 'true'
runs-on: ubuntu-latest
timeout-minutes: 25
steps:
- uses: actions/checkout@v7
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
node-version-file: .node-version
cache: pnpm
- run: pnpm install --frozen-lockfile
- name: A signing key for this run only (D13)
run: pnpm db:signing-key
- name: Start the local stack (D12)
run: pnpm supabase start -x studio,imgproxy,mailpit,edge-runtime,logflare,vector,supavisor,realtime
- run: pnpm supabase db reset
- name: pgTAP (D19)
run: pnpm supabase test db
- name: Regenerate the types and the reference pages
run: |
pnpm db:types
pnpm docs:reference
- name: The generated files match the schema (D12, D27, D33)
run: |
generated="packages/db-types apps/docs/src/content/docs/reference"
if [ -n "$(git status --porcelain -- $generated)" ]; then
git status --short -- $generated
git --no-pager diff --stat -- $generated
echo "::error::Generated files are out of date. Run pnpm db:types and pnpm docs:reference with the stack up, and commit what they change (D33)."
exit 1
fi
echo "Types and reference pages are current."
- if: always()
run: pnpm supabase stop --no-backup
EOF
git add .github/workflows/ci.yml
git commit -m "ci: run the database job on database changes"

3 · Push, open the pull request, and watch the checks

Section titled “3 · Push, open the pull request, and watch the checks”

The pull request changes ci.yml, which is on the path filter, so the database job runs. The first run pulls the Supabase images, which takes a few minutes.

Terminal window
git push -u origin ci/database
gh pr create --fill
gh pr checks --watch

4 · See the job catch hand-edited types, then undo it

Section titled “4 · See the job catch hand-edited types, then undo it”
Terminal window
echo "// edited by hand" >> packages/db-types/src/database.ts
git commit -am "test: a hand edit the database job must catch"
git push
sleep 15 && gh pr checks --watch
git revert --no-edit HEAD
git push
sleep 15 && gh pr checks --watch
Terminal window
gh pr merge --squash --delete-branch
git switch main
git pull

6 · Make the database job required on main

Section titled “6 · Make the database job required on main”

The job must have run once before the ruleset can require it. A pull request that skips the job still passes, because GitHub counts a skipped job as passing.

Terminal window
id=$(gh api repos/Opleettop/monorepo/rulesets --jq '.[] | select(.name == "main") | .id')
gh api repos/Opleettop/monorepo/rulesets/$id \
--jq '{rules: [.rules[] | if .type == "required_status_checks" then .parameters.required_status_checks += [{"context": "database"}] else . end]}' \
| gh api -X PUT repos/Opleettop/monorepo/rulesets/$id --input - \
--jq '.rules[] | select(.type == "required_status_checks") | .parameters.required_status_checks | map(.context)'
  • Block 1: pnpm supabase test db ends with "Result: PASS". The last command prints nothing. Any line it prints is a generated file that is already stale on main: stop, and see If it fails.
  • Block 2: Lefthook shows ✔️ biome and ✔️ commitlint, then one file changed.
  • Block 3: checks, changes, database and pr-title all pass. The database log ends with "Types and reference pages are current."
  • Block 4:
    • After the first push, database fails, with "Generated files are out of date" and packages/db-types/src/database.ts listed.
    • After the revert, it passes again.
    • checks passes both times: Biome skips database.ts, and a comment line typechecks.
  • Block 5: "✓ Squashed and merged pull request".
  • Block 6: ["checks", "pr-title", "database"].
Terminal window
gh run list --workflow CI --branch main --limit 1
gh api repos/Opleettop/monorepo/rules/branches/main \
--jq '.[] | select(.type == "required_status_checks") | .parameters.required_status_checks | map(.context)'

The run on main after the merge shows completed and success, and the required checks are checks, pr-title and database.

On the next pull request that touches only apps/web or apps/docs pages outside reference/, the database job shows as skipped, and the pull request can still merge. Note this in the run log when it happens.

  • Block 1 prints a file: main's generated files are stale. Don't start this step. Paste the output in #15. The Backend Engineer commits the regenerated files in a pull request first (D33).
  • The database job fails in "Start the local stack": paste the step's log (gh run view --log-failed). A pull limit or a registry error usually clears on a re-run: gh run rerun --failed.
  • The database job fails in "pgTAP": the tests fail on the CI stack but passed in block 1. Paste the log in #15. Don't merge.
  • "Generated files are out of date" on block 3's run: the files differ between your Mac and CI, which can mean a different CLI version. Compare pnpm supabase --version with the 2.119.0 pinned in package.json, and paste the step's log.
  • Block 6 errors: paste the output. The ruleset is unchanged until the PUT succeeds.
  • Before merging: gh pr close --delete-branch.
  • After merging: revert the commit with a new pull request.
  • To undo block 6: run it again with -= in place of +=. That removes database from the required checks, and do it before reverting the workflow, or every pull request waits for a check that never comes.

Log each run as a comment on #15: the date, what you ran, and the output that mattered.