-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Easier customization of the connection pool in rocket_sync_db_pools
#1838
Comments
If you are using sqlite, you may use the given in-memory database like this:
|
This seems pretty unusual - if you don't keep and then verify changes to the database, are you really testing that the routes work? More often we've seen a need for #1197 - to make changes, observe changes, keep changes, and discard them all after a series of routes have been exercised.
In the case of |
rocket_sync_db_pools
Both will work in my case. Because the data that is returned from the database is the data that has been updated/changed/created/... so we know the endpoint works (mostly) correctly. For our usecase this is currently more then enough. #1197 would be better but is much more difficult to implement at this stage. So having something simple/good enough is better then nothing.
If that solves the problem or reduces the code needed to pull this off it would be step in the right direction. But in If I can just directly change the connection_customizer for the |
The constraint is required within the current design; to meet this requirement you could additionally implement your own type that implements |
In use rocket::figment::Figment;
use rocket_db_pools::{Pool, Config, Error};
use rocket::futures::future::TryFutureExt;
struct MyPool(PgPool);
#[rocket::async_trait]
impl Pool for MyPool {
type Connection = <PgPool as Pool>::Connection;
type Error = <PgPool as Pool>::Error;
async fn init(figment: &Figment) -> Result<Self, Self::Error> {
<PgPool as Pool>::init(figment).map_ok(Self).await
}
async fn get(&self) -> Result<Self::Connection, Self::Error> {
let conn = <PgPool as Pool>::get(&self.0).await
conn.begin_transaction().await?;
Ok(conn)
}
async fn close(&self) {
<PgPool as Pool>::close(&self.0).await
}
} I wouldn't mind a rewrite of |
Description
When testing my code I want to be able to test out my API endpoint, but not keep the changes in the database.
I can do this in Diesel by using
conn.begin_test_transaction()
. But to use this with rocket is fairly complicated right now.Some small changes could make this much easier to do.
The code below is based on a bunch of thing, including this: diesel-rs/diesel#2733
How could it work (does not work currently)
But this does not work because of either
Poolable
ordiesel::PgConnection
needs to be implemented in the current crate.So to get around this you can always implement your own object and use that. Like this:
But this does not work because of the following constraint:
type Manager: ManageConnection<Connection=Self>;
This constraint is reasonable in most cases but does not allow for a different struct in these cases.
If this constraint could be removed or be made generic then this would allow for custom poolable connections.
This would make it much easier to implement this.
Right now you have to manually implement
#[rocket_contrib::database("databasename")]
And re-implement functions like
run
,fairing
,get
, ... andFromRequest
.So this is about 250 lines of code instead of the 28 from above.
It would be a very welcome change.
I can create a MR for this if this is something that you would like to see added.
Environment:
The text was updated successfully, but these errors were encountered: