MC/website/node/migration-schema-contract.mjs
2026-11-27 23:43:05 +00:00

18 lines
1.1 KiB
JavaScript

// Read-only MySQL schema contract verification before any account migration.
import {ACCOUNT_MIGRATION_ALLOWLIST} from './minimal-account-migration.mjs';
export function compareMigrationSchema(actual){
const errors=[];
for(const [table,required] of Object.entries(ACCOUNT_MIGRATION_ALLOWLIST)){
const columns=actual[table];
if(!Array.isArray(columns)){errors.push(`Missing table ${table}`);continue;}
for(const column of required)if(!columns.includes(column))errors.push(`Missing ${table}.${column}`);
}
return {ok:errors.length===0,errors};
}
export async function inspectMigrationSchema(db){
const tables=Object.keys(ACCOUNT_MIGRATION_ALLOWLIST);
const placeholders=tables.map(()=>'?').join(',');
const {results}=await db.prepare(`SELECT TABLE_NAME,COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME IN (${placeholders}) ORDER BY TABLE_NAME,ORDINAL_POSITION`).bind(...tables).all();
const actual={};for(const row of results){(actual[row.TABLE_NAME]??=[]).push(row.COLUMN_NAME);}
return {actual,...compareMigrationSchema(actual)};
}