use super::sql::{REGISTRY_TABLE, sql_device_store};
use crate::error::{KernelError, Result};
const MIGRATIONS: &[(i64, &str, &str)] = &[
(
1,
"create kernel device and pairing-code tables",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_devices (\
id TEXT PRIMARY KEY, \
created_us INTEGER NOT NULL, \
record TEXT NOT NULL\
);\
CREATE TABLE IF NOT EXISTS adk_kernel_pairing_codes (\
code_hash TEXT PRIMARY KEY, \
expires_us INTEGER NOT NULL, \
record TEXT NOT NULL\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_pairing_codes_expiry ON adk_kernel_pairing_codes (expires_us);",
),
(
2,
"create the shared count of pairing claims",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_pairing_attempts (\
id INTEGER PRIMARY KEY, \
attempts INTEGER NOT NULL\
);",
),
(
3,
"create device attachments",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_device_attachments (\
holder TEXT NOT NULL, \
kind TEXT NOT NULL, \
key TEXT NOT NULL, \
updated_us INTEGER NOT NULL, \
record TEXT NOT NULL, \
PRIMARY KEY (holder, kind, key)\
);",
),
];
#[derive(Debug, Clone)]
pub struct SqliteDeviceStore {
pool: sqlx::SqlitePool,
}
impl SqliteDeviceStore {
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_device_store!(SqliteDeviceStore, "sqlite");