Enforce clipboard deduplication in schema

This commit is contained in:
2026-09-10 21:21:24 +02:00
parent 73efcb3c35
commit 22928a639a
4 changed files with 28 additions and 2 deletions

View File

@@ -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.

View File

@@ -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);

View File

@@ -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(

View File

@@ -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);
}
}