Quantcast
Channel: Injecting a Diesel connection into an Iron middleware - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Injecting a Diesel connection into an Iron middleware

$
0
0

In writing my tests, I'd like to be able to inject a connection into the request so that I can wrap the entire test case in a transaction (even if there is more than one request in the test case).

I've attempted to do this using a BeforeMiddleware which I can link in my test cases to insert a connection, as such:

pub type DatabaseConnection = PooledConnection<ConnectionManager<PgConnection>>;pub struct DatabaseOverride {    conn: DatabaseConnection,}impl BeforeMiddleware for DatabaseOverride {    fn before(&self, req: &mut Request) -> IronResult<()> {        req.extensions_mut().entry::<DatabaseOverride>().or_insert(self.conn);        Ok(())    }}

However, I'm encountering a compile error in trying to do this:

error: the trait bound `std::rc::Rc<diesel::pg::connection::raw::RawConnection>: std::marker::Sync` is not satisfied [E0277]impl BeforeMiddleware for DatabaseOverride {     ^~~~~~~~~~~~~~~~help: run `rustc --explain E0277` to see a detailed explanationnote: `std::rc::Rc<diesel::pg::connection::raw::RawConnection>` cannot be shared between threads safelynote: required because it appears within the type `diesel::pg::PgConnection`note: required because it appears within the type `r2d2::Conn<diesel::pg::PgConnection>`note: required because it appears within the type `std::option::Option<r2d2::Conn<diesel::pg::PgConnection>>`note: required because it appears within the type `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>`note: required because it appears within the type `utility::db::DatabaseOverride`note: required by `iron::BeforeMiddleware`error: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not satisfied [E0277]impl BeforeMiddleware for DatabaseOverride {     ^~~~~~~~~~~~~~~~help: run `rustc --explain E0277` to see a detailed explanationnote: `std::cell::Cell<i32>` cannot be shared between threads safelynote: required because it appears within the type `diesel::pg::PgConnection`note: required because it appears within the type `r2d2::Conn<diesel::pg::PgConnection>`note: required because it appears within the type `std::option::Option<r2d2::Conn<diesel::pg::PgConnection>>`note: required because it appears within the type `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>`note: required because it appears within the type `utility::db::DatabaseOverride`note: required by `iron::BeforeMiddleware`

Is there a way around this with diesel's connections? I've found several examples on Github to do this using the pg crate, but I'd like to keep using diesel.


Viewing all articles
Browse latest Browse all 3

Trending Articles