use std::sync::Arc;
use async_trait::async_trait;
use sqlx::{Row, SqlitePool};
use crate::traits::{
Goal, GoalSchedule, GoalTokenBudgetStatus, ScheduledRunState, Task, TaskActivity,
};
mod goals;
mod mandates;
mod notifications;
mod work;
pub trait WorkDatabase: Send + Sync {
fn work_pool(&self) -> &SqlitePool;
fn redact_secrets(&self, text: &str) -> String;
}
pub struct SqliteWorkStore {
pool: SqlitePool,
redact: Arc<dyn Fn(&str) -> String + Send + Sync>,
}
impl SqliteWorkStore {
pub fn new(pool: SqlitePool, redact: Arc<dyn Fn(&str) -> String + Send + Sync>) -> Self {
Self { pool, redact }
}
pub fn pool(&self) -> SqlitePool {
self.pool.clone()
}
}
impl WorkDatabase for SqliteWorkStore {
fn work_pool(&self) -> &SqlitePool {
&self.pool
}
fn redact_secrets(&self, text: &str) -> String {
(self.redact)(text)
}
}
#[cfg(any(test, feature = "testing"))]
pub async fn open_for_tests(path: &str) -> SqliteWorkStore {
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
let options = SqliteConnectOptions::new()
.filename(path)
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Delete)
.busy_timeout(std::time::Duration::from_secs(30));
let pool = SqlitePoolOptions::new()
.max_connections(10)
.acquire_timeout(std::time::Duration::from_secs(30))
.connect_with(options)
.await
.unwrap();
aidaemon_events::schema::migrate_events(&pool)
.await
.unwrap();
sqlx::query("CREATE TABLE episodes (id INTEGER PRIMARY KEY)")
.execute(&pool)
.await
.ok();
crate::schema::migrate_work_tables(&pool, |_| false)
.await
.unwrap();
crate::schema::migrate_work_tail(&pool).await.unwrap();
SqliteWorkStore::new(pool, Arc::new(|text: &str| text.to_string()))
}