forked from unimatrix/web
17 lines
1.6 KiB
JavaScript
17 lines
1.6 KiB
JavaScript
// Emit a fixed schema/least-privilege migration for the host DBA to apply.
|
|
// Uses the existing bank identity; never prints its password or connection URL.
|
|
import {readFile} from 'node:fs/promises';
|
|
import {openPostbankDatabase} from '../node/postbank-db.mjs';
|
|
import {openGameMysqlPool} from '../node/game-mysql-pool.mjs';
|
|
const settings=process.env,bank=await openPostbankDatabase(settings),pool=await openGameMysqlPool(settings.DUNE_INVENTORY_DATABASE_URL);
|
|
try{
|
|
const identitySQL='SELECT @@hostname host,@@port port,@@server_id server_id';
|
|
const [b,g]=await Promise.all([bank.first(identitySQL),pool.query(identitySQL)]);
|
|
if(!g.rows[0]||['host','port','server_id'].some(key=>String(b[key])!==String(g.rows[0][key])))throw Error('The bank and game databases must share their transaction server');
|
|
const schema=new URL(settings.DUNE_INVENTORY_DATABASE_URL).pathname.slice(1),user=(await bank.first('SELECT CURRENT_USER() identity')).identity.split('@');
|
|
if(!/^[a-z][a-z0-9_]{2,63}$/.test(schema)||user.length!==2||user.some(s=>!s||!/^[a-zA-Z0-9_.%:-]+$/.test(s)))throw Error('Unexpected database identity');
|
|
const ddl=await readFile(new URL('../integrations/game-exchange-schema.sql',import.meta.url),'utf8');
|
|
process.stdout.write('USE `'+schema+'`;\n'+ddl+'\n');
|
|
const account="'"+user[0]+"'@'"+user[1]+"'";
|
|
for(const [table,rights]of [['dune_exchange_listings','SELECT,UPDATE,DELETE'],['dune_exchange_sales','SELECT,INSERT'],['dune_exchange_deliveries','SELECT,INSERT']])process.stdout.write('GRANT '+rights+' ON `'+schema+'`.`'+table+'` TO '+account+';\n');
|
|
}finally{await bank?.close();await pool.end();}
|

Staff