adk_kernel/channels/sqlite.rs
1//! The SQLite [`ChannelBindingStore`](super::ChannelBindingStore) (spec 3.3 §10 K1; features
2//! `channels`, `sqlite`).
3
4use super::sql::{REGISTRY_TABLE, sql_channel_store};
5use crate::error::{KernelError, Result};
6
7/// Migrations of [`SqliteChannelBindingStore`], recorded in `_adk_kernel_channel_migrations`.
8const MIGRATIONS: &[(i64, &str, &str)] = &[(
9 1,
10 "create kernel channel binding and pairing-request tables",
11 "\
12CREATE TABLE IF NOT EXISTS adk_kernel_channel_bindings (\
13 id TEXT PRIMARY KEY, \
14 paired_us INTEGER NOT NULL, \
15 record TEXT NOT NULL\
16);\
17CREATE TABLE IF NOT EXISTS adk_kernel_channel_requests (\
18 channel TEXT NOT NULL, \
19 account TEXT NOT NULL, \
20 code_hash TEXT NOT NULL UNIQUE, \
21 requested_us INTEGER NOT NULL, \
22 expires_us INTEGER NOT NULL, \
23 record TEXT NOT NULL, \
24 PRIMARY KEY (channel, account)\
25);\
26CREATE INDEX IF NOT EXISTS adk_kernel_channel_requests_expiry ON adk_kernel_channel_requests (expires_us);",
27)];
28
29/// A [`ChannelBindingStore`](super::ChannelBindingStore) in SQLite (spec 3.3 §10 K1,
30/// features `channels`, `sqlite`).
31///
32/// In the CLI it lives in `harness.db` on the same pool as the harness and process stores:
33/// its tables are `adk_kernel_channel_bindings` and `adk_kernel_channel_requests`, and
34/// [`migrate`](Self::migrate) records its steps in `_adk_kernel_channel_migrations`. Call
35/// `migrate` once before use.
36///
37/// # Example
38///
39/// ```rust
40/// use adk_kernel::channels::{ChannelBinding, ChannelBindingStore, SqliteChannelBindingStore};
41///
42/// # #[tokio::main(flavor = "current_thread")]
43/// # async fn main() -> adk_kernel::Result<()> {
44/// let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
45/// let store = SqliteChannelBindingStore::new(pool);
46/// store.migrate().await?;
47/// let binding = ChannelBinding::new("telegram", "4217", "4217", "ana", chrono::Utc::now());
48/// store.put_binding(binding.clone()).await?;
49/// assert_eq!(store.list_bindings().await?, [binding]);
50/// # Ok(())
51/// # }
52/// ```
53#[derive(Debug, Clone)]
54pub struct SqliteChannelBindingStore {
55 pool: sqlx::SqlitePool,
56}
57
58impl SqliteChannelBindingStore {
59 /// A store over `pool`, such as the pool of `harness.db`.
60 pub fn new(pool: sqlx::SqlitePool) -> Self {
61 Self { pool }
62 }
63
64 /// The pool the store writes to.
65 pub fn pool(&self) -> &sqlx::SqlitePool {
66 &self.pool
67 }
68
69 /// Creates or upgrades the store's tables. Idempotent.
70 ///
71 /// # Errors
72 ///
73 /// Returns [`KernelError::Store`] when a statement fails, or when the database was
74 /// migrated by a newer release.
75 pub async fn migrate(&self) -> Result<()> {
76 adk_session::migration::sqlite_runner::run_sql_migrations(
77 &self.pool,
78 REGISTRY_TABLE,
79 MIGRATIONS,
80 || async { Ok(false) },
81 )
82 .await
83 .map_err(|error| KernelError::Store { backend: "sqlite", reason: error.message })
84 }
85}
86
87sql_channel_store!(SqliteChannelBindingStore, "sqlite");