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:
ekko
2026-04-29 20:22:07 +08:00
committed by GitHub
parent 7053212d48
commit 6511033ed8
7 changed files with 284 additions and 153 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ export async function bootstrap() {
// Initialize all web-ui SQLite tables
const { initAllStores } = await import('./db/hermes/init')
await initAllStores()
initAllStores()
console.log('[bootstrap] all stores initialized')
// Sync Hermes sessions from all profiles (only if local DB is empty)