use sqlx::SqlitePool;
use crate::error::ClawResult;
pub async fn configure_wal(pool: &SqlitePool, checkpoint_pages: u32) -> ClawResult<()> {
sqlx::query("PRAGMA journal_mode = WAL")
.execute(pool)
.await?;
sqlx::query(&format!("PRAGMA wal_autocheckpoint = {checkpoint_pages}"))
.execute(pool)
.await?;
tracing::debug!(checkpoint_pages, "WAL configured");
Ok(())
}
pub async fn checkpoint(pool: &SqlitePool) -> ClawResult<()> {
sqlx::query("PRAGMA wal_checkpoint(PASSIVE)")
.execute(pool)
.await?;
tracing::debug!("WAL checkpoint completed");
Ok(())
}
pub async fn journal_mode(pool: &SqlitePool) -> ClawResult<String> {
let row: (String,) = sqlx::query_as("PRAGMA journal_mode")
.fetch_one(pool)
.await?;
Ok(row.0)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CheckpointMode {
Passive,
Full,
Restart,
Truncate,
}
impl CheckpointMode {
pub fn as_str(self) -> &'static str {
match self {
CheckpointMode::Passive => "PASSIVE",
CheckpointMode::Full => "FULL",
CheckpointMode::Restart => "RESTART",
CheckpointMode::Truncate => "TRUNCATE",
}
}
}
#[derive(Debug, Clone)]
pub struct CheckpointResult {
pub mode: CheckpointMode,
pub success: bool,
}