From 165ff7a826999e98b73e6f062fcb0e55069e64d3 Mon Sep 17 00:00:00 2001 From: Hamza Date: Fri, 4 Dec 2020 17:15:02 +0500 Subject: [PATCH 1/2] Fix transaction wrapper --- sqlx-core/src/connection.rs | 48 +++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/sqlx-core/src/connection.rs b/sqlx-core/src/connection.rs index d4b0a028e3..5c67ce9f33 100644 --- a/sqlx-core/src/connection.rs +++ b/sqlx-core/src/connection.rs @@ -35,30 +35,42 @@ pub trait Connection: Send { /// /// If the function returns an error, the transaction will be rolled back. If it does not /// return an error, the transaction will be committed. - fn transaction<'c: 'f, 'f, T, E, F, Fut>(&'c mut self, f: F) -> BoxFuture<'f, Result> - where - Self: Sized, - T: Send, - F: FnOnce(&mut ::Connection) -> Fut + Send + 'f, - E: From + Send, - Fut: Future> + Send, + /// + /// # Example + /// + /// ```rust + /// use sqlx_core::postgres::{PgConnection, PgRow}; + /// use sqlx_core::connection::Connection; + /// use sqlx_core::error::Error; + /// + /// # pub async fn _f(conn: &mut PgConnection) -> Result, Error> { + /// conn.transaction(|conn|Box::pin(async move { + /// sqlx::query("select * from ..").fetch_all(conn).await + /// })).await + /// # } + /// ``` + fn transaction(&mut self, callback: F) -> BoxFuture> + where + for<'c> F: + FnOnce(&'c mut Transaction) -> BoxFuture<'c, Result> + 'static + Send + Sync, + Self: Sized, + R: Send, + E: From + Send, { Box::pin(async move { - let mut tx = self.begin().await?; + let mut transaction = self.begin().await?; + let ret = callback(&mut transaction).await; - match f(&mut tx).await { - Ok(r) => { - // no error occurred, commit the transaction - tx.commit().await?; + match ret { + Ok(ret) => { + transaction.commit().await?; - Ok(r) + Ok(ret) } + Err(err) => { + transaction.rollback().await?; - Err(e) => { - // an error occurred, rollback the transaction - tx.rollback().await?; - - Err(e) + Err(err) } } }) From e211404bfaed03f718c98f1ba5f9b49b941e7f38 Mon Sep 17 00:00:00 2001 From: Hamza Date: Fri, 4 Dec 2020 17:36:42 +0500 Subject: [PATCH 2/2] Ensure tests pass --- sqlx-core/src/connection.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sqlx-core/src/connection.rs b/sqlx-core/src/connection.rs index 5c67ce9f33..28d8f18849 100644 --- a/sqlx-core/src/connection.rs +++ b/sqlx-core/src/connection.rs @@ -39,23 +39,27 @@ pub trait Connection: Send { /// # Example /// /// ```rust - /// use sqlx_core::postgres::{PgConnection, PgRow}; /// use sqlx_core::connection::Connection; /// use sqlx_core::error::Error; + /// use sqlx_core::executor::Executor; + /// use sqlx_core::postgres::{PgConnection, PgRow}; + /// use sqlx_core::query::query; /// /// # pub async fn _f(conn: &mut PgConnection) -> Result, Error> { /// conn.transaction(|conn|Box::pin(async move { - /// sqlx::query("select * from ..").fetch_all(conn).await + /// query("select * from ..").fetch_all(conn).await /// })).await /// # } /// ``` fn transaction(&mut self, callback: F) -> BoxFuture> - where - for<'c> F: - FnOnce(&'c mut Transaction) -> BoxFuture<'c, Result> + 'static + Send + Sync, - Self: Sized, - R: Send, - E: From + Send, + where + for<'c> F: FnOnce(&'c mut Transaction) -> BoxFuture<'c, Result> + + 'static + + Send + + Sync, + Self: Sized, + R: Send, + E: From + Send, { Box::pin(async move { let mut transaction = self.begin().await?;