15 lines
1014 B
TypeScript
15 lines
1014 B
TypeScript
import type Database from 'better-sqlite3';
|
|
|
|
export function installReportRevisions(handle: Database.Database) {
|
|
handle.exec(`CREATE TABLE IF NOT EXISTS report_revisions (topic TEXT PRIMARY KEY, revision INTEGER NOT NULL DEFAULT 0);
|
|
INSERT OR IGNORE INTO report_revisions(topic) VALUES ('metrics'), ('leaderboard');`);
|
|
for (const [topic, tables] of Object.entries({ metrics: ['metric_samples', 'servers'],
|
|
leaderboard: ['leaderboard_matches', 'leaderboard_match_players', 'leaderboard_players'] })) {
|
|
for (const table of tables) for (const action of ['INSERT', 'UPDATE', 'DELETE']) {
|
|
const condition = table === 'servers' && action === 'UPDATE' ? 'WHEN OLD.destroyed_at IS NOT NEW.destroyed_at OR OLD.name IS NOT NEW.name' : '';
|
|
handle.exec(`CREATE TRIGGER IF NOT EXISTS report_${table}_${action} AFTER ${action} ON ${table} ${condition}
|
|
BEGIN UPDATE report_revisions SET revision=revision+1 WHERE topic='${topic}'; END;`);
|
|
}
|
|
}
|
|
}
|