MC/website/node/minimal-account-import.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

40 lines
3 KiB
JavaScript

// Explicit, opt-in account-only import into an already initialized EMPTY database.
// Never drops, truncates or clears tables. Keep full source backups separately.
import {ACCOUNT_MIGRATION_ALLOWLIST,validateMinimalAccountSnapshot} from './minimal-account-migration.mjs';
import {inspectMigrationSchema} from './migration-schema-contract.mjs';
const ORDER=Object.freeze(['site_accounts','site_contact_emails','site_game_links','cp_account_ranks','website_badges','seal_entitlements']);
export function accountImportStatements(snapshot){
validateMinimalAccountSnapshot(snapshot);
return ORDER.flatMap(table=>snapshot[table].map(row=>{
const columns=ACCOUNT_MIGRATION_ALLOWLIST[table];
if(columns.some(column=>!Object.hasOwn(row,column)))throw new Error(`Missing required migration field in ${table}`);
return {table,sql:`INSERT INTO \`${table}\` (${columns.map(c=>`\`${c}\``).join(',')}) VALUES (${columns.map(()=>'?').join(',')})`,values:columns.map(c=>row[c])};
}));
}
// Compare actual imported field values, not just counts. Order-independent and strict.
export function compareImportedAccounts(expected,actual){
validateMinimalAccountSnapshot(expected);validateMinimalAccountSnapshot(actual);
for(const table of ORDER){
const columns=ACCOUNT_MIGRATION_ALLOWLIST[table];
const normalize=rows=>rows.map(row=>JSON.stringify(columns.map(column=>{const value=row[column];return value===null?null:String(value);}))).sort();
const left=normalize(expected[table]),right=normalize(actual[table]);
if(left.length!==right.length||left.some((value,index)=>value!==right[index]))throw new Error('Imported data mismatch: '+table);
}
return true;
}
export async function importMinimalAccounts(db,snapshot,{confirmedEmptyTarget=false}={}){
if(confirmedEmptyTarget!==true)throw new Error('Explicit empty-target confirmation required');
if(typeof db.transaction!=='function')throw new Error('Transactional database required');
const plan=accountImportStatements(snapshot);
const schema=await inspectMigrationSchema(db);if(!schema.ok)throw new Error('Target schema mismatch: '+schema.errors.join('; '));
return db.transaction(async tx=>{
// Locks are unnecessary on a dedicated, isolated target. The target must not serve traffic.
for(const table of ORDER){const result=await tx.first(`SELECT COUNT(*) AS count FROM \`${table}\``);if(BigInt(result?.count??-1)!==0n)throw new Error('Target table not empty: '+table);}
for(const step of plan)await tx.query(step.sql,step.values);
for(const table of ORDER){const expected=BigInt(snapshot[table].length);const actual=await tx.first(`SELECT COUNT(*) AS count FROM \`${table}\``);if(BigInt(actual?.count??-1)!==expected)throw new Error('Imported count mismatch: '+table);}
const actual={};
for(const table of ORDER){const columns=ACCOUNT_MIGRATION_ALLOWLIST[table];const sql=`SELECT ${columns.map(c=>`\`${c}\``).join(',')} FROM \`${table}\``;actual[table]=await tx.query(sql);}
compareImportedAccounts(snapshot,actual);
return validateMinimalAccountSnapshot(snapshot);
});
}