Files
a99d541b45 fix(db): actors outlived their accounts — the last table in the family (#655)
profiles (#637) and cat_conversations (#651) both cascade from the account
now. Each sweep stopped one table short of done, and `actors.user_id` is
what was left: it references nothing at all. Deleting an account still
leaves the actor standing — the row that owns every product, service,
project, cause, event, loan and asset the person ever published.

15 orphaned actors were live in production. They were `e2e-reset-*`,
`cat-wallet-eval-*` and `cascade-proof-*` rows: the verification of the
earlier fixes leaked rows into the table the next fix would be about.
Purged, behind a guard that proved they owned 0 rows across all 25 child
foreign keys before deleting anything.

Unlike its siblings this one cannot be closed with a constraint, which is
why this adds a check rather than a migration:

  - ON DELETE CASCADE would delete the actor, which cascades on into
    contracts, investments and bookings — destroying the COUNTERPARTY's
    record of an agreement because the other side closed their account.
  - ON DELETE SET NULL is rejected outright by actor_type_check, which
    requires actor_type='user' to have a non-null user_id.

The honest fix is a tombstone — keep the actor, sever the identity — which
needs a deleted/anonymous actor_type. That is a schema decision, not a
drive-by, so it is stated here and left for one.

Until it lands:

  - `actors.orphaned` in the nightly invariant gate. This is the part that
    covers call sites nobody thought to patch, including future ones.
  - the two known leak sites delete the actor explicitly:
    refresh-e2e-reset-tokens.mjs (every CI run) and write-test-supabase.js,
    whose comment claimed "cascade removes profile" — true, and precisely
    the trap: it names the fixed thing while missing the one still broken.

Mutation-tested both directions with --gate: one injected orphan makes it
exit 1, removing it exits 0.

Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 19:39:49 +00:00

160 lines
5.3 KiB
JavaScript

#!/usr/bin/env node
// Supabase write test (local/dev/prod)
// Safely creates a temporary auth user, ensures profile exists, creates a funding page, then cleans up.
const path = require('path');
const fs = require('fs');
const { createClient } = require('@supabase/supabase-js');
// Load env from .env.local if available
try {
const envPath = path.join(process.cwd(), '.env.local');
if (fs.existsSync(envPath)) {
require('dotenv').config({ path: envPath });
}
} catch {}
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
const service = process.env.SUPABASE_SERVICE_ROLE_KEY;
function log(section, message, extra) {
const ts = new Date().toISOString();
const tail = extra ? `\n${JSON.stringify(extra, null, 2)}` : '';
console.log(`[${ts}] [${section}] ${message}${tail}`);
}
if (!url || !service) {
log(
'ERROR',
'Missing env: require NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY in .env.local'
);
process.exit(1);
}
const supabase = createClient(url, service, {
auth: { persistSession: false, autoRefreshToken: false },
});
async function columnExists(table, column) {
const { data, error } = await supabase
.from('information_schema.columns')
.select('column_name')
.eq('table_schema', 'public')
.eq('table_name', table)
.eq('column_name', column)
.limit(1);
if (error) return false;
return Array.isArray(data) && data.length > 0;
}
async function ensureProfile(userId) {
// Check if profile exists (trigger should create it on auth.users insert)
const { data, error } = await supabase.from('profiles').select('id').eq('id', userId).limit(1);
if (!error && data && data.length > 0) return true;
// Attempt to create minimal profile if trigger is not present
const { error: insErr } = await supabase
.from('profiles')
.insert({ id: userId, username: `diag_${userId.substring(0, 8)}`, display_name: 'Diag User' });
return !insErr;
}
async function sleep(ms) {
return new Promise(r => setTimeout(r, ms));
}
async function main() {
const runId = Math.random().toString(36).slice(2, 8);
const email = `oc_diag_${Date.now()}_${runId}@example.com`;
log('INFO', 'Creating test auth user', { email });
const { data: created, error: createErr } = await supabase.auth.admin.createUser({
email,
email_confirm: true,
});
if (createErr || !created?.user?.id) {
log('ERROR', 'Failed to create auth user', { error: createErr?.message });
process.exit(2);
}
const userId = created.user.id;
log('INFO', 'Created auth user', { userId });
// Give trigger a moment (if present)
await sleep(250);
const profileOk = await ensureProfile(userId);
if (!profileOk) {
log('ERROR', 'Profile not present and failed to create');
// cleanup user before exit — actor first, see the note at the end of main()
await supabase.from('actors').delete().eq('user_id', userId);
await supabase.auth.admin.deleteUser(userId);
process.exit(3);
}
// Determine funding_pages column for owner
const hasCreator = await columnExists('funding_pages', 'creator_id');
const hasUser = !hasCreator && (await columnExists('funding_pages', 'user_id'));
const ownerColumn = hasCreator ? 'creator_id' : hasUser ? 'user_id' : null;
// Insert a test funding page if table and owner column exist
let fundingId = null;
let fundingOk = false;
if (ownerColumn) {
const slug = `diag-${runId}-${Date.now()}`;
const payload = {
[ownerColumn]: userId,
slug,
title: 'Diagnostics Test Campaign',
description: 'Temporary record created by write-test-supabase.js',
is_public: true,
is_active: true,
total_raised: 0,
supporter_count: 0,
};
const { data, error } = await supabase
.from('funding_pages')
.insert(payload)
.select('id')
.limit(1);
if (!error && data && data.length > 0) {
fundingId = data[0].id;
fundingOk = true;
log('INFO', 'Inserted funding page', { fundingId, slug });
} else {
log('WARN', 'Failed to insert funding page (continuing)', { error: error?.message });
}
} else {
log('WARN', 'No suitable owner column found on funding_pages (skipping funding page write)');
}
// Cleanup: delete funding page if created
if (fundingId) {
await supabase.from('funding_pages').delete().eq('id', fundingId);
log('INFO', 'Deleted test funding page', { fundingId });
}
// Cleanup: the profile goes with the auth user via profiles_id_fkey CASCADE,
// but the ACTOR does not — actors.user_id still has no foreign key, so it has
// to be deleted explicitly or this diagnostic leaks a row into production
// every time it runs. (The nightly `actors.orphaned` invariant is the
// backstop for every call site that forgets, including future ones.)
await supabase.from('actors').delete().eq('user_id', userId);
await supabase.auth.admin.deleteUser(userId);
log('INFO', 'Deleted test auth user', { userId });
const summary = { userCreated: true, profileOk, fundingOk };
if (profileOk && (fundingOk || ownerColumn === null)) {
log('RESULT', 'Write test successful', summary);
} else {
log('RESULT', 'Write test completed with issues', summary);
process.exit(4);
}
}
main().catch(async err => {
log('ERROR', 'Write test crashed', { error: err?.message || String(err) });
process.exit(1);
});