Support a separate database for Web UI tables

This commit is contained in:
bryanpedini committed 2026-11-21 20:36:03 +01:00
1 parent a2f8e59d9b
commit f464dc0af9
38 files changed
+880 -302

No files matched your search

+22 -12
View File
@@ -63,6 +63,10 @@ export interface SessionRow {
export type RawRecord = { zone: string; name: string; ttl: number | null; content: string | null; record_type: string };
const INIT_TEMPLATE = path.resolve(process.cwd(), 'stack', 'mysql-init.sql');
const WEBUI_GRANT_TEMPLATES = {
tables: path.resolve(process.cwd(), 'stack', 'mysql-webui-tables.sql'),
database: path.resolve(process.cwd(), 'stack', 'mysql-webui-database.sql'),
} as const;
function connectionOptions(database?: string): mysql.ConnectionOptions {
return {
@@ -85,11 +89,17 @@ function q(identifier: string): string {
return '`' + identifier + '`';
}
function qualified(schema: string, table: string): string {
return `${q(schema)}.${q(table)}`;
}
export function initSql(g: GroupConfig): string {
const template = fs.readFileSync(INIT_TEMPLATE, 'utf8');
const template = fs.readFileSync(INIT_TEMPLATE, 'utf8') + '\n' + fs.readFileSync(WEBUI_GRANT_TEMPLATES[g.webuiGrants], 'utf8');
const values: Record<string, string> = {
DATABASE: g.database,
PREFIX: g.prefix,
WEBUI_DATABASE: g.webuiDatabase,
WEBUI_PREFIX: g.webuiPrefix,
DNS_USER: g.dnsUser,
DNS_PASSWORD: g.dnsPassword,
APP_USER: g.appUser,
@@ -101,7 +111,7 @@ export function initSql(g: GroupConfig): string {
.join('\n')
.replace(/\{\{([A-Z_]+)\}\}/g, (_m, k: string) => {
const v = values[k];
if (v === undefined || !/^[A-Za-z0-9_-]+$/.test(v)) throw new HarnessError('infrastructure', `bad template value ${k}`);
if (v === undefined || !/^[A-Za-z0-9_-]*$/.test(v) || (v === '' && !k.endsWith('PREFIX'))) throw new HarnessError('infrastructure', `bad template value ${k}`);
return v;
});
}
@@ -140,14 +150,14 @@ export class Db {
constructor(readonly group: GroupConfig) {
this.pool = mysql.createPool({ ...connectionOptions(group.database), connectionLimit: 4 });
const p = group.prefix;
const ui = (table: string) => qualified(group.webuiDatabase, `${group.webuiPrefix}webui_${table}`);
this.t = {
records: q(`${p}records`),
users: q(`${p}webui_users`),
sessions: q(`${p}webui_sessions`),
delegations: q(`${p}webui_zone_delegations`),
audit: q(`${p}webui_audit`),
schema: q(`${p}webui_schema`),
records: qualified(group.database, `${group.prefix}records`),
users: ui('users'),
sessions: ui('sessions'),
delegations: ui('zone_delegations'),
audit: ui('audit'),
schema: ui('schema'),
};
}
@@ -289,9 +299,9 @@ export class Db {
return Number(rows[0].n);
}
async tableExists(name: string): Promise<boolean> {
const rows = await this.query<{ n: number }>('SELECT COUNT(*) AS n FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?', [name]);
return Number(rows[0].n) === 1;
async tables(schema: string): Promise<string[]> {
const rows = await this.query<{ name: string }>('SELECT TABLE_NAME AS name FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME', [schema]);
return rows.map((r) => r.name);
}
async snapshot(): Promise<string> {