Skip to content

Commit 9e1dbbf

Browse files
committed
Add guilds table to schema and seed from config on startup
1 parent 3f156ef commit 9e1dbbf

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/bot/start.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { registerCommands } from './commands'
44
import { registerGlobalHandlers } from './handlers/handlers'
55
import { setupBotCommands } from './setup'
66
import { initDb, runMigrations, takeDailySnapshot, sql } from '../db'
7+
import { seedGuilds } from '../db/migrate'
78

89
const BUILD_TIME = new Date().toLocaleString('en-GB', {
910
timeZone: 'Europe/Helsinki',
@@ -52,6 +53,7 @@ export async function startBot(): Promise<void> {
5253
// Step 2: Run migrations
5354
console.log('📊 Running migrations...')
5455
await runMigrations()
56+
await seedGuilds()
5557
console.log('✅ Migrations complete\n')
5658

5759
// Step 2.5: Backfill snapshots if sparse, then schedule daily at midnight

src/db/migrate.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { sql } from './index'
22
import { readFileSync } from 'fs'
33
import { join } from 'path'
4+
import { GUILDS } from '../config/guilds'
45

56
export async function runMigrations() {
67
try {
@@ -22,6 +23,18 @@ export async function runMigrations() {
2223
}
2324
}
2425

26+
export async function seedGuilds() {
27+
for (const guild of GUILDS) {
28+
await sql`
29+
INSERT INTO guilds (name, total_members, is_active)
30+
VALUES (${guild.name}, ${guild.totalMembers}, ${guild.isActive})
31+
ON CONFLICT (name) DO UPDATE
32+
SET total_members = EXCLUDED.total_members,
33+
is_active = EXCLUDED.is_active
34+
`
35+
}
36+
}
37+
2538
// Actually run the migration when this file is executed
2639
if (import.meta.main) {
2740
runMigrations()

src/db/schema.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ CREATE TABLE IF NOT EXISTS feedback (
3434
reviewed BOOLEAN DEFAULT FALSE
3535
);
3636

37+
-- Guilds table (seeded from src/config/guilds.ts on startup)
38+
CREATE TABLE IF NOT EXISTS guilds (
39+
id SERIAL PRIMARY KEY,
40+
name VARCHAR(100) UNIQUE NOT NULL,
41+
total_members INTEGER NOT NULL DEFAULT 0,
42+
is_active BOOLEAN DEFAULT TRUE
43+
);
44+
3745
-- Historical Snapshots for Performance
3846
CREATE TABLE IF NOT EXISTS user_daily_snapshots (
3947
date DATE NOT NULL,

0 commit comments

Comments
 (0)