Use 'diesel::r2d2' in state guide and todo example.

Diesel now reexports r2d2, so rather than including that library
explicitly, let's leverage the reexport.
This commit is contained in:
Eric Dattore 2018-02-21 08:47:50 -07:00 committed by Sergio Benitez
parent a80a187c8f
commit c48905f483
4 changed files with 16 additions and 25 deletions

View File

@ -9,10 +9,7 @@ rocket_codegen = { path = "../../codegen" }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
r2d2 = "0.8"
diesel = { version = "1.0", features = ["sqlite"] }
diesel_derives = { version = "1.0", features = ["sqlite"] }
r2d2-diesel = "1.0"
diesel = { version = "1.0", features = ["sqlite", "r2d2"] }
dotenv = "0.10"
[dev-dependencies]

View File

@ -1,23 +1,22 @@
use std::ops::Deref;
use r2d2;
use diesel::sqlite::SqliteConnection;
use r2d2_diesel::ConnectionManager;
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
use rocket::http::Status;
use rocket::request::{self, FromRequest};
use rocket::{Request, State, Outcome};
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
pub type SqlitePool = Pool<ConnectionManager<SqliteConnection>>;
pub const DATABASE_URL: &'static str = env!("DATABASE_URL");
pub fn init_pool() -> Pool {
pub fn init_pool() -> SqlitePool {
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_URL);
r2d2::Pool::new(manager).expect("db pool")
Pool::new(manager).expect("db pool")
}
pub struct Conn(pub r2d2::PooledConnection<ConnectionManager<SqliteConnection>>);
pub struct Conn(pub PooledConnection<ConnectionManager<SqliteConnection>>);
impl Deref for Conn {
type Target = SqliteConnection;
@ -32,7 +31,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Conn {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Conn, ()> {
let pool = request.guard::<State<Pool>>()?;
let pool = request.guard::<State<SqlitePool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(Conn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))

View File

@ -3,11 +3,8 @@
extern crate rocket;
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_derives;
#[macro_use] extern crate serde_derive;
extern crate rocket_contrib;
extern crate r2d2;
extern crate r2d2_diesel;
mod static_files;
mod task;

View File

@ -178,9 +178,8 @@ use the following dependencies:
```
[dependencies]
rocket = "0.4.0-dev"
diesel = { version = "1.0", features = ["sqlite"] }
r2d2-diesel = "1.0"
r2d2 = "0.8"
rocket_codegen = "0.4.0-dev"
diesel = { version = "1.0", features = ["sqlite", "r2d2"] }
```
Your `diesel` dependency information may differ. The crates are imported as
@ -189,8 +188,6 @@ well:
```rust
extern crate rocket;
#[macro_use] extern crate diesel;
extern crate r2d2_diesel;
extern crate r2d2;
```
### Managed Pool
@ -206,10 +203,10 @@ default configuration parameters and a Diesel `SqliteConnection`
```rust
use diesel::sqlite::SqliteConnection;
use r2d2_diesel::ConnectionManager;
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
// An alias to the type for a pool of Diesel SQLite connections.
type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
type SqlitePool = Pool<ConnectionManager<SqliteConnection>>;
// The URL to the database, set via the `DATABASE_URL` environment variable.
static DATABASE_URL: &'static str = env!("DATABASE_URL");
@ -217,7 +214,7 @@ static DATABASE_URL: &'static str = env!("DATABASE_URL");
/// Initializes a database pool.
fn init_pool() -> Pool {
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_URL);
r2d2::Pool::new(manager).expect("db pool")
Pool::new(manager).expect("db pool")
}
```
@ -245,9 +242,10 @@ use std::ops::Deref;
use rocket::http::Status;
use rocket::request::{self, FromRequest};
use rocket::{Request, State, Outcome};
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
// Connection request guard type: a wrapper around an r2d2 pooled connection.
pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<SqliteConnection>>);
pub struct DbConn(pub PooledConnection<ConnectionManager<SqliteConnection>>);
/// Attempts to retrieve a single connection from the managed database pool. If
/// no pool is currently managed, fails with an `InternalServerError` status. If
@ -255,8 +253,8 @@ pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<SqliteConnection>
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<DbConn, ()> {
let pool = request.guard::<State<Pool>>()?;
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let pool = request.guard::<State<SqlitePool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(DbConn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))