From 308004aacff13102ab16a22dcf540a2d0df4981d Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 31 Aug 2022 20:00:22 +0100 Subject: [PATCH] Improve unique index duplicate value error messages Closes #75 --- lib/src/doc/index.rs | 18 ++++++++++++------ lib/src/err/mod.rs | 4 ++-- lib/src/key/index.rs | 11 +++++++++-- lib/tests/define.rs | 14 +++++++------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/src/doc/index.rs b/lib/src/doc/index.rs index 92b034b5..7ff5e926 100644 --- a/lib/src/doc/index.rs +++ b/lib/src/doc/index.rs @@ -53,17 +53,20 @@ impl<'a> Document<'a> { // Delete the old index data if self.initial.is_some() { #[rustfmt::skip] - let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, o, None); + let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &o, None); let _ = run.delc(key, Some(rid)).await; // Ignore this error } // Create the new index data if self.current.is_some() { #[rustfmt::skip] - let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, n, None); + let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &n, None); if run.putc(key, rid, None).await.is_err() { return Err(Error::IndexExists { index: ix.name.to_string(), - thing: rid.to_string(), + value: match n.len() { + 1 => n.first().unwrap().to_string(), + _ => n.to_string(), + }, }); } } @@ -72,17 +75,20 @@ impl<'a> Document<'a> { // Delete the old index data if self.initial.is_some() { #[rustfmt::skip] - let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, o, Some(&rid.id)); + let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &o, Some(&rid.id)); let _ = run.delc(key, Some(rid)).await; // Ignore this error } // Create the new index data if self.current.is_some() { #[rustfmt::skip] - let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, n, Some(&rid.id)); + let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, &n, Some(&rid.id)); if run.putc(key, rid, None).await.is_err() { return Err(Error::IndexExists { index: ix.name.to_string(), - thing: rid.to_string(), + value: match n.len() { + 1 => n.first().unwrap().to_string(), + _ => n.to_string(), + }, }); } } diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index 74612ee4..c5461b2b 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -217,10 +217,10 @@ pub enum Error { }, /// A database index entry for the specified record already exists - #[error("Database index `{index}` already contains `{thing}`")] + #[error("Database index `{index}` already contains {value}")] IndexExists { index: String, - thing: String, + value: String, }, /// The specified field did not conform to the field ASSERT clause diff --git a/lib/src/key/index.rs b/lib/src/key/index.rs index fda21641..3fae9cdc 100644 --- a/lib/src/key/index.rs +++ b/lib/src/key/index.rs @@ -47,8 +47,15 @@ pub struct Index { pub id: Option, } -pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: Array, id: Option<&Id>) -> Index { - Index::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string(), fd, id.cloned()) +pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: &Array, id: Option<&Id>) -> Index { + Index::new( + ns.to_string(), + db.to_string(), + tb.to_string(), + ix.to_string(), + fd.to_owned(), + id.cloned(), + ) } pub fn prefix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec { diff --git a/lib/tests/define.rs b/lib/tests/define.rs index 1c072d2c..b0ee2ea3 100644 --- a/lib/tests/define.rs +++ b/lib/tests/define.rs @@ -654,7 +654,7 @@ async fn define_statement_index_single_unique() -> Result<(), Error> { let tmp = res.remove(0).result; assert!(matches!( tmp.err(), - Some(e) if e.to_string() == "Database index `test` already contains `user:2`" + Some(e) if e.to_string() == r#"Database index `test` already contains "test@surrealdb.com""# )); // Ok(()) @@ -704,13 +704,13 @@ async fn define_statement_index_multiple_unique() -> Result<(), Error> { let tmp = res.remove(0).result; assert!(matches!( tmp.err(), - Some(e) if e.to_string() == "Database index `test` already contains `user:3`" + Some(e) if e.to_string() == r#"Database index `test` already contains ["apple", "test@surrealdb.com"]"# )); // let tmp = res.remove(0).result; assert!(matches!( tmp.err(), - Some(e) if e.to_string() == "Database index `test` already contains `user:4`" + Some(e) if e.to_string() == r#"Database index `test` already contains ["tesla", "test@surrealdb.com"]"# )); // Ok(()) @@ -743,13 +743,13 @@ async fn define_statement_index_single_unique_existing() -> Result<(), Error> { let tmp = res.remove(0).result; assert!(matches!( tmp.err(), - Some(e) if e.to_string() == "Database index `test` already contains `user:3`" + Some(e) if e.to_string() == r#"Database index `test` already contains "test@surrealdb.com""# )); // let tmp = res.remove(0).result; assert!(matches!( tmp.err(), - Some(e) if e.to_string() == "Database index `test` already contains `user:3`" + Some(e) if e.to_string() == r#"Database index `test` already contains "test@surrealdb.com""# )); // let tmp = res.remove(0).result?; @@ -797,13 +797,13 @@ async fn define_statement_index_multiple_unique_existing() -> Result<(), Error> let tmp = res.remove(0).result; assert!(matches!( tmp.err(), - Some(e) if e.to_string() == "Database index `test` already contains `user:3`" + Some(e) if e.to_string() == r#"Database index `test` already contains ["apple", "test@surrealdb.com"]"# )); // let tmp = res.remove(0).result; assert!(matches!( tmp.err(), - Some(e) if e.to_string() == "Database index `test` already contains `user:3`" + Some(e) if e.to_string() == r#"Database index `test` already contains ["apple", "test@surrealdb.com"]"# )); // let tmp = res.remove(0).result?;