use super::sql::{REGISTRY_TABLE, sql_control_store};
use crate::error::{KernelError, Result};
const MIGRATIONS: &[(i64, &str, &str)] = &[(
1,
"create kernel area settings and away cursor tables",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_area_settings (\
partition_id TEXT COLLATE \"C\" PRIMARY KEY, \
record TEXT NOT NULL\
);\
CREATE TABLE IF NOT EXISTS adk_kernel_away_cursors (\
user_id TEXT COLLATE \"C\" NOT NULL, \
viewer TEXT COLLATE \"C\" NOT NULL, \
seq BIGINT NOT NULL, \
record TEXT NOT NULL, \
PRIMARY KEY (user_id, viewer)\
);",
)];
#[derive(Debug, Clone)]
pub struct PostgresControlStore {
pool: sqlx::PgPool,
}
impl PostgresControlStore {
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_control_store!(PostgresControlStore, "postgres");