Files
orangecat/scripts/check-schema-columns.mjs
df6edd3c7f fix(wallets): stop dividing a BTC balance by a goal denominated in francs (#758)
A wallet's balance is BTC. Its goal is not: `goal_amount` is denominated
in `goal_currency`, and the wallet form defaults that to fiat. Both
surfaces that drew the progress bar divided one straight into the other:

    const progressPercent = (wallet.balance_btc / wallet.goal_amount) * 100

Measured against production today: 18 of 33 wallets carry a goal, every
one of them in fiat (CHF 11, USD 6, EUR 1), and not one in BTC. So every
savings-goal bar in the app was computed from BTC divided by francs —
including the one on the PUBLIC profile, which is the second copy of the
same expression.

It is invisible right now only because every one of those balances is
still 0.00, and zero over anything is zero. It starts lying on the first
deposit: a wallet holding 0.025 BTC toward a CHF 2 500 goal would have
read "0.001% funded" while sitting at the whole target. That case is
pinned as a test.

Both sides now meet in the goal's own currency before they meet in one
expression, via a single lib/wallet-goal.ts rather than a third inline
copy. When no rate is available the helper returns null instead of a
number: useCurrencyConversion signals a missing rate with 0 precisely so
callers do not guess, and 0 read as a percentage would tell someone they
have saved nothing when they may be fully funded.

Fixing the same line in two files is where the Never-Twice rule says to
automate, so check:currency-units joins `verify`: it fails on arithmetic
between a BTC-suffixed identifier and a bare `*_amount`. Proven by
mutation — restoring the original expression turns it red, and it caught
the naive version of its own scanner on the first run (it stripped only
`//`, and the first thing it read was a block comment describing the
bug). That is why blankNonCode moved to scripts/lib/: one tokenizer, so
a third scanner cannot reinvent the version that already cost a day.

Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 01:06:41 +00:00

339 lines
13 KiB
JavaScript

#!/usr/bin/env node
/**
* Every field an entity schema collects must EXIST as a column.
*
* check-dead-fields.mjs asks "is this field rendered anywhere?". This asks the
* question underneath it: "can this field be stored at all?". A field with no
* column is worse than one that displays nowhere — `buildUpdatePayload` omits
* null/undefined, so nothing breaks until a user ACTUALLY FILLS IT IN, and then
* PostgREST rejects the entire payload and the whole save fails. The people who
* use the feature are the only ones who ever see it break.
*
* That is not hypothetical: on 2026-08-25 `projects.start_date` and
* `projects.target_completion` had a form field, a Zod rule, an API mapping and
* guidance copy, and had never been columns — not in the baseline, not in any
* migration. Filling either one failed the project save.
*
* WHY IT PARSES MIGRATIONS RATHER THAN QUERYING THE DATABASE
* CI has no database credentials, and a gate that needs the network is a gate
* that goes red when the network hiccups — which is how gates get disabled.
* supabase/migrations/ is the committed SSOT for schema, is offline, and is
* deterministic. The cost is that the parser must be right; see below.
*
* WHY YOU CAN TRUST THE PARSER
* `--verify` compares every reconstructed table against the LIVE database and
* reports any difference. That was run against production before this gate
* landed and matched exactly for all mapped tables. Re-run it after touching the
* parser: `node scripts/check-schema-columns.mjs --verify` (needs .env.local).
* A parser that silently misses an ALTER produces FALSE failures, and a gate
* that cries wolf gets deleted — so proving it is not optional.
*/
import { readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { blankNonCode } from './lib/blank-non-code.mjs';
const ROOT = new URL('..', import.meta.url).pathname;
const MIGRATIONS = join(ROOT, 'supabase/migrations');
const VALIDATION = join(ROOT, 'src/lib/validation');
/**
* Schema export -> table. Explicit on purpose: one validation FILE can hold
* several schemas for several tables (wishlist.ts has four), and mapping by
* filename silently compares item fields against the wishlist table. Anything
* not listed here is skipped, and the count of skipped schemas is printed —
* a gate must never quietly narrow its own scope.
*/
const SCHEMA_TABLES = {
projectSchema: 'projects',
userProductSchema: 'user_products',
userServiceSchema: 'user_services',
userCauseSchema: 'user_causes',
aiAssistantSchema: 'ai_assistants',
assetSchema: 'assets',
loanSchema: 'loans',
investmentSchema: 'investments',
eventSchema: 'events',
wishlistSchema: 'wishlists',
wishlistItemSchema: 'wishlist_items',
circleSchema: 'circles',
documentSchema: 'user_documents',
profileSchema: 'profiles',
};
/**
* Fields a schema legitimately accepts that are NOT columns of its own table.
* Ratchet: this list may shrink, never grow without a reason on the line.
*/
const NOT_COLUMNS = new Set([
// Write-side routing: names the row's parent/target, resolved before insert.
'wishlist_id',
'wishlist_item_id',
// Accepted for convenience and mapped onto a differently-named column, or
// consumed entirely by the API layer.
'entity_type',
'entity_id',
// A child RELATION, not a column: collateral rows live in public.loan_collateral
// and are written through /api/loan-collateral. loanSchema accepts the array so
// the create form can submit loan + collateral together; the API splits them.
'collateral',
]);
// ---------------------------------------------------------------- SQL parsing
/** Strip -- line comments and slash-star blocks so they cannot fake a match. */
function stripComments(sql) {
return sql.replace(/\/\*[\s\S]*?\*\//g, '').replace(/--[^\n]*/g, '');
}
const RESERVED_ROW = /^(constraint|primary|foreign|unique|check|exclude|like|partition)\b/i;
/** Column names from a CREATE TABLE body, ignoring table-level constraints. */
function columnsFromBody(body) {
const cols = [];
let depth = 0;
let current = '';
for (const ch of body) {
if (ch === '(') depth++;
if (ch === ')') depth--;
if (ch === ',' && depth === 0) {
cols.push(current);
current = '';
continue;
}
current += ch;
}
cols.push(current);
return cols
.map(c => c.trim())
.filter(c => c && !RESERVED_ROW.test(c))
.map(c => c.match(/^"?([a-zA-Z_][a-zA-Z0-9_]*)"?/)?.[1])
.filter(Boolean);
}
/** Reconstruct table -> Set(columns) by replaying migrations in filename order. */
export function buildSchema() {
const tables = new Map();
const files = readdirSync(MIGRATIONS)
.filter(f => f.endsWith('.sql'))
.sort();
for (const file of files) {
const sql = stripComments(readFileSync(join(MIGRATIONS, file), 'utf8'));
// CREATE TABLE [IF NOT EXISTS] [public.]name ( ... );
const createRe =
/create\s+table\s+(?:if\s+not\s+exists\s+)?(?:public\.)?"?([a-zA-Z_][a-zA-Z0-9_]*)"?\s*\(/gi;
let m;
while ((m = createRe.exec(sql)) !== null) {
const name = m[1];
// Walk to the matching close paren so nested types/constraints survive.
let i = createRe.lastIndex;
let depth = 1;
const start = i;
while (i < sql.length && depth > 0) {
if (sql[i] === '(') depth++;
else if (sql[i] === ')') depth--;
i++;
}
const body = sql.slice(start, i - 1);
const existing = tables.get(name) ?? new Set();
for (const c of columnsFromBody(body)) existing.add(c);
tables.set(name, existing);
}
// ALTER TABLE [public.]name ADD/DROP/RENAME COLUMN ...
const alterRe =
/alter\s+table\s+(?:only\s+)?(?:if\s+exists\s+)?(?:public\.)?"?([a-zA-Z_][a-zA-Z0-9_]*)"?([\s\S]*?);/gi;
while ((m = alterRe.exec(sql)) !== null) {
const name = m[1];
const rest = m[2];
const set = tables.get(name) ?? new Set();
for (const a of rest.matchAll(
/add\s+column\s+(?:if\s+not\s+exists\s+)?"?([a-zA-Z_][a-zA-Z0-9_]*)"?/gi
)) {
set.add(a[1]);
}
for (const d of rest.matchAll(
/drop\s+column\s+(?:if\s+exists\s+)?"?([a-zA-Z_][a-zA-Z0-9_]*)"?/gi
)) {
set.delete(d[1]);
}
for (const r of rest.matchAll(
/rename\s+column\s+"?([a-zA-Z_][a-zA-Z0-9_]*)"?\s+to\s+"?([a-zA-Z_][a-zA-Z0-9_]*)"?/gi
)) {
set.delete(r[1]);
set.add(r[2]);
}
if (set.size) tables.set(name, set);
}
}
return tables;
}
// ------------------------------------------------------------ schema fields
// Same anchoring rule as check-dead-fields.mjs: a key opens a line or follows
// `{`/`,`, so it is a property and not a ternary branch. Anchoring on
// indentation instead misses every schema Prettier keeps on one line.
const FIELD_DECL = /(?:^|[{,])\s*([a-z][a-z0-9_]{2,})\s*:\s*[A-Za-z_$][\w$]*\s*[(.]/gm;
// blankNonCode moved to scripts/lib/blank-non-code.mjs — check-currency-units.mjs
// needs the identical parse, and a second hand-rolled copy is how the naive
// `//`-only version comes back.
/**
* Fields declared at the TOP level of one exported schema.
*
* Depth matters, and getting it wrong is what makes this kind of gate useless.
* `profileSchema` holds `social_links: z.object({ links: z.array(z.object({
* platform, label, value })) })` — one JSONB column whose inner names are not
* columns and never will be. A flat regex reports four phantom columns for it,
* and a gate with four false alarms out of nine findings gets switched off. So
* only depth-1 properties count; anything nested belongs to its parent column.
*/
function fieldsOfSchema(src, exportName) {
const start = src.indexOf(`export const ${exportName} =`);
if (start === -1) return null;
// Take until the next top-level `export const`, so sibling schemas in the
// same file cannot bleed their fields into this one.
const nextExport = src.indexOf('\nexport const ', start + 1);
let chunk = src.slice(start, nextExport === -1 ? undefined : nextExport);
chunk = blankNonCode(chunk);
const objStart = chunk.indexOf('{');
if (objStart === -1) return new Set();
const fields = new Set();
let depth = 0;
for (let i = objStart; i < chunk.length; i++) {
const ch = chunk[i];
if (ch === '{' || ch === '(' || ch === '[') {
depth++;
continue;
}
if (ch === '}' || ch === ')' || ch === ']') {
depth--;
continue;
}
// Depth 1 == a property of the schema's own object literal.
if (depth !== 1) continue;
// Match the NAME AND COLON ONLY, never the bracket that follows. An earlier
// version consumed the trailing `(` and skipped past it, so that bracket was
// never counted, depth drifted permanently, and everything after the second
// field silently fell out of scope — the schema reported 2 fields instead of
// 30 and the gate went quietly green. A parser that under-reports is worse
// than one that over-reports: it gives false confidence instead of noise.
const m = chunk.slice(i, i + 60).match(/^\s*([a-z][a-z0-9_]{2,})\s*:\s*(?=[A-Za-z_$])/);
if (m) {
fields.add(m[1]);
i += m[0].length - 1;
}
}
return fields;
}
function allSchemaFields() {
const out = new Map();
for (const file of readdirSync(VALIDATION).filter(f => f.endsWith('.ts'))) {
const src = readFileSync(join(VALIDATION, file), 'utf8');
for (const [exportName, table] of Object.entries(SCHEMA_TABLES)) {
const fields = fieldsOfSchema(src, exportName);
if (fields) out.set(exportName, { table, fields, file });
}
}
return out;
}
// ------------------------------------------------------------------ commands
const tables = buildSchema();
const schemas = allSchemaFields();
if (process.argv.includes('--verify')) {
// Prove the parser against the live database. Not part of `verify` — needs
// credentials and the network. Run it after touching the parsing above.
const env = {};
for (const line of readFileSync(join(ROOT, '../../../.env.local'), 'utf8').split('\n')) {
const mm = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/);
if (mm) env[mm[1]] = mm[2].replace(/^["']|["']$/g, '');
}
const url = env.NEXT_PUBLIC_SUPABASE_URL;
const key = env.SUPABASE_SERVICE_ROLE_KEY || env.SUPABASE_SECRET_KEY;
let mismatches = 0;
for (const table of new Set([...schemas.values()].map(s => s.table))) {
const parsed = tables.get(table);
if (!parsed) {
console.log(`${table}: NOT PARSED`);
mismatches++;
continue;
}
// Ask per column so this works on EMPTY tables — reading Object.keys of a
// row reports every column as missing when there are no rows.
const bad = [];
for (const col of parsed) {
const r = await fetch(`${url}/rest/v1/${table}?select=${col}&limit=0`, {
headers: { apikey: key, Authorization: `Bearer ${key}` },
});
if (!r.ok) bad.push(col);
}
if (bad.length) {
console.log(`${table}: parser claims ${bad.length} column(s) the DB lacks -> ${bad.join(', ')}`);
mismatches += bad.length;
} else {
console.log(`${table}: ${parsed.size} parsed columns all exist ✓`);
}
}
if (mismatches) {
console.log(
`\n${mismatches} mismatch(es).\n\n` +
` Expected when a migration is merged but not yet DEPLOYED — the parser\n` +
` reads supabase/migrations, the database reflects what has been applied,\n` +
` and apply-migrations.sh only runs on deploy. Re-run after the deploy\n` +
` lands. Anything still mismatched after that is a genuine parser bug:\n` +
` a column the parser invented, or a table it failed to reconstruct.`
);
} else {
console.log('\nparser matches the live database');
}
process.exit(mismatches ? 1 : 0);
}
const problems = [];
let checked = 0;
for (const [exportName, { table, fields, file }] of schemas) {
const cols = tables.get(table);
if (!cols) {
problems.push({ exportName, table, field: '(table not found in migrations)', file });
continue;
}
checked++;
for (const f of fields) {
if (!cols.has(f) && !NOT_COLUMNS.has(f)) problems.push({ exportName, table, field: f, file });
}
}
const skipped = Object.keys(SCHEMA_TABLES).length - schemas.size;
console.log(
`[check-schema-columns] schemas checked: ${checked}` +
(skipped > 0 ? ` — ${skipped} mapped schema(s) not found in source` : '') +
` — fields with no column: ${problems.length}`
);
if (problems.length > 0) {
console.error(
`\n[check-schema-columns] FAIL: ${problems.length} field(s) are collected but cannot be stored:\n` +
problems.map(p => ` ${p.table}.${p.field} (${p.exportName} in ${p.file})`).join('\n') +
`\n\n A write that includes one of these fails the ENTIRE payload, so the bug\n` +
` only appears when a user actually fills the field in.\n\n` +
` Fix by adding a migration under supabase/migrations/ (it is applied on\n` +
` deploy by scripts/apply-migrations.sh), or — if the field is genuinely\n` +
` not a column of this table — add it to NOT_COLUMNS in this file WITH the\n` +
` reason. Do not delete the check to make this pass.`
);
process.exit(1);
}
console.log('[check-schema-columns] OK — every collected field has a column.');