Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transaction #22

Closed
wants to merge 3 commits into from
Closed

Support transaction #22

wants to merge 3 commits into from

Conversation

elpiel
Copy link
Contributor

@elpiel elpiel commented May 25, 2019

Currently the fn transaction() was created for the postgres implementation as per #20 (comment) .
We can implement the same thing for Redis and add a transaction fn in bb8 that will call the specific connection transaction() fn I guess?

Resolves #20

@khuey I couldn't get the E: From<tokio_postgres::Error> working sadly.

error[E0271]: type mismatch resolving `<Fut as futures::IntoFuture>::Error == (tokio_postgres::Error, tokio_postgres::Client)`
   --> postgres/src/lib.rs:104:10
    |
104 |         .and_then(|connection| f(connection))
    |          ^^^^^^^^ expected type parameter, found struct `tokio_postgres::Error`
    |
    = note: expected type `(E, tokio_postgres::Client)`
               found type `(tokio_postgres::Error, tokio_postgres::Client)`

- examples - txn - update accordingly to use `transaction` fn
@khuey
Copy link
Collaborator

khuey commented Jul 19, 2019

Something like this works.

diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs
index 87c38ce..47bd6a7 100644
--- a/postgres/src/lib.rs
+++ b/postgres/src/lib.rs
@@ -90,16 +90,20 @@ where
 }

 /// Run asynchronous into a Transaction
-pub fn transaction<R, Fut, F>(mut connection: tokio_postgres::Client, f: F) -> impl Future<Item=(R, tokio_postgres::Client), Error=(tokio_postgres::Error, tokio_postgres::Client)>
+pub fn transaction<R, E, Fut, F>(mut connection: tokio_postgres::Client, f: F) -> impl Future<Item=(R, tokio_postgres::Client), Error=(E, tokio_postgres::Client)>
     where
         F: FnOnce(tokio_postgres::Client) -> Fut,
-        Fut: Future<Item=(R, tokio_postgres::Client), Error=(tokio_postgres::Error, tokio_postgres::Client)>,
+        E: From<tokio_postgres::Error>,
+        Fut: Future<Item=(R, tokio_postgres::Client), Error=(E, tokio_postgres::Client)>,
 {
     connection.simple_query("BEGIN")
         .for_each(|_| Ok(()))
         .then(|r| match r {
             Ok(_) => Ok(connection),
-            Err(e) => Err((e, connection)),
+            Err(e) => {
+                let error: E = e.into();
+                Err((error, connection))
+            },
         })
         .and_then(|connection| f(connection))
         .and_then(|(result, mut connection)| {
@@ -107,11 +111,11 @@ pub fn transaction<R, Fut, F>(mut connection: tokio_postgres::Client, f: F) -> i
                 .for_each(|_| Ok(()))
                 .then(|r| match r {
                     Ok(_) => Ok((result, connection)),
-                    Err(e) => Err((e, connection))
+                    Err(e) => Err((e.into(), connection))
                 })
         })
         .or_else(|(e, mut connection)| {
             connection.simple_query("ROLLBACK").for_each(|_| Ok(()))
-                .then(|_| Err((e, connection)))
+                .then(|_| Err((e.into(), connection)))
         })
-}
\ No newline at end of file
+}

@elpiel
Copy link
Contributor Author

elpiel commented Oct 9, 2019

@khuey thanks for the help, from here on, what's next?
Adding the method to ManageConnection?

@djc
Copy link
Owner

djc commented Dec 31, 2019

@elpiel are you still interested in finishing this?

@elpiel
Copy link
Contributor Author

elpiel commented Jan 1, 2020

Yes, I am. I would love to continue on this once the #45 is merged.

@godofdream
Copy link

@elpiel looks like #45 is merged.

@djc djc changed the base branch from master to main November 5, 2020 19:28
@djc
Copy link
Owner

djc commented Aug 30, 2021

Going to close this for lack of progress. Feel free to reopen (or open a new one) if someone wants to work on this.

@djc djc closed this Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minimal example with Transaction?
4 participants