diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-01-16 11:25:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-16 11:25:09 +0000 |
| commit | a2578c4521d4615d8265744ab51a1cc4f291605e (patch) | |
| tree | 26fb3da7a1d7312691703919cc700e433bbd1220 /atuin-client/src/database.rs | |
| parent | fix(sync): save sync time when it starts, not ends (#1573) (diff) | |
| download | atuin-a2578c4521d4615d8265744ab51a1cc4f291605e.zip | |
feat: add history rebuild (#1575)
* feat: add history rebuild
This adds a function that will
1. List all history from the store
2. Segment by create/delete
3. Insert all creates into the database
4. Delete all deleted
This replaces the old history sync.
Presently it's incomplete. There is no incremental rebuild, it can only
do the entire thing at once.
This is ran by `atuin store rebuild history`
* fix tests
* add incremental sync
* add auto sync
Diffstat (limited to 'atuin-client/src/database.rs')
| -rw-r--r-- | atuin-client/src/database.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 376c7b75..a6957093 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -18,7 +18,7 @@ use sqlx::{ }; use time::OffsetDateTime; -use crate::history::HistoryStats; +use crate::history::{HistoryId, HistoryStats}; use super::{ history::History, @@ -93,6 +93,7 @@ pub trait Database: Send + Sync + 'static { async fn before(&self, timestamp: OffsetDateTime, count: i64) -> Result<Vec<History>>; async fn delete(&self, h: History) -> Result<()>; + async fn delete_rows(&self, ids: &[HistoryId]) -> Result<()>; async fn deleted(&self) -> Result<Vec<History>>; // Yes I know, it's a lot. @@ -172,6 +173,18 @@ impl Sqlite { Ok(()) } + async fn delete_row_raw( + tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>, + id: HistoryId, + ) -> Result<()> { + sqlx::query("delete from history where id = ?1") + .bind(id.0.as_str()) + .execute(&mut **tx) + .await?; + + Ok(()) + } + fn query_history(row: SqliteRow) -> History { let deleted_at: Option<i64> = row.get("deleted_at"); @@ -567,6 +580,18 @@ impl Database for Sqlite { Ok(()) } + async fn delete_rows(&self, ids: &[HistoryId]) -> Result<()> { + let mut tx = self.pool.begin().await?; + + for id in ids { + Self::delete_row_raw(&mut tx, id.clone()).await?; + } + + tx.commit().await?; + + Ok(()) + } + async fn stats(&self, h: &History) -> Result<HistoryStats> { // We select the previous in the session by time let mut prev = SqlBuilder::select_from("history"); |
