use codex_utils_absolute_path::AbsolutePathBuf;
use log::LevelFilter;
use sqlx::ConnectOptions;
use sqlx::Error;
use sqlx::SqlitePool;
use sqlx::sqlite::SqliteAutoVacuum;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::sqlite::SqliteJournalMode;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::sqlite::SqliteSynchronous;
use std::path::Path;
use std::time::Duration;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SqliteConfig {
sqlite_home: AbsolutePathBuf,
}
impl SqliteConfig {
pub fn from_sqlite_home(sqlite_home: AbsolutePathBuf) -> Self {
Self { sqlite_home }
}
pub fn new_for_testing(sqlite_home: AbsolutePathBuf) -> Self {
Self::from_sqlite_home(sqlite_home)
}
pub fn home(&self) -> &Path {
self.sqlite_home.as_path()
}
pub async fn open_read_write_pool(&self, path: &Path) -> Result<SqlitePool, Error> {
let options = SqliteConnectOptions::new()
.filename(path)
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal)
.synchronous(SqliteSynchronous::Normal)
.auto_vacuum(SqliteAutoVacuum::Incremental)
.busy_timeout(Duration::from_secs(5))
.log_statements(LevelFilter::Off);
SqlitePoolOptions::new()
.max_connections(5)
.connect_with(options)
.await
}
pub async fn open_read_only_pool(&self, path: &Path) -> Result<SqlitePool, Error> {
let options = SqliteConnectOptions::new()
.filename(path)
.create_if_missing(false)
.read_only(true)
.log_statements(LevelFilter::Off);
SqlitePoolOptions::new()
.max_connections(1)
.connect_with(options)
.await
}
}