Skip to content

Commit d1af1d0

Browse files
authored
Merge pull request #230 from weiznich/fix/229
Also instrument the postgres connection builder
2 parents 9d41c62 + 7e96e50 commit d1af1d0

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

src/transaction_manager.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,21 @@ impl AnsiTransactionManager {
169169
{
170170
let is_broken = conn.transaction_state().is_broken.clone();
171171
let state = Self::get_transaction_state(conn)?;
172-
match state.transaction_depth() {
173-
None => {
174-
Self::critical_transaction_block(&is_broken, conn.batch_execute(sql)).await?;
175-
Self::get_transaction_state(conn)?
176-
.change_transaction_depth(TransactionDepthChange::IncreaseDepth)?;
177-
Ok(())
178-
}
179-
Some(_depth) => Err(Error::AlreadyInTransaction),
172+
if let Some(_depth) = state.transaction_depth() {
173+
return Err(Error::AlreadyInTransaction);
180174
}
175+
let instrumentation_depth = NonZeroU32::new(1);
176+
177+
conn.instrumentation()
178+
.on_connection_event(InstrumentationEvent::begin_transaction(
179+
instrumentation_depth.expect("We know that 1 is not zero"),
180+
));
181+
182+
// Keep remainder of this method in sync with `begin_transaction()`.
183+
Self::critical_transaction_block(&is_broken, conn.batch_execute(sql)).await?;
184+
Self::get_transaction_state(conn)?
185+
.change_transaction_depth(TransactionDepthChange::IncreaseDepth)?;
186+
Ok(())
181187
}
182188

183189
// This function should be used to await any connection

tests/instrumentation.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ impl From<InstrumentationEvent<'_>> for Event {
5454
}
5555

5656
async fn setup_test_case() -> (Arc<Mutex<Vec<Event>>>, TestConnection) {
57+
setup_test_case_with_connection(connection_with_sean_and_tess_in_users_table().await)
58+
}
59+
60+
fn setup_test_case_with_connection(
61+
mut conn: TestConnection,
62+
) -> (Arc<Mutex<Vec<Event>>>, TestConnection) {
5763
let events = Arc::new(Mutex::new(Vec::<Event>::new()));
5864
let events_to_check = events.clone();
59-
let mut conn = connection_with_sean_and_tess_in_users_table().await;
6065
conn.set_instrumentation(move |event: InstrumentationEvent<'_>| {
6166
events.lock().unwrap().push(event.into());
6267
});
@@ -255,3 +260,26 @@ async fn check_events_transaction_nested() {
255260
assert_matches!(events[10], Event::StartQuery { .. });
256261
assert_matches!(events[11], Event::FinishQuery { .. });
257262
}
263+
264+
#[cfg(feature = "postgres")]
265+
#[tokio::test]
266+
async fn check_events_transaction_builder() {
267+
use crate::connection_without_transaction;
268+
use diesel::result::Error;
269+
use scoped_futures::ScopedFutureExt;
270+
271+
let (events_to_check, mut conn) =
272+
setup_test_case_with_connection(connection_without_transaction().await);
273+
conn.build_transaction()
274+
.run(|_tx| async move { Ok::<(), Error>(()) }.scope_boxed())
275+
.await
276+
.unwrap();
277+
let events = events_to_check.lock().unwrap();
278+
assert_eq!(events.len(), 6, "{:?}", events);
279+
assert_matches!(events[0], Event::BeginTransaction { .. });
280+
assert_matches!(events[1], Event::StartQuery { .. });
281+
assert_matches!(events[2], Event::FinishQuery { .. });
282+
assert_matches!(events[3], Event::CommitTransaction { .. });
283+
assert_matches!(events[4], Event::StartQuery { .. });
284+
assert_matches!(events[5], Event::FinishQuery { .. });
285+
}

tests/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ async fn setup(connection: &mut TestConnection) {
203203
}
204204

205205
async fn connection() -> TestConnection {
206-
let db_url = std::env::var("DATABASE_URL").unwrap();
207-
let mut conn = TestConnection::establish(&db_url).await.unwrap();
206+
let mut conn = connection_without_transaction().await;
208207
if cfg!(feature = "postgres") {
209208
// postgres allows to modify the schema inside of a transaction
210209
conn.begin_test_transaction().await.unwrap();
@@ -218,3 +217,8 @@ async fn connection() -> TestConnection {
218217
}
219218
conn
220219
}
220+
221+
async fn connection_without_transaction() -> TestConnection {
222+
let db_url = std::env::var("DATABASE_URL").unwrap();
223+
TestConnection::establish(&db_url).await.unwrap()
224+
}

0 commit comments

Comments
 (0)