Skip to content

PD4: postgres-drizzle-starter becomes an Orun baseline - #5

Merged
ruehowl merged 2 commits into
mainfrom
claude/blissful-ptolemy-c5ajf8
Oct 15, 2026
Merged

ruehowl merged 2 commits into
mainfrom
claude/blissful-ptolemy-c5ajf8

Conversation

@ruehowl

@ruehowl ruehowl commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

PD4 (task PD-5): this repository becomes an Orun baseline on the Lumen (Cloudflare + Supabase) conventions, built the way cirrus-baseline v22 builds a product — one landing. The reference is sourceplane/saas-starter#6.

How the table got created, and what changed

Upstream (and PD0–PD3) created profiles on the first request: components/table.tsx caught relation "profiles" does not exist and ran lib/seed.ts — CREATE TABLE IF NOT EXISTS, then three INSERT … ON CONFLICT (email) DO NOTHING. No migration, no seed script, no drizzle push.

That DDL is idempotent but not safe under concurrent first requests: IF NOT EXISTS is not checked under a lock, so two sessions can both create the table and one fails on Postgres's catalog index. Measured on Postgres 16 with upstream's exact DDL: 2 of 160 racing sessions failed (pg_type_typname_nsp_index, pg_class_relname_nsp_index) — each a 500 for that visitor. So the schema moved to db-migrate, and the chain is supabase → cloudflare-hyperdrive → db-migrate → web, enforced on a push to main and advisory on PRs.

  • packages/db is Lumen's runner, named @saas/db (stack-tectonic 0.19.0's db-migrate composition builds --filter=@saas/db). Migrations: 000_control, 010_profiles (drizzle-kit's output for lib/drizzle.ts, with IF NOT EXISTS), 020_row_level_security. The drizzle-kit twins are in apps/web/lib/migrations (pnpm db:migrate locally), and tests/db checks the two copies have the same statements.
  • The page now seeds only rows, and only on an empty table (ON CONFLICT (email) DO NOTHING, which is safe to race). A missing table renders an "apply the migrations" notice instead of creating it.
  • Hyperdrive's read cache (second commit). Seeding on an empty table means the first, successful select can be cached empty for up to a minute. The read-back after a seed therefore adds where now() is not null: Cloudflare documents that queries calling STABLE or VOLATILE functions are never cached. /api/health is never cached either (to_regclass is STABLE). This is risk PD-I; it can't be observed offline, because wrangler dev's local binding has no cache.
  • Rule A: migration 020 enables RLS with no policy (the app uses neither the Data API nor Supabase Auth), behind the reference's lock-out guard. .enableRLS() is declared in the schema. tests/db (PGlite) fails if any public table lacks RLS, and drives the guard both ways.
  • Rule B: two Supabase projects per product (stage and prod; the free plan's limit) is in the card's description and in BOOTSTRAP.md's prerequisites.
  • New /api/health: 200 only when the database answers through the HYPERDRIVE binding and profiles exists with RLS. The smoke reads it, plus Recent Users on /.

The baseline

  • Card and build: blueprint.yaml (requires [github, cloudflare, supabase]; previews the five brokered keys: workers-deploy, hyperdrive-edit, account-id, management-access, org-id) and repo-blueprint.yaml. Phase 01-product places every module and runs the reference's hook chain. Phase 02-docs is hook-only.
  • 01-product await: watches the convergence; checks SUPABASE_PROJECT_REF, SUPABASE_DB_PASSWORD, SUPABASE_DB_URL and WIRING_CLOUDFLARE_HYPERDRIVE on stage and prod; probes / and /api/health on both.
  • Machinery: .baseline/, the rename engine (SITE_NAME seam in apps/web/lib/site.ts), tooling/bootstrap, hooks/render-deployment-docs.sh (its Stripe section became a Database section), ai/context, docs/phases, BOOTSTRAP.md, testing/, and the baseline.yml / tag.yml lanes.
  • Copied verbatim from the reference: testing/lib/*, the manifest/coverage/retouch/bootstrap-defer tests, tooling/bootstrap/*, ci.yml, tag.yml, the Terraform roots and their lock files.
  • Also changed:
    • stack-tectonic pinned to 0.19.0.
    • checks.yml runs on PRs only.
    • ci.yml uses the linked gate.
    • wrangler.jsonc is committed as the fixture render.
    • The supabase root is reduced to Lumen's three outputs.
    • The build's site origin is written out. It used to read ${WORKERS_DEV_SUBDOMAIN}, which the composition exports only to the smoke step, so every build would have had an empty subdomain.

Verified

All of this ran locally on this branch (Node 22.22, pnpm 10.12.1, Postgres 16, terraform 1.15.3).

The repository's own checks — all green

  • pnpm install --frozen-lockfile, wire:fixture (leaves the committed wrangler.jsonc unchanged), typecheck, lint, build (/ and /api/health are ƒ).
  • test: 29 tests — 17 in tests/web, 12 in tests/db.
  • pnpm --filter @saas/db run manifest:check.
  • drizzle-kit generate: "No schema changes" (the twins match the schema).

testing/*.test.sh — all ok

  • manifest, phases, coverage, rebrand (31 cases, including an acme name with ' and &), retouch, bootstrap-defer.
  • leak: 107 files placed; none of the factory paths ships or is named.
  • placement:
    • The offline bootstrap of both phases.
    • The product's push-to-main plan is supabase → cloudflare-hyperdrive → db-migrate → web, enforced, on stage and prod.
    • The PR plan holds the same edges advisory.
    • The plan resolves exactly the 5 minted keys and the 4 published ones.
    • After 02-docs, prod waits on stage again.
    • A --resume is a no-op.
    • Ran under orun 2.79.0 and v2.58.11. The advisory half is skipped below the lane pin, as designed.
  • e2e: green under orun 2.79.0 and v2.78.4 (the lane pin). The final run was at v2.78.4, on the head commit.
    • Checks at the 01-product landing.
    • The db-migrate lane's own steps in the staged tree: the apply runs 000/010/020, a second apply is a no-op, and the PR plan step verifies checksums. profiles exists before the app serves — empty, with RLS on, owned by postgres.
    • The web lane's install, verify-app-structure, build-app (Turbopack, inside its own tree, re-rendered with the stage Hyperdrive id), verify-build-output and deploy-dry-run.
    • Then wrangler dev with the Hyperdrive binding on the local Postgres:
      • 8 concurrent first requests all rendered the table, and exactly 3 seed rows were written.
      • The component's own smoke passed.
      • / serves under the product's name, with Open Graph on the stage origin and the upstream link kept.
      • /api/health answered, and so did 4 static assets and a 404.
      • A row inserted behind the app's back shows on the next render.
      • An emptied table is re-seeded.
    • anon and authenticated see 0 rows and cannot write; the app's role is neither superuser nor BYPASSRLS.

orun and Terraform

  • orun new --status: v2.58.11, v2.78.4 and 2.79.0.
  • orun validate and orun work check: clean.
  • terraform fmt -check, init -backend=false (the committed lock files unchanged) and validate: green on both roots.

Verified only offline
These all need a linked workspace with Cloudflare and Supabase connections; Terraform is verified to validate only.

  • Terraform applies (Supabase projects, Hyperdrive configs).
  • A db-migrate apply against the real Management API.
  • A live deploy and a live bootstrap.
  • Hyperdrive's real query cache.

One flake while testing: one v2.78.4 e2e run failed Migration Apply with fetch failed against the local Management API stand-in. Two 2.79.0 runs had passed that step, and the rerun passed. The e2e now checks that the stand-in is answering before the migrate lane and prints its log, instead of failing with a bare fetch failed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AuyckrXahuAVgdmxamUUtk

The repository is an Orun baseline on the Lumen (Cloudflare + Supabase)
conventions, built the way cirrus-baseline v22 builds a product: ONE
landing. 01-product places every module, brands it, links the repository,
reconciles five brokered keys and lands it as one pull request; the
convergence after that merge applies supabase -> cloudflare-hyperdrive ->
db-migrate -> web per environment, every edge enforced on a push to main and
advisory on a pull request. 02-docs is hook-only.

The table moves from the page to db-migrate. Upstream created `profiles`
from its first request: the table component caught `relation "profiles"
does not exist` and ran seed.ts, whose CREATE TABLE IF NOT EXISTS is
idempotent but not safe under concurrent first requests (2 of 160 racing
sessions failed on pg_type_typname_nsp_index / pg_class_relname_nsp_index,
Postgres 16). So:

- packages/db (Lumen's runner, named @saas/db because stack-tectonic's
  db-migrate composition builds that filter): 000_control, 010_profiles
  (drizzle-kit's output for lib/drizzle.ts, IF NOT EXISTS), and
  020_row_level_security (RLS, no policy, with the reference's lock-out
  guard); drizzle-kit twins in apps/web/lib/migrations for local use;
  infra/db-migrate after supabase and cloudflare-hyperdrive.
- The page seeds only rows, on an empty table (INSERT ... ON CONFLICT
  (email) DO NOTHING, safe to race); a missing table renders a notice.
- /api/health: 200 only when the database answers through the HYPERDRIVE
  binding and profiles exists with RLS. The smoke reads it and the page.
- tests/db (PGlite) fails on any table in public without RLS, drives the
  guard both ways, and holds the twins to the same statements.

The baseline: blueprint.yaml (requires [github, cloudflare, supabase], five
secrets previewed, two Supabase projects as an owner prerequisite),
repo-blueprint.yaml, .baseline/, the rename engine with the SITE_NAME seam,
tooling/bootstrap, hooks/render-deployment-docs.sh, ai/context, docs/phases,
BOOTSTRAP.md, testing/ and the baseline.yml / tag.yml lanes. Also:
stack-tectonic 0.19.0, checks.yml on pull requests only, ci.yml's linked
gate, wrangler.jsonc committed as the fixture render, the supabase root
reduced to Lumen's three outputs with lock files on both roots, and the
build's site origin written out (it read a subdomain the build never had).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AuyckrXahuAVgdmxamUUtk
The Hyperdrive config caches read queries for up to a minute (Lumen's
root: caching on; 60 s max_age, 15 s stale-while-revalidate) and does not
invalidate them on a write. Upstream's first select failed, so nothing
empty was ever cached; seeding on an EMPTY table changes that: the first
select succeeds with no rows and is cached, and the same select after the
seed would be answered from it, so a fresh environment's first visitors
would see "Fetched 0 users" for up to a minute.

The read-back after a seed now adds `where now() is not null`. Cloudflare
documents that a query calling a STABLE or VOLATILE function is never
cached, so it reaches the database. Ordinary page views stay cacheable.
/api/health already calls to_regclass (STABLE) and is never cached.

tests/web holds the read-back's shape. wrangler dev's local binding has no
cache, so the e2e cannot observe this; its wording now says so. Documented
in operations.md and risk PD-I.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AuyckrXahuAVgdmxamUUtk
@ruehowl
ruehowl marked this pull request as ready for review October 15, 2026 20:44
@ruehowl
ruehowl merged commit 8366a54 into main Oct 15, 2026
6 checks passed
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.

2 participants