From ed04cc97b3bed9643e72eddc975ca178b7c97602 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Sep 2022 23:33:09 +0100 Subject: [PATCH] update diesel to v2 --- Cargo.lock | 30 ++++++++++++++----- Cargo.toml | 5 +--- auth/simple-auth-server/Cargo.toml | 4 +-- auth/simple-auth-server/src/auth_handler.rs | 9 ++++-- .../src/invitation_handler.rs | 9 +++--- auth/simple-auth-server/src/models.rs | 4 +-- .../src/register_handler.rs | 18 ++++++----- auth/simple-auth-server/src/schema.rs | 18 +++++------ databases/diesel/Cargo.toml | 4 +-- databases/diesel/src/actions.rs | 7 ++--- databases/diesel/src/main.rs | 10 +++---- 11 files changed, 68 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c5c366..ec12bb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2273,25 +2273,42 @@ checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" [[package]] name = "diesel" -version = "1.4.8" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d" +checksum = "01e2adfd0a7a81070ed7beec0c62636458926326c16fedb77796d41e447b282d" dependencies = [ "bitflags", "byteorder", "chrono", "diesel_derives", + "itoa 1.0.3", + "libsqlite3-sys", "pq-sys", "r2d2", - "uuid 0.8.2", + "uuid 1.1.2", +] + +[[package]] +name = "diesel-example" +version = "1.0.0" +dependencies = [ + "actix-web", + "diesel", + "dotenv", + "env_logger 0.9.0", + "log", + "serde", + "serde_json", + "uuid 1.1.2", ] [[package]] name = "diesel_derives" -version = "1.4.1" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" +checksum = "22a7ab9d7967e6a1a247ea38aedf88ab808b4ac0c159576bc71866ab8f9f9250" dependencies = [ + "proc-macro-error", "proc-macro2", "quote", "syn", @@ -5769,7 +5786,7 @@ dependencies = [ "serde_json", "sparkpost", "time 0.3.14", - "uuid 0.8.2", + "uuid 1.1.2", ] [[package]] @@ -7082,7 +7099,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" dependencies = [ "getrandom 0.2.7", - "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b7bf64a..d99bdf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ members = [ "basics/todo", "cors/backend", "data-factory", + "databases/diesel", "databases/mongodb", "databases/postgres", "databases/redis", @@ -60,7 +61,3 @@ members = [ "websockets/echo-actorless", "websockets/echo", ] -exclude = [ - # uses incompatible libsqlite-sys to other examples - "databases/diesel", -] diff --git a/auth/simple-auth-server/Cargo.toml b/auth/simple-auth-server/Cargo.toml index 5cef185..b795d31 100644 --- a/auth/simple-auth-server/Cargo.toml +++ b/auth/simple-auth-server/Cargo.toml @@ -9,7 +9,7 @@ actix-identity = "0.4" chrono = { version = "0.4", features = ["serde"] } derive_more = "0.99.5" -diesel = { version = "1.4", features = ["postgres", "uuidv07", "r2d2", "chrono"] } +diesel = { version = "2", features = ["postgres", "r2d2", "uuid", "chrono"] } dotenv = "0.15" env_logger = "0.9" r2d2 = "0.8" @@ -18,5 +18,5 @@ lazy_static = "1.4" serde = { version = "1", features = ["derive"] } serde_json = "1" sparkpost = "0.5" -uuid = { version = "0.8", features = ["v4", "serde"] } +uuid = { version = "1", features = ["v4", "serde"] } time = "0.3" diff --git a/auth/simple-auth-server/src/auth_handler.rs b/auth/simple-auth-server/src/auth_handler.rs index b0d3f84..43149be 100644 --- a/auth/simple-auth-server/src/auth_handler.rs +++ b/auth/simple-auth-server/src/auth_handler.rs @@ -2,7 +2,7 @@ use std::future::{ready, Ready}; use actix_identity::Identity; use actix_web::{dev::Payload, web, Error, FromRequest, HttpRequest, HttpResponse}; -use diesel::{prelude::*, PgConnection}; +use diesel::prelude::*; use serde::Deserialize; use crate::{ @@ -52,6 +52,7 @@ pub async fn login( let user_string = serde_json::to_string(&user).unwrap(); id.remember(user_string); + Ok(HttpResponse::Ok().finish()) } @@ -61,10 +62,12 @@ pub async fn get_me(logged_user: LoggedUser) -> HttpResponse { /// Diesel query fn query(auth_data: AuthData, pool: web::Data) -> Result { use crate::schema::users::dsl::{email, users}; - let conn: &PgConnection = &pool.get().unwrap(); + + let mut conn = pool.get().unwrap(); + let mut items = users .filter(email.eq(&auth_data.email)) - .load::(conn)?; + .load::(&mut conn)?; if let Some(user) = items.pop() { if let Ok(matching) = verify(&user.hash, &auth_data.password) { diff --git a/auth/simple-auth-server/src/invitation_handler.rs b/auth/simple-auth-server/src/invitation_handler.rs index dc94a9e..2017ec5 100644 --- a/auth/simple-auth-server/src/invitation_handler.rs +++ b/auth/simple-auth-server/src/invitation_handler.rs @@ -1,5 +1,5 @@ use actix_web::{web, HttpResponse}; -use diesel::{prelude::*, PgConnection}; +use diesel::prelude::*; use serde::Deserialize; use crate::{ @@ -34,12 +34,13 @@ fn create_invitation( fn query(eml: String, pool: web::Data) -> Result { use crate::schema::invitations::dsl::invitations; - let new_invitation: Invitation = eml.into(); - let conn: &PgConnection = &pool.get().unwrap(); + let mut conn = pool.get().unwrap(); + + let new_invitation = Invitation::from(eml); let inserted_invitation = diesel::insert_into(invitations) .values(&new_invitation) - .get_result(conn)?; + .get_result(&mut conn)?; Ok(inserted_invitation) } diff --git a/auth/simple-auth-server/src/models.rs b/auth/simple-auth-server/src/models.rs index 0778f8e..e4514b7 100644 --- a/auth/simple-auth-server/src/models.rs +++ b/auth/simple-auth-server/src/models.rs @@ -9,7 +9,7 @@ use super::schema::*; pub type Pool = r2d2::Pool>; #[derive(Debug, Serialize, Deserialize, Queryable, Insertable)] -#[table_name = "users"] +#[diesel(table_name = users)] pub struct User { pub email: String, pub hash: String, @@ -27,7 +27,7 @@ impl User { } #[derive(Debug, Serialize, Deserialize, Queryable, Insertable)] -#[table_name = "invitations"] +#[diesel(table_name = invitations)] pub struct Invitation { pub id: uuid::Uuid, pub email: String, diff --git a/auth/simple-auth-server/src/register_handler.rs b/auth/simple-auth-server/src/register_handler.rs index 7c4086b..bdcceae 100644 --- a/auth/simple-auth-server/src/register_handler.rs +++ b/auth/simple-auth-server/src/register_handler.rs @@ -36,16 +36,15 @@ fn query( password: String, pool: web::Data, ) -> Result { - use crate::schema::{ - invitations::dsl::{id, invitations}, - users::dsl::users, - }; + use crate::schema::{invitations::dsl::*, users::dsl::*}; + + let mut conn = pool.get().unwrap(); + let invitation_id = uuid::Uuid::parse_str(&invitation_id)?; - let conn: &PgConnection = &pool.get().unwrap(); invitations .filter(id.eq(invitation_id)) - .load::(conn) + .load::(&mut conn) .map_err(|_db_error| ServiceError::BadRequest("Invalid Invitation".into())) .and_then(|mut result| { if let Some(invitation) = result.pop() { @@ -54,10 +53,13 @@ fn query( // try hashing the password, else return the error that will be converted to ServiceError let password: String = hash_password(&password)?; dbg!(&password); + let user = User::from_details(invitation.email, password); - let inserted_user: User = - diesel::insert_into(users).values(&user).get_result(conn)?; + let inserted_user: User = diesel::insert_into(users) + .values(&user) + .get_result(&mut conn)?; dbg!(&inserted_user); + return Ok(inserted_user.into()); } } diff --git a/auth/simple-auth-server/src/schema.rs b/auth/simple-auth-server/src/schema.rs index 8c48962..30edad2 100644 --- a/auth/simple-auth-server/src/schema.rs +++ b/auth/simple-auth-server/src/schema.rs @@ -1,11 +1,3 @@ -table! { - invitations (id) { - id -> Uuid, - email -> Varchar, - expires_at -> Timestamp, - } -} - table! { users (email) { email -> Varchar, @@ -14,4 +6,12 @@ table! { } } -allow_tables_to_appear_in_same_query!(invitations, users,); +table! { + invitations (id) { + id -> Uuid, + email -> Varchar, + expires_at -> Timestamp, + } +} + +allow_tables_to_appear_in_same_query!(users, invitations); diff --git a/databases/diesel/Cargo.toml b/databases/diesel/Cargo.toml index a050b00..8a8dab4 100644 --- a/databases/diesel/Cargo.toml +++ b/databases/diesel/Cargo.toml @@ -5,10 +5,10 @@ edition = "2021" [dependencies] actix-web = "4" -diesel = { version = "1.4", features = ["sqlite", "r2d2"] } +diesel = { version = "2", features = ["sqlite", "r2d2"] } dotenv = "0.15" env_logger = "0.9" log = "0.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -uuid = { version = "0.8", features = ["serde", "v4"] } +uuid = { version = "1", features = ["v4", "serde"] } diff --git a/databases/diesel/src/actions.rs b/databases/diesel/src/actions.rs index 010cf6d..0cddc94 100644 --- a/databases/diesel/src/actions.rs +++ b/databases/diesel/src/actions.rs @@ -7,8 +7,8 @@ type DbError = Box; /// Run query using Diesel to find user by uid and return it. pub fn find_user_by_uid( + conn: &mut SqliteConnection, uid: Uuid, - conn: &SqliteConnection, ) -> Result, DbError> { use crate::schema::users::dsl::*; @@ -22,9 +22,8 @@ pub fn find_user_by_uid( /// Run query using Diesel to insert a new database row and return the result. pub fn insert_new_user( - // prevent collision with `name` column imported inside the function - nm: &str, - conn: &SqliteConnection, + conn: &mut SqliteConnection, + nm: &str, // prevent collision with `name` column imported inside the function ) -> Result { // It is common when using Diesel with Actix Web to import schema-related // modules inside a function's scope (rather than the normal module's scope) diff --git a/databases/diesel/src/main.rs b/databases/diesel/src/main.rs index 62c68e3..28edd0e 100644 --- a/databases/diesel/src/main.rs +++ b/databases/diesel/src/main.rs @@ -27,8 +27,8 @@ async fn get_user( // use web::block to offload blocking Diesel code without blocking server thread let user = web::block(move || { - let conn = pool.get()?; - actions::find_user_by_uid(user_uid, &conn) + let mut conn = pool.get()?; + actions::find_user_by_uid(&mut conn, user_uid) }) .await? .map_err(actix_web::error::ErrorInternalServerError)?; @@ -49,8 +49,8 @@ async fn add_user( ) -> Result { // use web::block to offload blocking Diesel code without blocking server thread let user = web::block(move || { - let conn = pool.get()?; - actions::insert_new_user(&form.name, &conn) + let mut conn = pool.get()?; + actions::insert_new_user(&mut conn, &form.name) }) .await? .map_err(actix_web::error::ErrorInternalServerError)?; @@ -136,7 +136,7 @@ mod tests { // Delete new user from table use crate::schema::users::dsl::*; diesel::delete(users.filter(id.eq(resp.id))) - .execute(&pool.get().expect("couldn't get db connection from pool")) + .execute(&mut pool.get().expect("couldn't get db connection from pool")) .expect("couldn't delete test user from table"); } }