From 22928a639adbf54cd128ed4cd23ebda62aaeb912 Mon Sep 17 00:00:00 2001 From: haffel Date: Thu, 10 Sep 2026 21:21:24 +0200 Subject: [PATCH] Enforce clipboard deduplication in schema --- .agents/CLIPBHOARD_HARDENING_CHECKLIST.md | 2 +- migrations/002_unique_content.sql | 9 +++++++++ src/storage/migrations.rs | 5 ++++- src/storage/mod.rs | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 migrations/002_unique_content.sql diff --git a/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md b/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md index 5e4b79f..787eeb5 100644 --- a/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md +++ b/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md @@ -59,7 +59,7 @@ Each bounded iteration must: ## Storage and migrations -- [ ] Enforce deduplication at the database level with an appropriate constraint/index. +- [x] Enforce deduplication at the database level with an appropriate constraint/index (migration and 18 core tests, Docker GTK/X11 build, 2026-09-10). - [ ] Add migration coverage and recovery behavior for schema failures. - [ ] Test persistence, trimming, pinning, deletion, and clear-history failure paths. - [ ] Confirm startup cleanup and shutdown cleanup under crashes and interrupted runs. diff --git a/migrations/002_unique_content.sql b/migrations/002_unique_content.sql new file mode 100644 index 0000000..f340942 --- /dev/null +++ b/migrations/002_unique_content.sql @@ -0,0 +1,9 @@ +DELETE FROM entries +WHERE id NOT IN ( + SELECT MAX(id) + FROM entries + GROUP BY content_hash, content +); + +CREATE UNIQUE INDEX IF NOT EXISTS entries_content_dedup + ON entries(content_hash, content); diff --git a/src/storage/migrations.rs b/src/storage/migrations.rs index 4821378..efc4c7c 100644 --- a/src/storage/migrations.rs +++ b/src/storage/migrations.rs @@ -1,7 +1,10 @@ use anyhow::Result; use rusqlite::Connection; -const MIGRATIONS: &[&str] = &[include_str!("../../migrations/001_initial.sql")]; +const MIGRATIONS: &[&str] = &[ + include_str!("../../migrations/001_initial.sql"), + include_str!("../../migrations/002_unique_content.sql"), +]; pub fn run(connection: &Connection) -> Result<()> { connection.execute_batch( diff --git a/src/storage/mod.rs b/src/storage/mod.rs index fbd1e49..2075518 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -187,4 +187,18 @@ mod tests { assert!(db.toggle_pinned(first.id, 1).unwrap() == false); assert!(db.toggle_pinned(second.id, 1).unwrap()); } + + #[test] + fn migration_enforces_content_deduplication_index() { + let db = Database::open("file:clipbhoard-index?mode=memory&cache=shared").unwrap(); + let unique: i64 = db + .connection + .query_row( + "SELECT COUNT(*) FROM pragma_index_list('entries') WHERE name = 'entries_content_dedup'", + [], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(unique, 1); + } }