Files
orangecat/scripts/db/setup-db.ts
CatoandClaude Opus 5 3e6c202c59 refactor(config): the deploy target is configuration, not a literal (#1061)
Three hardcoded things in a public repo, each of which is someone else's
value baked into the source.

The box address had a literal fallback in five places: the CD workflow and
four operational shell scripts. A fallback literal guarantees the variable
is never exercised — if the variable were missing or wrong, every run would
still succeed against the baked-in address and nobody would find out until
the address changed. So:

  - cd.yml takes the address from vars.OC_BOX, or root@ the HETZNER_IP
    variable, and a new guard step fails the run with ::error:: when the
    result is empty. The ssh-keyscan and ssh-config host now derive from
    the host half of OC_BOX instead of repeating the literal, so the two
    cannot drift apart silently.
  - apply-migrations.sh, db/dump-live-schema.sh, db/gen-types.sh and
    deploy-selfhost.sh use ${OC_BOX:?...}: they stop with one clear line
    instead of reaching for a default. The two of them invoked by the
    deploy script are already passed OC_BOX explicitly, and the workflow
    sets it in the job env, so the configured path is unchanged.

An audit log was committed under logs/ in 2025-10 with 14 absolute paths
from a developer's machine. Nothing in the repo reads it (the only mentions
of the filename are inside the file itself). The `logs/` ignore rule was
added five days after the file, so it never applied to it — removed, and
the rule now says why, because ignoring a directory does not untrack what
is already inside it.

scripts/db/setup-db.ts signed in as a personal email address written into
the source. It now reads USER_EMAIL, matching the USER_PASSWORD it already
read, and exits with a clear message when unset.

Verified: bash -n on all four scripts and each one run with OC_BOX unset
(exit 1, clear message); the workflow parses and the guard step is in
place; pnpm run type-check:scripts green; prettier clean; the two unit
tests that read deploy-selfhost.sh pass.


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

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 01:07:48 -07:00

58 lines
1.8 KiB
TypeScript

import 'dotenv/config';
import { createClient } from '@supabase/supabase-js';
import { neutralUsernameFor } from '@/lib/profile/neutral-username';
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;
const supabase = createClient(supabaseUrl, supabaseKey);
// Which account this bootstraps is the operator's, not the repo's: a name baked
// in here is a personal detail in a public repo *and* a silently wrong answer
// for anyone else who runs it. Pairs with USER_PASSWORD below.
const userEmail: string = process.env.USER_EMAIL ?? '';
if (!userEmail) {
console.error('setup-db: USER_EMAIL is not set — set it to the account to sign in as.');
process.exit(1);
}
async function setupDatabase() {
try {
// REMOVED: console.log statement
// Sign in
const { error: signInError } = await supabase.auth.signInWithPassword({
email: userEmail,
password: process.env.USER_PASSWORD || 'your-password',
});
if (signInError) throw signInError;
if (process.env.NODE_ENV === 'development') console.log('✅ Signed in');
// Create initial profile
const {
data: { user },
error: userError,
} = await supabase.auth.getUser();
if (userError) throw userError;
if (user) {
const { error: profileError } = await supabase.from('profiles').upsert({
id: user.id,
// Never the email local part — see src/lib/profile/neutral-username.ts.
username: neutralUsernameFor(user.id),
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
});
if (profileError) throw profileError;
// REMOVED: console.log statement
}
// REMOVED: console.log statement
} catch (error) {
console.error('Error setting up database:', error);
process.exit(1);
}
}
setupDatabase();