aidaemon_memory/store/
migrations.rs1use sqlx::SqlitePool;
10
11use crate::error::{MemoryError, Result};
12
13pub use aidaemon_store_sqlite::Migration;
15
16pub const MIGRATIONS: &[Migration] = &[
18 Migration {
19 version: 1,
20 name: "facts_episodes_fts",
21 sql: r#"
22CREATE TABLE memory_facts (
23 id TEXT PRIMARY KEY NOT NULL,
24 scope TEXT NOT NULL,
25 category TEXT NOT NULL,
26 key TEXT NOT NULL,
27 value TEXT NOT NULL,
28 confidence REAL NOT NULL,
29 source_run_id TEXT,
30 source_session_id TEXT,
31 source_excerpt TEXT,
32 created_at TEXT NOT NULL,
33 updated_at TEXT NOT NULL,
34 superseded_at TEXT,
35 recall_count INTEGER NOT NULL DEFAULT 0,
36 last_recalled_at TEXT,
37 embedding BLOB,
38 embedding_model TEXT
39);
40
41-- At most one active row per key; superseded rows are history.
42CREATE UNIQUE INDEX idx_memory_facts_active
43 ON memory_facts (scope, category, key) WHERE superseded_at IS NULL;
44CREATE INDEX idx_memory_facts_history ON memory_facts (scope, category, key, created_at);
45CREATE INDEX idx_memory_facts_superseded ON memory_facts (superseded_at)
46 WHERE superseded_at IS NOT NULL;
47
48-- Lexical index over ACTIVE facts only, maintained in the same transaction
49-- as every write.
50CREATE VIRTUAL TABLE memory_facts_fts USING fts5(
51 fact_id UNINDEXED,
52 scope UNINDEXED,
53 content,
54 tokenize = 'unicode61'
55);
56
57CREATE TABLE memory_episodes (
58 id TEXT PRIMARY KEY NOT NULL,
59 scope TEXT NOT NULL,
60 session_id TEXT NOT NULL,
61 summary TEXT NOT NULL,
62 topics TEXT NOT NULL CHECK (json_valid(topics)),
63 importance REAL NOT NULL,
64 created_at TEXT NOT NULL,
65 recall_count INTEGER NOT NULL DEFAULT 0,
66 last_recalled_at TEXT,
67 embedding BLOB,
68 embedding_model TEXT
69);
70
71CREATE INDEX idx_memory_episodes_scope ON memory_episodes (scope, created_at);
72
73CREATE VIRTUAL TABLE memory_episodes_fts USING fts5(
74 episode_id UNINDEXED,
75 scope UNINDEXED,
76 content,
77 tokenize = 'unicode61'
78);
79"#,
80 },
81 Migration {
82 version: 2,
83 name: "record_access",
84 sql: r#"
87ALTER TABLE memory_facts ADD COLUMN privacy TEXT NOT NULL DEFAULT 'private';
88ALTER TABLE memory_facts ADD COLUMN channel_id TEXT;
89ALTER TABLE memory_episodes ADD COLUMN privacy TEXT NOT NULL DEFAULT 'private';
90ALTER TABLE memory_episodes ADD COLUMN channel_id TEXT;
91"#,
92 },
93];
94
95pub fn latest_version() -> i64 {
97 MIGRATIONS.last().map(|m| m.version).unwrap_or(0)
98}
99
100pub(crate) async fn current_version(pool: &SqlitePool) -> Result<i64> {
101 Ok(aidaemon_store_sqlite::recorded_version(pool, VERSION_TABLE).await?)
102}
103
104const VERSION_TABLE: &str = "memory_schema_migrations";
106
107pub(crate) async fn run(pool: &SqlitePool) -> Result<()> {
109 aidaemon_store_sqlite::apply_migrations(pool, VERSION_TABLE, MIGRATIONS)
110 .await
111 .map_err(|error| match error {
112 aidaemon_store_sqlite::MigrateError::Sqlx(error) => MemoryError::Sqlx(error),
113 aidaemon_store_sqlite::MigrateError::SchemaTooNew { found, supported } => {
114 MemoryError::SchemaTooNew { found, supported }
115 }
116 aidaemon_store_sqlite::MigrateError::Step {
117 version,
118 name,
119 source,
120 } => MemoryError::Migration {
121 version,
122 name,
123 source,
124 },
125 })
126}