INSERT should respect unique indexes (#3092)

This commit is contained in:
Micha de Vries 2023-12-07 15:45:12 +01:00 committed by GitHub
parent 3a907813fd
commit daf02dc4e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 deletions

View file

@ -60,10 +60,10 @@ impl<'a> Document<'a> {
self.clean(ctx, opt, txn, stm).await?;
// Check if allowed
self.allow(ctx, opt, txn, stm).await?;
// Store record data
self.store(ctx, opt, txn, stm).await?;
// Store index data
self.index(ctx, opt, txn, stm).await?;
// Store record data
self.store(ctx, opt, txn, stm).await?;
// Run table queries
self.table(ctx, opt, txn, stm).await?;
// Run lives queries
@ -95,10 +95,10 @@ impl<'a> Document<'a> {
self.clean(ctx, opt, txn, stm).await?;
// Check if allowed
self.allow(ctx, opt, txn, stm).await?;
// Store record data
self.store(ctx, opt, txn, stm).await?;
// Store index data
self.index(ctx, opt, txn, stm).await?;
// Store record data
self.store(ctx, opt, txn, stm).await?;
// Run table queries
self.table(ctx, opt, txn, stm).await?;
// Run lives queries

View file

@ -488,3 +488,32 @@ async fn check_permissions_auth_disabled() {
assert!(res.unwrap() != Value::parse("[]"), "{}", "anonymous user should be able to insert a new record if the table exists and grants full permissions");
}
}
#[tokio::test]
async fn insert_statement_unique_index() -> Result<(), Error> {
let sql = "
DEFINE INDEX name ON TABLE company COLUMNS name UNIQUE;
INSERT INTO company { name: 'SurrealDB' };
INSERT INTO company { name: 'SurrealDB' };
SELECT count() FROM company GROUP ALL;
";
let dbs = new_ds().await?;
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 4);
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let tmp = res.remove(0).result?;
let val = Value::parse("[ { count: 1 } ]");
assert_eq!(tmp, val);
//
Ok(())
}