From d016e2c9be39072b3b0eaa7cb35418e4549c3823 Mon Sep 17 00:00:00 2001 From: 依云 Date: Fri, 18 Apr 2025 06:21:25 +0800 Subject: feat: delete duplicate history (#2697) --- crates/atuin-client/src/database.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'crates/atuin-client/src/database.rs') diff --git a/crates/atuin-client/src/database.rs b/crates/atuin-client/src/database.rs index de362766..06f0bbb8 100644 --- a/crates/atuin-client/src/database.rs +++ b/crates/atuin-client/src/database.rs @@ -119,6 +119,8 @@ pub trait Database: Send + Sync + 'static { async fn all_with_count(&self) -> Result>; async fn stats(&self, h: &History) -> Result; + + async fn get_dups(&self, before: i64, dupkeep: u32) -> Result>; } // Intended for use on a developer machine and not a sync server. @@ -768,6 +770,26 @@ impl Database for Sqlite { duration_over_time, }) } + + async fn get_dups(&self, before: i64, dupkeep: u32) -> Result> { + let res = sqlx::query( + "SELECT * FROM ( + SELECT *, ROW_NUMBER() + OVER (PARTITION BY command, cwd, hostname ORDER BY timestamp DESC) + AS rn + FROM history + ) sub + WHERE rn > ?1 and timestamp < ?2; + ", + ) + .bind(dupkeep) + .bind(before) + .map(Self::query_history) + .fetch_all(&self.pool) + .await?; + + Ok(res) + } } trait SqlBuilderExt { -- cgit v1.3.1