adk_kernel/stores/
sqlite.rs1use super::sql::{REGISTRY_TABLE, sql_process_store};
4use crate::error::{KernelError, Result};
5
6const MIGRATIONS: &[(i64, &str, &str)] = &[
8 (
9 1,
10 "create kernel process and spend tables",
11 "\
12CREATE TABLE IF NOT EXISTS adk_kernel_processes (\
13 pid TEXT PRIMARY KEY, \
14 parent TEXT, \
15 partition_id TEXT NOT NULL, \
16 queue TEXT NOT NULL, \
17 kind TEXT NOT NULL, \
18 wake TEXT, \
19 suspended_by TEXT, \
20 suspend_requested BOOLEAN NOT NULL DEFAULT FALSE, \
21 agenda_item TEXT, \
22 labels TEXT NOT NULL\
23);\
24CREATE INDEX IF NOT EXISTS adk_kernel_processes_partition ON adk_kernel_processes (partition_id, pid);\
25CREATE INDEX IF NOT EXISTS adk_kernel_processes_queue ON adk_kernel_processes (queue, pid);\
26CREATE INDEX IF NOT EXISTS adk_kernel_processes_parent ON adk_kernel_processes (parent, pid);\
27CREATE INDEX IF NOT EXISTS adk_kernel_processes_suspend ON adk_kernel_processes (suspend_requested, pid);\
28CREATE TABLE IF NOT EXISTS adk_kernel_spend (\
29 pid TEXT NOT NULL, \
30 partition_id TEXT NOT NULL, \
31 day BIGINT NOT NULL, \
32 micro_usd BIGINT NOT NULL, \
33 PRIMARY KEY (pid, partition_id, day)\
34);\
35CREATE INDEX IF NOT EXISTS adk_kernel_spend_partition ON adk_kernel_spend (partition_id, day);",
36 ),
37 (
38 2,
39 "create kernel usage counters table",
40 "\
41CREATE TABLE IF NOT EXISTS adk_kernel_usage (\
42 pid TEXT NOT NULL, \
43 partition_id TEXT NOT NULL, \
44 day BIGINT NOT NULL, \
45 counter TEXT NOT NULL, \
46 amount BIGINT NOT NULL, \
47 PRIMARY KEY (pid, partition_id, day, counter)\
48);\
49CREATE INDEX IF NOT EXISTS adk_kernel_usage_partition ON adk_kernel_usage (partition_id, day);",
50 ),
51];
52
53#[derive(Debug, Clone)]
82pub struct SqliteProcessStore {
83 pool: sqlx::SqlitePool,
84}
85
86impl SqliteProcessStore {
87 pub fn new(pool: sqlx::SqlitePool) -> Self {
89 Self { pool }
90 }
91
92 pub fn pool(&self) -> &sqlx::SqlitePool {
94 &self.pool
95 }
96
97 pub async fn migrate(&self) -> Result<()> {
107 adk_session::migration::sqlite_runner::run_sql_migrations(
108 &self.pool,
109 REGISTRY_TABLE,
110 MIGRATIONS,
111 || async { Ok(false) },
112 )
113 .await
114 .map_err(|error| KernelError::Store { backend: "sqlite", reason: error.message })
115 }
116}
117
118sql_process_store!(SqliteProcessStore, "sqlite");