use super::sql::{REGISTRY_TABLE, sql_channel_store};
use crate::error::{KernelError, Result};
const MIGRATIONS: &[(i64, &str, &str)] = &[(
1,
"create kernel channel binding and pairing-request tables",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_channel_bindings (\
id TEXT COLLATE \"C\" PRIMARY KEY, \
paired_us BIGINT NOT NULL, \
record TEXT NOT NULL\
);\
CREATE TABLE IF NOT EXISTS adk_kernel_channel_requests (\
channel TEXT COLLATE \"C\" NOT NULL, \
account TEXT COLLATE \"C\" NOT NULL, \
code_hash TEXT NOT NULL UNIQUE, \
requested_us BIGINT NOT NULL, \
expires_us BIGINT NOT NULL, \
record TEXT NOT NULL, \
PRIMARY KEY (channel, account)\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_channel_requests_expiry ON adk_kernel_channel_requests (expires_us);",
)];
#[derive(Debug, Clone)]
pub struct PostgresChannelBindingStore {
pool: sqlx::PgPool,
}
impl PostgresChannelBindingStore {
pub fn new(pool: sqlx::PgPool) -> Self {
Self { pool }
}
pub fn pool(&self) -> &sqlx::PgPool {
&self.pool
}
pub async fn connect(database_url: &str) -> Result<Self> {
let pool =
sqlx::PgPool::connect(database_url).await.map_err(|error| KernelError::Store {
backend: "postgres",
reason: format!("could not connect: {error}"),
})?;
Ok(Self::new(pool))
}
pub async fn migrate(&self) -> Result<()> {
crate::pairing::postgres::migrate_locked(self.pool.clone(), REGISTRY_TABLE, MIGRATIONS)
.await
}
}
sql_channel_store!(PostgresChannelBindingStore, "postgres");