diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/atuin-scripts/src/database.rs | 13 | ||||
| -rw-r--r-- | crates/atuin-scripts/src/store.rs | 7 |
2 files changed, 19 insertions, 1 deletions
diff --git a/crates/atuin-scripts/src/database.rs b/crates/atuin-scripts/src/database.rs index 26fb9328..be113526 100644 --- a/crates/atuin-scripts/src/database.rs +++ b/crates/atuin-scripts/src/database.rs @@ -188,6 +188,19 @@ impl Database { Ok(res) } + pub async fn clear(&self) -> Result<()> { + debug!("clearing all scripts from sqlite"); + + sqlx::query("delete from script_tags") + .execute(&self.pool) + .await?; + sqlx::query("delete from scripts") + .execute(&self.pool) + .await?; + + Ok(()) + } + pub async fn delete(&self, id: &str) -> Result<()> { debug!("deleting script {}", id); diff --git a/crates/atuin-scripts/src/store.rs b/crates/atuin-scripts/src/store.rs index ba7a1ca1..e70f6909 100644 --- a/crates/atuin-scripts/src/store.rs +++ b/crates/atuin-scripts/src/store.rs @@ -91,7 +91,12 @@ impl ScriptStore { } pub async fn build(&self, database: Database) -> Result<()> { - // Get all the scripts from the database - they are already sorted by timestamp + // Clear existing data before replaying all records from the store. + // Without this, stale rows can cause unique constraint violations + // when records are replayed (eg name conflicts from renamed scripts). + database.clear().await?; + + // Get all the scripts from the store - they are already sorted by timestamp let scripts = self.scripts().await?; for script in scripts { |
