Skip to content

Commit 0d072fd

Browse files
committed
fix: add a passthrough method to access underlying transaction depth
1 parent 102d013 commit 0d072fd

6 files changed

Lines changed: 39 additions & 19 deletions

File tree

libs/driver-adapters/src/queryable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ impl JsQueryable {
361361
isolation: Option<IsolationLevel>,
362362
) -> quaint::Result<Box<dyn Transaction + 'a>> {
363363
let tx = self.driver_proxy.start_transaction(isolation).await?;
364-
tx.depth += 1;
364+
365+
tx.increment_depth();
366+
365367
self.server_reset_query(tx.as_ref()).await?;
366368
Ok(tx)
367369
}

libs/driver-adapters/src/transaction.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
future::Future,
3-
sync::atomic::{AtomicI32, Ordering},
4-
};
1+
use std::sync::atomic::{AtomicI32, Ordering};
52

63
use async_trait::async_trait;
74
use prisma_metrics::gauge;

quaint/src/connector/queryable.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::{DescribedQuery, ExternalConnector, IsolationLevel, ResultSet, Transaction};
22
use std::borrow::Cow;
33

4-
use super::{DescribedQuery, IsolationLevel, ResultSet, Transaction};
54
use crate::ast::*;
65
use async_trait::async_trait;
76

query-engine/connector-test-kit-rs/query-engine-tests/tests/new/interactive_tx.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ mod interactive_tx {
556556
let known_err = error.as_known().unwrap();
557557

558558
assert_eq!(known_err.error_code, Cow::Borrowed("P2028"));
559+
559560
assert!(known_err
560561
.message
561562
.contains("A rollback cannot be executed on a transaction that was rolled back"));
@@ -593,9 +594,12 @@ mod interactive_tx {
593594
let known_err = error.as_known().unwrap();
594595

595596
assert_eq!(known_err.error_code, Cow::Borrowed("P2028"));
596-
assert!(known_err
597-
.message
598-
.contains("A commit cannot be executed on a transaction that was rolled back"));
597+
assert!(
598+
known_err
599+
.message
600+
.contains("A commit cannot be executed on a transaction that was rolled back"),
601+
"{known_err:?}"
602+
);
599603

600604
// Check that the commit didn't work
601605
insta::assert_snapshot!(
@@ -630,9 +634,13 @@ mod interactive_tx {
630634
let known_err = error.as_known().unwrap();
631635

632636
assert_eq!(known_err.error_code, Cow::Borrowed("P2028"));
633-
assert!(known_err
634-
.message
635-
.contains("A rollback cannot be executed on a committed transaction"));
637+
638+
assert!(
639+
known_err
640+
.message
641+
.contains("A rollback cannot be executed on a committed transaction"),
642+
"{known_err:?}"
643+
);
636644

637645
// Check that the commit worked
638646
insta::assert_snapshot!(

query-engine/core/src/interactive_transactions/manager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ impl ItxManager {
191191
pub async fn commit_tx(&self, tx_id: &TxId) -> crate::Result<()> {
192192
let transaction_entry = self.get_transaction(tx_id, "commit").await?;
193193
let mut tx = transaction_entry.lock().await;
194-
let depth = tx.depth();
194+
let depth = tx.depth()?;
195+
195196
if depth > 1 {
196197
tx.release_savepoint().await
197198
} else {
@@ -202,7 +203,7 @@ impl ItxManager {
202203
pub async fn rollback_tx(&self, tx_id: &TxId) -> crate::Result<()> {
203204
let transaction_entry = self.get_transaction(tx_id, "rollback").await?;
204205
let mut tx = transaction_entry.lock().await;
205-
let depth = tx.depth();
206+
let depth = tx.depth()?;
206207
if depth > 1 {
207208
tx.rollback_to_savepoint().await
208209
} else {

query-engine/core/src/interactive_transactions/transaction.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ impl TransactionState {
9999
}
100100
}
101101

102+
// Allow depth to be called on the transaction even if it's closed.
103+
// This is usefule for the manger to handle nested transactions in a sane
104+
// way, and allows useful error messages to propagate, since depth tracking
105+
// is an internal detail of the engine.
106+
// If the transaction is closed, we return 0, since the depth is not tracked
107+
// anymore, and we allow the usual error message for rollback/commit/savepointX
108+
// to be returned.
109+
fn depth(&self) -> crate::Result<i32> {
110+
match self {
111+
Self::Open { tx, .. } => Ok(tx.depth()),
112+
Self::Committed => Ok(0),
113+
Self::RolledBack => Ok(0),
114+
Self::Expired { .. } => Ok(0),
115+
}
116+
}
117+
102118
fn as_closed(&self) -> Option<ClosedTransaction> {
103119
match self {
104120
Self::Open { .. } => None,
@@ -197,11 +213,8 @@ impl InteractiveTransaction {
197213
})
198214
}
199215

200-
pub fn depth(&mut self) -> i32 {
201-
match self.state.as_open("depth") {
202-
Ok(state) => state.depth(),
203-
Err(_) => 0,
204-
}
216+
pub fn depth(&mut self) -> crate::Result<i32> {
217+
self.state.depth()
205218
}
206219

207220
pub async fn commit(&mut self) -> crate::Result<()> {

0 commit comments

Comments
 (0)