From 84a16fee3eff3306f7a4572e357e535d2551f9d1 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Wed, 12 Jun 2024 23:19:17 -0600 Subject: [PATCH] sqlite: add vacuum command I normally do this with the SQLite command line tool, but thats not always available. --- src/commands/sqlite/mod.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/commands/sqlite/mod.rs b/src/commands/sqlite/mod.rs index 22f1baec..b23e203b 100644 --- a/src/commands/sqlite/mod.rs +++ b/src/commands/sqlite/mod.rs @@ -49,14 +49,13 @@ enum Commands { /// Optimize an EveBox SQLite database Optimize(OptimizeArgs), /// Analyze an EveBox SQLite database - Analyze { - filename: String, - }, + Analyze { filename: String }, /// Enable auto-vacuum - EnableAutoVacuum { - filename: String, - }, + EnableAutoVacuum { filename: String }, + /// Reindex the database (can take a long time) Reindex(ReindexArgs), + /// Vacuum the database + Vacuum { filename: String }, } #[derive(Parser, Debug)] @@ -149,6 +148,7 @@ pub async fn main(args: &ArgMatches) -> anyhow::Result<()> { Commands::Analyze { filename } => analyze(filename).await, Commands::EnableAutoVacuum { filename } => enable_auto_vacuum(filename).await, Commands::Reindex(args) => reindex(args).await, + Commands::Vacuum { filename } => vacuum(filename).await, } } @@ -396,6 +396,22 @@ async fn reindex(args: &ReindexArgs) -> Result<()> { Ok(()) } +async fn vacuum(filename: &str) -> Result<()> { + println!("WARNING: Vacuuming can take a while"); + println!("- Database availability will be limited."); + if !confirm("Do you wish to continue?") { + return Ok(()); + } + + let mut conn = ConnectionBuilder::filename(Some(filename)) + .open_connection(false) + .await?; + + sqlx::query("VACUUM").execute(&mut conn).await?; + + Ok(()) +} + fn confirm(msg: &str) -> bool { inquire::Confirm::new(msg).prompt().unwrap_or(false) }