diff --git a/src/context.rs b/src/context.rs index 1ca193d90..9f7ae43d3 100644 --- a/src/context.rs +++ b/src/context.rs @@ -423,6 +423,9 @@ impl Context { /// Stops the IO scheduler. pub async fn stop_io(&self) { self.scheduler.stop(self).await; + if let Err(err) = self.sql.checkpoint(self).await { + error!(self, "Failed to checkpoint the database: {err:#}."); + } } /// Restarts the IO scheduler if it was running before diff --git a/src/sql.rs b/src/sql.rs index 82199c1d3..1f2801e2e 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -131,6 +131,23 @@ impl Sql { // drop closes the connection } + /// Flushes the WAL file. + pub(crate) async fn checkpoint(&self, context: &Context) -> Result<()> { + let busy = self + .call_write(move |conn| { + let busy = conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", (), |row| { + let busy: bool = row.get(0)?; + Ok(busy) + })?; + Ok(busy) + }) + .await?; + if busy { + warn!(context, "Failed to checkpoint WAL"); + } + Ok(()) + } + /// Imports the database from a separate file with the given passphrase. pub(crate) async fn import(&self, path: &Path, passphrase: String) -> Result<()> { let path_str = path