#!/usr/bin/env node const fs = require('fs'); const path = require('path'); function walk(d, acc = []) { for (const e of fs.readdirSync(d, { withFileTypes: true })) { const p = path.join(d, e.name); if (e.isDirectory()) { if (['node_modules', 'dist'].includes(e.name)) continue; walk(p, acc); } else if (/\.(ts|js)$/.test(e.name)) acc.push(p); } return acc; } const STOP = new Set([ 'select', 'set', 'values', 'as', 'on', 'where', 'and', 'or', 'left', 'right', 'inner', 'outer', 'cross', 'lateral', 'true', 'false', 'null', 'now', 'case', 'when', 'then', 'else', 'end', 'limit', 'offset', 'group', 'order', 'having', 'union', 'all', 'distinct', 'insert', 'update', 'delete', 'returning', 'conflict', 'excluded', 'do', 'nothing', 'with', 'recursive', 'only', 'schema', 'information_schema', 'pg_catalog', 'auth', 'public', 'legacy_public', 'tenant_template', 'generate_series', 'unnest', 'coalesce', 'greatest', 'least', 'exists', 'any', 'some', 'in', 'not', 'is', 'between', 'like', 'ilike', 'current_date', 'current_timestamp', 'localtimestamp', 'interval', 'text', 'uuid', 'jsonb', 'json', 'boolean', 'integer', 'numeric', 'varchar', ]); const files = walk(path.join(__dirname, '..', 'server', 'src')); const tables = new Set(); const re = /\b(?:FROM|INTO|UPDATE|JOIN|TABLE)\s+(?:ONLY\s+)?(?:public\.|legacy_public\.)?([a-z][a-z0-9_]*)\b/gi; for (const f of files) { const text = fs.readFileSync(f, 'utf8'); let m; while ((m = re.exec(text))) { const name = m[1].toLowerCase(); if (STOP.has(name)) continue; if (name.startsWith('pg_')) continue; if (name.startsWith('tenant_')) continue; tables.add(name); } } const businessListMatch = fs .readFileSync( path.join(__dirname, '..', 'migrations', '20260776_tenant_missing_link_tables.sql'), 'utf8' ) .match(/SELECT ARRAY\[([\s\S]*?)\]::text\[\]/); const business = new Set(); if (businessListMatch) { for (const m of businessListMatch[1].matchAll(/'([a-z0-9_]+)'/g)) business.add(m[1]); } const platform = new Set([ 'branches', 'profiles', 'user_roles', 'permissions', 'role_permissions', 'role_definitions', 'schema_migrations', 'tenant_schema_registry', 'system_configs', ]); const usedNotInBusiness = [...tables] .filter((t) => !business.has(t) && !platform.has(t)) .sort(); console.log('=== server/src SQL tables NOT in tenant_business_tables (excl platform) ==='); for (const t of usedNotInBusiness) console.log(t); console.log('count', usedNotInBusiness.length);