From 79f7b1d81c182bfbcc8298963886390658dda71b Mon Sep 17 00:00:00 2001 From: "J. Emiliano Deustua" Date: Tue, 13 Jun 2023 02:43:06 -0500 Subject: [PATCH] Fix `--delete-it-all` and `--delete` commands (#913) * Add `delete_at` is null condition during search Since entries are searched everytime a delete process ends, the --delete-it-all command will enter an infinite loop if searching the whole history. * Remove command blanking Command blanking may violate the `unique(timestamp, cwd, command)` condition. * Overwrite command with random string when deleting * Add rand dependency to client crate --------- Co-authored-by: Ellie Huxtable --- Cargo.lock | 1 + atuin-client/Cargo.toml | 1 + atuin-client/src/database.rs | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 7a310c0..c3604b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,6 +151,7 @@ dependencies = [ "memchr", "minspan", "parse_duration", + "rand", "regex", "reqwest", "rmp-serde", diff --git a/atuin-client/Cargo.toml b/atuin-client/Cargo.toml index 770d774..42e3cf6 100644 --- a/atuin-client/Cargo.toml +++ b/atuin-client/Cargo.toml @@ -41,6 +41,7 @@ serde_json = { workspace = true } parse_duration = "2.1.1" async-trait = { workspace = true } itertools = { workspace = true } +rand = { workspace = true } shellexpand = "2" sqlx = { workspace = true, features = ["sqlite"] } minspan = "0.1.1" diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index f7c063c..22bd588 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -6,6 +6,7 @@ use chrono::{prelude::*, Utc}; use fs_err as fs; use itertools::Itertools; use lazy_static::lazy_static; +use rand::{distributions::Alphanumeric, Rng}; use regex::Regex; use sql_builder::{esc, quote, SqlBuilder, SqlName}; use sqlx::{ @@ -460,6 +461,8 @@ impl Database for Sqlite { .map(|after| sql.and_where_gt("timestamp", quote(after.timestamp_nanos()))) }); + sql.and_where_is_null("deleted_at"); + let query = sql.sql().expect("bug in search query. please report"); let res = sqlx::query(&query) @@ -519,7 +522,11 @@ impl Database for Sqlite { // but the time that the system marks it as deleted async fn delete(&self, mut h: History) -> Result<()> { let now = chrono::Utc::now(); - h.command = String::from(""); // blank it + h.command = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(32) + .map(char::from) + .collect(); // overwrite with random string h.deleted_at = Some(now); // delete it self.update(&h).await?; // save it