import {pathToFileURL} from 'node:url'; import {mkdir,writeFile} from 'node:fs/promises'; import {createHash} from 'node:crypto'; export function kitRoutineWithEquipment(source){ if(typeof source!=='string'||!source.includes('READS SQL DATA'))throw Error('Expected a read-only native kit routine.'); if(source.includes('AS kit_equipment_json')){ if(!source.includes('LEFT JOIN website.site_kit_equipment ke ON ke.kit_id=b.id AND ke.blueprint_version=b.version'))throw Error('Unexpected partially modified kit routine.'); return source; } const select='b.lore_json AS kit_lore_json',join='JOIN website.site_accounts a ON a.id=b.owner_user_id'; if(source.split(select).length!==2||source.split(join).length!==2)throw Error('Native kit routine changed; no SQL replaced.'); return source.replace(select,select+',ke.equipment_json AS kit_equipment_json,b.item_names_json AS kit_item_names_json,b.upgrade_json AS kit_upgrade_json') .replace(join,'LEFT JOIN website.site_kit_equipment ke ON ke.kit_id=b.id AND ke.blueprint_version=b.version '+join); } async function main(apply){ const mysql=(await import('mysql2/promise')).default; const connection=await mysql.createConnection({user:'root',socketPath:'/var/run/mysqld/mysqld.sock'}); try{ const plans=[]; for(const name of ['dune_kit_catalogue','dune_kit_listing_lock']){ const [rows]=await connection.query('SHOW CREATE PROCEDURE dunegameplay.'+name); const before=rows[0]?.['Create Procedure'];plans.push({name,before,after:kitRoutineWithEquipment(before)}); } if(!apply){console.log('Plan: version-bound equipment metadata in '+plans.filter(p=>p.before!==p.after).length+' read-only kit routines.');return;} await connection.query('CREATE TABLE IF NOT EXISTS website.site_kit_equipment (kit_id CHAR(36) PRIMARY KEY,blueprint_version INT NOT NULL,equipment_json JSON NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4'); for(const plan of plans){ if(plan.before===plan.after)continue; const directory='/mnt/HC_Volume_107481500/dunegit/migration-backups';await mkdir(directory,{recursive:true,mode:0o700}); const hash=createHash('sha256').update(plan.before).digest('hex').slice(0,16); try{await writeFile(directory+'/'+plan.name+'-'+hash+'.sql',plan.before,{mode:0o600,flag:'wx'});}catch(error){if(error.code!=='EEXIST')throw error;} await connection.query('USE dunegameplay');await connection.query('DROP PROCEDURE '+plan.name); try{await connection.query(plan.after);}catch(error){await connection.query(plan.before);throw error;} console.log('Installed versioned equipment metadata in '+plan.name); } }finally{await connection.end();} } if(process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href)void main(process.argv.includes('--apply')).catch(error=>{console.error('Native kit routine installation failed.',error.code||error.name);process.exitCode=1;});