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:
@@ -6,23 +6,8 @@
|
||||
* the previous one left off.
|
||||
*/
|
||||
|
||||
import { isSqliteAvailable, ensureTable, getDb } from '../index'
|
||||
|
||||
const TABLE = 'chat_compression_snapshots'
|
||||
|
||||
const SCHEMA: Record<string, string> = {
|
||||
session_id: 'TEXT PRIMARY KEY',
|
||||
summary: 'TEXT NOT NULL DEFAULT \'\'',
|
||||
last_message_index: 'INTEGER NOT NULL DEFAULT 0',
|
||||
message_count_at_time: 'INTEGER NOT NULL DEFAULT 0',
|
||||
updated_at: 'INTEGER NOT NULL',
|
||||
}
|
||||
|
||||
export function initCompressionSnapshotStore(): void {
|
||||
if (isSqliteAvailable()) {
|
||||
ensureTable(TABLE, SCHEMA)
|
||||
}
|
||||
}
|
||||
import { isSqliteAvailable, getDb } from '../index'
|
||||
import { COMPRESSION_SNAPSHOT_TABLE as TABLE } from './schemas'
|
||||
|
||||
export function getCompressionSnapshot(sessionId: string): { summary: string; lastMessageIndex: number; messageCountAtTime: number } | null {
|
||||
if (!isSqliteAvailable()) return null
|
||||
|
||||
Reference in New Issue
Block a user