Unique index should ignore none or null values ()

This commit is contained in:
Emmanuel Keller 2023-07-21 19:40:59 +01:00 committed by GitHub
parent a2f4d6776d
commit b66e537f98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions
lib

View file

@ -164,9 +164,11 @@ impl<'a> IndexOperation<'a> {
}
// Create the new index data
if let Some(n) = &self.n {
let key = self.get_unique_index_key(n);
if run.putc(key, self.rid, None).await.is_err() {
return self.err_index_exists(n);
if !n.is_all_none_or_null() {
let key = self.get_unique_index_key(n);
if run.putc(key, self.rid, None).await.is_err() {
return self.err_index_exists(n);
}
}
}
Ok(())

View file

@ -134,6 +134,10 @@ impl Array {
}
Ok(Value::Array(x))
}
pub(crate) fn is_all_none_or_null(&self) -> bool {
self.0.iter().all(|v| v.is_none_or_null())
}
}
impl Display for Array {

View file

@ -100,3 +100,22 @@ async fn create_with_id() -> Result<(), Error> {
//
Ok(())
}
#[tokio::test]
async fn create_on_non_values_with_unique_index() -> Result<(), Error> {
let sql = "
DEFINE INDEX national_id_idx ON foo FIELDS national_id UNIQUE;
CREATE foo SET name = 'John Doe';
CREATE foo SET name = 'Jane Doe';
";
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 3);
//
for _ in 0..3 {
let _ = res.remove(0).result?;
}
Ok(())
}