Skip to content

Commit

Permalink
sqlite: add vacuum command
Browse files Browse the repository at this point in the history
I normally do this with the SQLite command line tool, but thats not
always available.
  • Loading branch information
jasonish committed Jun 13, 2024
1 parent 4cea075 commit 84a16fe
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/commands/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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)
}

0 comments on commit 84a16fe

Please sign in to comment.