Skip to content

Commit

Permalink
feat: add abort method to RwTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed May 20, 2024
1 parent 3979adf commit b5c6365
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/transaction/rw_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,9 @@ impl<'db, 'txn> RwTransaction<'db> {
pub fn migrate<T: Input + Debug>(&self) -> Result<()> {
self.internal.migrate::<T>()
}

/// Abort the transaction.
pub fn abort(self) -> Result<()> {
Ok(self.internal.redb_transaction.abort()?)
}
}
25 changes: 25 additions & 0 deletions tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ fn test_transaction_obj_1_and_obj_2() {
assert_eq!(result.id, 2);
}

#[test]
fn test_abort_transaction_obj_1_and_obj_2() {
let tf = TmpFs::new().unwrap();

let mut builder = DatabaseBuilder::new();
builder.define::<Item>().unwrap();
builder.define::<Item2>().unwrap();
let db = builder.create(tf.path("test").as_std_path()).unwrap();

let item_1 = Item {
id: 1,
name: "test".to_string(),
};

let rw = db.rw_transaction().unwrap();
rw.insert(item_1).unwrap();
rw.abort().unwrap();
// After abort, the transaction, the transaction can not be used anymore.
//rw.insert(item_2).unwrap();
//rw.commit().unwrap();

let r = db.r_transaction().unwrap();
assert!(r.get().primary::<Item>(1u32).unwrap().is_none());
}

#[allow(unreachable_code)]
#[test]
fn test_transaction_fail() {
Expand Down

0 comments on commit b5c6365

Please sign in to comment.