use super::sql::{REGISTRY_TABLE, sql_process_store};
use crate::error::{KernelError, Result};
const MIGRATIONS: &[(i64, &str, &str)] = &[
(
1,
"create kernel process and spend tables",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_processes (\
pid TEXT PRIMARY KEY, \
parent TEXT, \
partition_id TEXT NOT NULL, \
queue TEXT NOT NULL, \
kind TEXT NOT NULL, \
wake TEXT, \
suspended_by TEXT, \
suspend_requested BOOLEAN NOT NULL DEFAULT FALSE, \
agenda_item TEXT, \
labels TEXT NOT NULL\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_processes_partition ON adk_kernel_processes (partition_id, pid);\
CREATE INDEX IF NOT EXISTS adk_kernel_processes_queue ON adk_kernel_processes (queue, pid);\
CREATE INDEX IF NOT EXISTS adk_kernel_processes_parent ON adk_kernel_processes (parent, pid);\
CREATE INDEX IF NOT EXISTS adk_kernel_processes_suspend ON adk_kernel_processes (suspend_requested, pid);\
CREATE TABLE IF NOT EXISTS adk_kernel_spend (\
pid TEXT NOT NULL, \
partition_id TEXT NOT NULL, \
day BIGINT NOT NULL, \
micro_usd BIGINT NOT NULL, \
PRIMARY KEY (pid, partition_id, day)\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_spend_partition ON adk_kernel_spend (partition_id, day);",
),
(
2,
"create kernel usage counters table",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_usage (\
pid TEXT NOT NULL, \
partition_id TEXT NOT NULL, \
day BIGINT NOT NULL, \
counter TEXT NOT NULL, \
amount BIGINT NOT NULL, \
PRIMARY KEY (pid, partition_id, day, counter)\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_usage_partition ON adk_kernel_usage (partition_id, day);",
),
];
#[derive(Debug, Clone)]
pub struct SqliteProcessStore {
pool: sqlx::SqlitePool,
}
impl SqliteProcessStore {
pub fn new(pool: sqlx::SqlitePool) -> Self {
Self { pool }
}
pub fn pool(&self) -> &sqlx::SqlitePool {
&self.pool
}
pub async fn migrate(&self) -> Result<()> {
adk_session::migration::sqlite_runner::run_sql_migrations(
&self.pool,
REGISTRY_TABLE,
MIGRATIONS,
|| async { Ok(false) },
)
.await
.map_err(|error| KernelError::Store { backend: "sqlite", reason: error.message })
}
}
sql_process_store!(SqliteProcessStore, "sqlite");