refactor(db): unify SQLite table schema management and initialization (#310)
Centralized all 11 Hermes SQLite table definitions and initialization logic into a single schemas.ts file to eliminate duplication and improve maintainability. Changes: - **NEW**: packages/server/src/db/hermes/schemas.ts - Centralized schema definitions for all 11 tables - Unified initAllHermesTables() function with migration logic - Includes usage table PRIMARY KEY migration (session_id → id) - **Refactored**: packages/server/src/db/hermes/init.ts - Simplified from async to sync (all operations are synchronous) - Single responsibility: delegate to schemas.ts - **Refactored**: packages/server/src/db/hermes/session-store.ts - Removed schema definitions (now in schemas.ts) - Removed initSessionStore() function - Imports table constants from schemas.ts - **Refactored**: packages/server/src/db/hermes/usage-store.ts - Removed initUsageStore() function and migration logic - Migration moved to schemas.ts for consistency - Only handles CRUD operations now - **Refactored**: packages/server/src/db/hermes/compression-snapshot.ts - Removed initCompressionSnapshotStore() function - Fixed duplicate getCompressionSnapshot definition - Imports table constant from schemas.ts - **Refactored**: packages/server/src/services/hermes/group-chat/index.ts - Removed ensureTable() calls (now in schemas.ts) - Only handles index creation now - Imports table constants from schemas.ts - **Updated**: packages/server/src/index.ts - Removed await from initAllStores() call (now sync) Benefits: - 🎯 Single responsibility: schemas.ts manages all tables, stores only do CRUD - 📋 Centralized maintenance: all table definitions in one place - 🔄 No duplication: each table created exactly once with proper migrations - 🚀 Clean architecture: clear separation between initialization and operations Tables managed (11 total): 1. session_usage (usage statistics) 2. sessions (session metadata) 3. messages (message content) 4. chat_compression_snapshots (compression snapshots) 5. gc_rooms (group chat rooms) 6. gc_messages (group chat messages) 7. gc_room_agents (room agents) 8. gc_context_snapshots (group chat snapshots) 9. gc_room_members (room members) 10. gc_pending_session_deletes (pending session deletes) 11. gc_session_profiles (session profiles) Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,8 @@ import { Server, Socket, Namespace } from 'socket.io'
|
||||
import type { Server as HttpServer } from 'http'
|
||||
import { getToken } from '../../../services/auth'
|
||||
import { logger } from '../../../services/logger'
|
||||
import { getDb, ensureTable } from '../../../db'
|
||||
import { getDb } from '../../../db'
|
||||
import { GC_ROOMS_TABLE, GC_MESSAGES_TABLE, GC_ROOM_AGENTS_TABLE, GC_CONTEXT_SNAPSHOTS_TABLE, GC_ROOM_MEMBERS_TABLE, GC_PENDING_SESSION_DELETES_TABLE, GC_SESSION_PROFILES_TABLE } from '../../../db/hermes/schemas'
|
||||
import { AgentClients } from './agent-clients'
|
||||
import { ContextEngine } from '../context-engine/compressor'
|
||||
import { SessionDeleter } from '../session-deleter'
|
||||
@@ -139,14 +140,8 @@ class ChatStorage {
|
||||
if (_tablesEnsured) return
|
||||
const db = this.db()
|
||||
if (!db) return
|
||||
ensureTable('gc_rooms', GC_ROOMS_SCHEMA)
|
||||
ensureTable('gc_messages', GC_MESSAGES_SCHEMA)
|
||||
ensureTable('gc_room_agents', GC_ROOM_AGENTS_SCHEMA)
|
||||
ensureTable('gc_context_snapshots', GC_CONTEXT_SNAPSHOTS_SCHEMA)
|
||||
ensureTable('gc_room_members', GC_ROOM_MEMBERS_SCHEMA)
|
||||
ensureTable('gc_pending_session_deletes', GC_PENDING_SESSION_DELETES_SCHEMA)
|
||||
ensureTable('gc_session_profiles', GC_SESSION_PROFILES_SCHEMA)
|
||||
// Indexes (safe to run multiple times — CREATE INDEX IF NOT EXISTS)
|
||||
// Tables are now created centrally in initAllHermesTables()
|
||||
// Only create indexes here
|
||||
try { db.exec('CREATE INDEX IF NOT EXISTS idx_gc_messages_room ON gc_messages(roomId, timestamp)') } catch { /* ignore */ }
|
||||
try { db.exec('CREATE INDEX IF NOT EXISTS idx_gc_room_agents_room ON gc_room_agents(roomId)') } catch { /* ignore */ }
|
||||
try { db.exec('CREATE UNIQUE INDEX IF NOT EXISTS idx_gc_room_members_unique ON gc_room_members(roomId, userId)') } catch { /* ignore */ }
|
||||
|
||||
Reference in New Issue
Block a user