Files
orangecat/scripts
GandGeorgy Butaev f2bfa7cf30 fix(db): profiles outlived their users — make that structurally impossible (#637)
public.profiles had NO foreign key at all. `profiles.id` held an
auth.users id by convention only, so deleting a user removed the auth
row and left the profile standing forever: a public page, a username
still taken, and a row counted by every "how many users" query.

Measured in production today: 115 orphaned profiles. 113 came from the
per-run `e2e-reset-*` CI fixtures minted since 2026-06-12.
refresh-e2e-reset-tokens.mjs was doing its job the whole time — it calls
admin.deleteUser on throwaways older than a day — but with no cascade
the profile survived every single deletion.

The consequence was not cosmetic: 136 of 185 profile rows (73%) were
test accounts, so OrangeCat's own signup numbers were mostly synthetic.
The real figure — 39 profiles, 5 with a bio, 3 created in the last 30
days — was invisible behind them. A metric nobody can trust is worse
than no metric, because it still gets used to decide what to build.

No real end user has been orphaned yet; the other 2 are our own
memtest_* / selfhost-verify+* fixtures. But nothing prevented it, and
"delete my account" must not leave a public profile behind.

- FK profiles.id -> auth.users(id) ON DELETE CASCADE. A delete path you
  have to remember to call is one that eventually is not called; a
  foreign key cannot be forgotten. Added NOT VALID so it governs every
  future write without requiring the 115 existing rows to be deleted —
  purging live production data is a separate, explicit decision and this
  migration must not smuggle it in.
- count_orphaned_profiles(), service_role only, because auth.users is
  not reachable through PostgREST and the nightly gate needs to see it.
- New `profiles.orphaned` invariant in check-data-invariants.mjs — the
  backstop for the constraint being dropped, bypassed, or left NOT VALID
  while something writes around it.

Verified against the production schema in a rolled-back transaction:
migration applies clean, the gate reports 115, and end to end an auth
user insert creates a profile (1) while deleting that user now removes
it (0). Before this, that second number was 1.

The gate will report the 115 existing orphans until they are cleared;
that is the honest state, not a regression. After clearing:
ALTER TABLE public.profiles VALIDATE CONSTRAINT profiles_id_fkey;

Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com>
2026-08-07 00:30:46 +00:00
..
2025-12-10 16:56:40 +01:00

Scripts Directory

This directory contains all automation scripts organized by purpose.

Directory Structure

  • analysis/ - Bundle analysis, documentation checking, and optimization tools
  • auth-fix/ - Scripts to fix authentication and RLS policy issues
  • db/ - Database operations, migrations, and schema management
    • Migration scripts moved from root: apply-migration.js, apply-*-migration.js
    • Old schema files archived in db/archive/
  • deployment/ - Deployment, production setup, and monitoring scripts
  • dev/ - Development helpers, local setup, and debugging tools
  • diagnostics/ - Health checks and system diagnostics
  • maintenance/ - Cleanup, optimization, and maintenance scripts
  • monitoring/ - Performance monitoring and bundle size tracking
  • oauth/ - OAuth provider setup and configuration
  • profile-fix/ - Scripts to fix profile-related issues
  • schema-fix/ - Database schema fixes and migrations
  • storage/ - Supabase storage bucket setup and management
  • test/ - Testing utilities and validation scripts
    • Test/debug scripts moved from root: test-*.js, check-*.js, debug-*.js

Usage

Most scripts can be run with:

node scripts/{category}/script-name.js

For deployment scripts:

bash scripts/deployment/script-name.sh

Important Scripts

  • scripts/dev/dev-start.js - Main development server starter
  • scripts/deployment/browser-verify.js - Post-deploy browser smoke test (deploys themselves happen on the box — see docs/deployment/DEPLOYMENT_PROCESS.md)
  • scripts/db/apply-migrations.ts - Database migrations
  • scripts/maintenance/cleanup-console-logs.ts - Remove console.log statements