mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
update diesel to v2
This commit is contained in:
@ -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"] }
|
||||
|
@ -7,8 +7,8 @@ type DbError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
/// 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<Option<models::User>, 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<models::User, DbError> {
|
||||
// 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)
|
||||
|
@ -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<HttpResponse, Error> {
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user