-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Minimal example with Transaction? #20
Comments
Hey elpiel! What you're asking should be very simple, if you know intermediate level Rust. |
Well actually I coulnd't.
PS: I am probably not explaining it very well since I am also learning, but it's not that easy as it seems as well. And I am having a hard time doing it without any guidance or documentation/examples for quite some time now. |
I haven't actually run this against a real database but I think it should work: 36c52b6#diff-fe61805d997a94ac99aa2ae71f7750d7R30 The tokio-postgres transaction API is not usable from bb8 at the moment, unfortunately, so you need to do it manually like this. |
@khuey would you guide me through the implementation? I would like to try to implement it, if you have an idea how it will work? |
Guide you through the implementation of what, exactly? |
How to implement the Transaction API of |
So tokio-postgres's transaction API is available[0], it just doesn't work. The What we need is a fn transaction<R, E, Fut, F>(connection: tokio_postgres::Client, f: F) -> impl Future<Item = (tokio_postgres::Client, R), Error = (tokio_postgres::Client, E)>
where F: FnOnce(tokio_postgres::Client) -> Fut,
Fut: Future<Item = (tokio_postgres::Client, R), Error = (tokio_postgres::Client, E),
E: From<tokio_postgres::Error>
{
connection.simple_query("BEGIN")
...
.and_then(|connection| {
f(connection)
})
.and_then(|(mut connection, result)| {
connection.simple_query("COMMIT")
...
.then(|r| match r {
Ok(_) => Ok((connection, result)),
Err(e) => Err((connection, e))
})
})
.or_else(|(mut connection, e)| {
connection.simple_query("ROLLBACK")
...
.then(|_| Err((connection, e)))
})
} So step 1 is to write that function and get it working. [0] https://docs.rs/tokio-postgres/0.4.0-rc.2/tokio_postgres/struct.Client.html#method.build_transaction |
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
+} |
Woops, meant to post that in the PR. |
Hello,
I'm trying to make a transaction, but I am hitting a wall and can't figure it out.
Is it possible for an example with transaction and 2 inserts for example?
The text was updated successfully, but these errors were encountered: