From 07c96713714b5fcdb9b3885dee352d3cc385e975 Mon Sep 17 00:00:00 2001 From: calhilcaw Date: Sat, 30 Dec 2017 10:03:06 +1100 Subject: [PATCH] Update docs and examples for diesel 1.0.0-rc1. --- examples/todo/Cargo.toml | 9 +++++---- .../20160720150332_create_tasks_table/up.sql | 5 ++++- examples/todo/src/db.rs | 7 +++---- examples/todo/src/main.rs | 2 +- examples/todo/src/task.rs | 19 ++++++++++++------- site/guide/state.md | 18 +++++++----------- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/examples/todo/Cargo.toml b/examples/todo/Cargo.toml index b9e257b5..1fce1e66 100644 --- a/examples/todo/Cargo.toml +++ b/examples/todo/Cargo.toml @@ -9,10 +9,11 @@ rocket_codegen = { path = "../../codegen" } serde = "1.0" serde_json = "1.0" serde_derive = "1.0" -r2d2 = "0.7" -diesel = { version = "0.14", features = ["sqlite"] } -diesel_codegen = { version = "0.14", features = ["sqlite"] } -r2d2-diesel = "0.14" +r2d2 = "0.8" +diesel = { version = "1.0.0-rc1", features = ["sqlite"] } +diesel_derives = { version = "1.0.0-rc1", features = ["sqlite"] } +r2d2-diesel = "1.0.0-rc1" +dotenv = "0.10.*" # workaround for broken dependencies [dev-dependencies] parking_lot = {version = "0.4", features = ["nightly"]} diff --git a/examples/todo/migrations/20160720150332_create_tasks_table/up.sql b/examples/todo/migrations/20160720150332_create_tasks_table/up.sql index 6f824598..65948c47 100644 --- a/examples/todo/migrations/20160720150332_create_tasks_table/up.sql +++ b/examples/todo/migrations/20160720150332_create_tasks_table/up.sql @@ -2,4 +2,7 @@ CREATE TABLE tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, description VARCHAR NOT NULL, completed BOOLEAN NOT NULL DEFAULT 0 -) +); + +INSERT INTO tasks (description) VALUES ("demo task"); +INSERT INTO tasks (description) VALUES ("demo task2"); diff --git a/examples/todo/src/db.rs b/examples/todo/src/db.rs index 6f9a82a9..d00086ef 100644 --- a/examples/todo/src/db.rs +++ b/examples/todo/src/db.rs @@ -10,12 +10,11 @@ use rocket::{Request, State, Outcome}; pub type Pool = r2d2::Pool>; -pub const DATABASE_FILE: &'static str = env!("DATABASE_URL"); +pub const DATABASE_URL: &'static str = env!("DATABASE_URL"); pub fn init_pool() -> Pool { - let config = r2d2::Config::default(); - let manager = ConnectionManager::::new(DATABASE_FILE); - r2d2::Pool::new(config, manager).expect("db pool") + let manager = ConnectionManager::::new(DATABASE_URL); + r2d2::Pool::new(manager).expect("db pool") } pub struct Conn(pub r2d2::PooledConnection>); diff --git a/examples/todo/src/main.rs b/examples/todo/src/main.rs index 2a082108..83b86124 100644 --- a/examples/todo/src/main.rs +++ b/examples/todo/src/main.rs @@ -3,7 +3,7 @@ extern crate rocket; #[macro_use] extern crate diesel; -#[macro_use] extern crate diesel_codegen; +#[macro_use] extern crate diesel_derives; #[macro_use] extern crate serde_derive; extern crate rocket_contrib; extern crate r2d2; diff --git a/examples/todo/src/task.rs b/examples/todo/src/task.rs index 6f29623d..d86fc0a0 100644 --- a/examples/todo/src/task.rs +++ b/examples/todo/src/task.rs @@ -2,14 +2,20 @@ use diesel; use diesel::prelude::*; use diesel::sqlite::SqliteConnection; +mod schema { + table! { + tasks { + id -> Nullable, + description -> Text, + completed -> Bool, + } + } +} + use self::schema::tasks; use self::schema::tasks::dsl::{tasks as all_tasks, completed as task_completed}; -mod schema { - infer_schema!("env:DATABASE_URL"); -} - -#[table_name = "tasks"] +#[table_name="tasks"] #[derive(Serialize, Queryable, Insertable, Debug, Clone)] pub struct Task { pub id: Option, @@ -29,7 +35,7 @@ impl Task { pub fn insert(todo: Todo, conn: &SqliteConnection) -> bool { let t = Task { id: None, description: todo.description, completed: false }; - diesel::insert(&t).into(tasks::table).execute(conn).is_ok() + diesel::insert_into(tasks::table).values(&t).execute(conn).is_ok() } pub fn toggle_with_id(id: i32, conn: &SqliteConnection) -> bool { @@ -47,4 +53,3 @@ impl Task { diesel::delete(all_tasks.find(id)).execute(conn).is_ok() } } - diff --git a/site/guide/state.md b/site/guide/state.md index 6facc3b7..c50be991 100644 --- a/site/guide/state.md +++ b/site/guide/state.md @@ -166,7 +166,7 @@ fn handler(conn: DbConn) { ... } ``` [`diesel`]: http://diesel.rs/ -[`r2d2`]: https://docs.rs/r2d2/0.7.2/r2d2/ +[`r2d2`]: https://docs.rs/r2d2/ ### Dependencies @@ -178,20 +178,17 @@ use the following dependencies: ``` [dependencies] rocket = "0.3.5" -diesel = { version = "*", features = ["sqlite"] } -diesel_codegen = { version = "*", features = ["sqlite"] } -r2d2-diesel = "*" -r2d2 = "*" +diesel = { version = "1.0.0-rc1", features = ["sqlite"] } +r2d2-diesel = "1.0.0-rc1" +r2d2 = "0.8" ``` -Your `diesel` dependency information will differ. In particular, you should -specify the latest versions of these libraries as opposed to using a `*`. The -crates are imported as well: +Your `diesel` dependency information may differ. The crates are imported as +well: ```rust extern crate rocket; #[macro_use] extern crate diesel; -#[macro_use] extern crate diesel_codegen; extern crate r2d2_diesel; extern crate r2d2; ``` @@ -219,9 +216,8 @@ static DATABASE_URL: &'static str = env!("DATABASE_URL"); /// Initializes a database pool. fn init_pool() -> Pool { - let config = r2d2::Config::default(); let manager = ConnectionManager::::new(DATABASE_URL); - r2d2::Pool::new(config, manager).expect("db pool") + r2d2::Pool::new(manager).expect("db pool") } ```