// Minimal account-only export plan. Read-only SQL, explicit columns, no wildcard exports. import {ACCOUNT_MIGRATION_ALLOWLIST,validateMinimalAccountSnapshot} from './minimal-account-migration.mjs'; import {inspectMigrationSchema} from './migration-schema-contract.mjs'; const IDENTIFIER=/^[a-z][a-z0-9_]*$/; export function accountExportQueries(){ return Object.entries(ACCOUNT_MIGRATION_ALLOWLIST).map(([table,columns])=>{ if(!IDENTIFIER.test(table)||columns.some(column=>!IDENTIFIER.test(column)))throw new Error('Unsafe export identifier'); return Object.freeze({table,sql:`SELECT ${columns.map(c=>`\`${c}\``).join(', ')} FROM \`${table}\` ORDER BY ${table==='site_accounts'?'id':table==='seal_entitlements'?'id':'user_id'}`}); }); } // Requires a read-only connection to a stable database snapshot or maintenance-mode source. // This function never writes, truncates, or drops any table. export async function readMinimalAccountExport(db){ const schema=await inspectMigrationSchema(db); if(!schema.ok)throw new Error('Account migration schema mismatch: '+schema.errors.join('; ')); if(typeof db.readSnapshot!=='function')throw new Error('Consistent read-only snapshot required'); return db.readSnapshot(async reader=>{ const snapshot={}; for(const {table,sql} of accountExportQueries()){const {results}=await reader.prepare(sql).all();snapshot[table]=results;} const summary=validateMinimalAccountSnapshot(snapshot); return {snapshot,summary}; }); }