Files
botsmann/lib/supabase-server.ts
T
93d6ed067b fix(db): our tables are not in orangecat's schema — tell every client (#138)
/api/health has returned 503 because `public.consultations` does not exist. It
never did. Botsmann shares one self-hosted Supabase database with orangecat,
which owns `public` and its 128 tables; ours were nowhere, because all eleven
migrations were unapplied.

The schema half is fixed in the deploy pipeline (fleetcrown#398): botsmann now
declares `supabase:botsmann` and its migrations apply into a schema of its own,
exactly as printcraft's do. That has run — all 11 tags are recorded and the 12
tables exist.

This is the other half. A Supabase client that is not told the schema queries
`public`, so it would still get PGRST205. Measured against production just now:

  GET /rest/v1/consultations                      -> 404 PGRST205
  GET /rest/v1/consultations  Accept-Profile: botsmann -> 200 []

`db: { schema }` is what sends that header, and it was missing from all four
client factories — anon, service-role, browser and route-handler.

The schema name is one constant in lib/constants.ts, not four literals. The
second copy is how the first goes stale, and this repo has just spent a day
proving what duplicated configuration costs.

Two things this also removes:

  * `scripts/run-migration.ts` and `scripts/migrate-via-api.ts` applied SQL
    through the service-role client with no schema set. They are from the era
    when migrations were run by hand; now they would create our tables inside
    orangecat's `public`. Schema is the pipeline's job, and a second way to do
    it is a second way to do it wrong. Nothing references them.
  * The bare `SupabaseClient` type hardcodes `'public'`, so annotating with it
    silently asserts we query orangecat's tables — and rejects the client we
    actually build. The type is now derived from the factory, which keeps the
    two in step and survives supabase-js reordering its generics.

New test pins it per file, by count: every `createClient` call in a client file
must set the schema, from the shared constant, with comments stripped first so
the guard cannot be fooled by prose. Adding a fifth unscoped factory fails the
test instead of production. Mutation-checked: unscoping the browser client
fails it.

verify green: 237 tests, lint, typecheck, build.


Claude-Session: https://claude.ai/code/session_01HVwg8DKHQktxJuHeLM3xpG

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-27 19:57:39 +00:00

44 lines
1.4 KiB
TypeScript

/**
* Server-side Supabase utilities
*
* This file contains utilities that use `next/headers` and can only
* be used in server components and API routes.
*
* For client components, use `createClientComponentClient` from `@/lib/supabase`.
*/
import { createServerClient } from '@supabase/ssr';
import { cookies } from 'next/headers';
import { getClientEnv } from '@/lib/config/env';
import { DB_SCHEMA } from '@/lib/constants';
/**
* Create Supabase client for route handlers (API routes)
* Replaces deprecated createRouteHandlerClient from @supabase/auth-helpers-nextjs
*
* @param _options - Options object (for backwards compatibility, not used)
*/
export async function createRouteHandlerClient(_options?: { cookies?: unknown }) {
const { NEXT_PUBLIC_SUPABASE_URL: supabaseUrl, NEXT_PUBLIC_SUPABASE_ANON_KEY: supabaseAnonKey } =
getClientEnv();
const cookieStore = await cookies();
return createServerClient(supabaseUrl, supabaseAnonKey, {
db: { schema: DB_SCHEMA },
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
} catch {
// The `setAll` method was called from a Server Component.
// This can be ignored if you have middleware refreshing user sessions.
}
},
},
});
}