Improve unique index duplicate value error messages

Closes #75
This commit is contained in:
Tobie Morgan Hitchcock 2022-08-31 20:00:22 +01:00
parent fbd9c12cc8
commit 308004aacf
4 changed files with 30 additions and 17 deletions

View file

@ -53,17 +53,20 @@ impl<'a> Document<'a> {
// Delete the old index data // Delete the old index data
if self.initial.is_some() { if self.initial.is_some() {
#[rustfmt::skip] #[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 let _ = run.delc(key, Some(rid)).await; // Ignore this error
} }
// Create the new index data // Create the new index data
if self.current.is_some() { if self.current.is_some() {
#[rustfmt::skip] #[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() { if run.putc(key, rid, None).await.is_err() {
return Err(Error::IndexExists { return Err(Error::IndexExists {
index: ix.name.to_string(), 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 // Delete the old index data
if self.initial.is_some() { if self.initial.is_some() {
#[rustfmt::skip] #[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 let _ = run.delc(key, Some(rid)).await; // Ignore this error
} }
// Create the new index data // Create the new index data
if self.current.is_some() { if self.current.is_some() {
#[rustfmt::skip] #[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() { if run.putc(key, rid, None).await.is_err() {
return Err(Error::IndexExists { return Err(Error::IndexExists {
index: ix.name.to_string(), index: ix.name.to_string(),
thing: rid.to_string(), value: match n.len() {
1 => n.first().unwrap().to_string(),
_ => n.to_string(),
},
}); });
} }
} }

View file

@ -217,10 +217,10 @@ pub enum Error {
}, },
/// A database index entry for the specified record already exists /// 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 { IndexExists {
index: String, index: String,
thing: String, value: String,
}, },
/// The specified field did not conform to the field ASSERT clause /// The specified field did not conform to the field ASSERT clause

View file

@ -47,8 +47,15 @@ pub struct Index {
pub id: Option<Id>, pub id: Option<Id>,
} }
pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: Array, id: Option<&Id>) -> Index { 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()) 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<u8> { pub fn prefix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec<u8> {

View file

@ -654,7 +654,7 @@ async fn define_statement_index_single_unique() -> Result<(), Error> {
let tmp = res.remove(0).result; let tmp = res.remove(0).result;
assert!(matches!( assert!(matches!(
tmp.err(), 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(()) Ok(())
@ -704,13 +704,13 @@ async fn define_statement_index_multiple_unique() -> Result<(), Error> {
let tmp = res.remove(0).result; let tmp = res.remove(0).result;
assert!(matches!( assert!(matches!(
tmp.err(), 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; let tmp = res.remove(0).result;
assert!(matches!( assert!(matches!(
tmp.err(), 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(()) Ok(())
@ -743,13 +743,13 @@ async fn define_statement_index_single_unique_existing() -> Result<(), Error> {
let tmp = res.remove(0).result; let tmp = res.remove(0).result;
assert!(matches!( assert!(matches!(
tmp.err(), 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; let tmp = res.remove(0).result;
assert!(matches!( assert!(matches!(
tmp.err(), 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?; 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; let tmp = res.remove(0).result;
assert!(matches!( assert!(matches!(
tmp.err(), 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; let tmp = res.remove(0).result;
assert!(matches!( assert!(matches!(
tmp.err(), 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?; let tmp = res.remove(0).result?;