Update diesel to 2.0, sqlx to 0.6.

Closes #2209.
Closes #2335.
Closes #2249.
This commit is contained in:
Tilen Pintarič 2022-06-21 16:07:21 +02:00 committed by Sergio Benitez
parent 707936177a
commit f0d678d464
9 changed files with 37 additions and 35 deletions

View File

@ -59,7 +59,7 @@ features = ["tokio-runtime"]
optional = true optional = true
[dependencies.sqlx] [dependencies.sqlx]
version = "0.5" version = "0.6"
default-features = false default-features = false
features = ["runtime-tokio-rustls"] features = ["runtime-tokio-rustls"]
optional = true optional = true

View File

@ -246,7 +246,7 @@ mod sqlx {
sqlx::pool::PoolOptions::new() sqlx::pool::PoolOptions::new()
.max_connections(config.max_connections as u32) .max_connections(config.max_connections as u32)
.connect_timeout(Duration::from_secs(config.connect_timeout)) .acquire_timeout(Duration::from_secs(config.connect_timeout))
.idle_timeout(config.idle_timeout.map(Duration::from_secs)) .idle_timeout(config.idle_timeout.map(Duration::from_secs))
.min_connections(config.min_connections.unwrap_or_default()) .min_connections(config.min_connections.unwrap_or_default())
.connect_with(opts) .connect_with(opts)

View File

@ -23,13 +23,13 @@ r2d2 = "0.8"
tokio = { version = "1.6.1", features = ["rt", "rt-multi-thread"] } tokio = { version = "1.6.1", features = ["rt", "rt-multi-thread"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
diesel = { version = "1.0", default-features = false, optional = true } diesel = { version = "2.0.0", default-features = false, optional = true }
postgres = { version = "0.19", optional = true } postgres = { version = "0.19", optional = true }
r2d2_postgres = { version = "0.18", optional = true } r2d2_postgres = { version = "0.18", optional = true }
rusqlite = { version = "0.25", optional = true } rusqlite = { version = "0.27.0", optional = true }
r2d2_sqlite = { version = "0.18", optional = true } r2d2_sqlite = { version = "0.20.0", optional = true }
memcache = { version = "0.15", optional = true } memcache = { version = "0.15", optional = true }
r2d2-memcache = { version = "0.6", optional = true } r2d2-memcache = { version = "0.6", optional = true }

View File

@ -7,11 +7,11 @@ publish = false
[dependencies] [dependencies]
rocket = { path = "../../core/lib", features = ["json"] } rocket = { path = "../../core/lib", features = ["json"] }
diesel = { version = "1.3", features = ["sqlite", "r2d2"] } diesel = { version = "2.0.0", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.3" diesel_migrations = "2.0.0"
[dependencies.sqlx] [dependencies.sqlx]
version = "0.5.1" version = "0.6.0"
default-features = false default-features = false
features = ["macros", "offline", "migrate"] features = ["macros", "offline", "migrate"]

View File

@ -14,7 +14,7 @@ type Result<T, E = Debug<diesel::result::Error>> = std::result::Result<T, E>;
#[derive(Debug, Clone, Deserialize, Serialize, Queryable, Insertable)] #[derive(Debug, Clone, Deserialize, Serialize, Queryable, Insertable)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
#[table_name="posts"] #[diesel(table_name = posts)]
struct Post { struct Post {
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
id: Option<i32>, id: Option<i32>,
@ -84,13 +84,14 @@ async fn destroy(db: Db) -> Result<()> {
} }
async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> { async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
// This macro from `diesel_migrations` defines an `embedded_migrations` use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
// module containing a function named `run` that runs the migrations in the
// specified directory, initializing the database.
embed_migrations!("db/diesel/migrations");
let conn = Db::get_one(&rocket).await.expect("database connection"); const MIGRATIONS: EmbeddedMigrations = embed_migrations!("db/diesel/migrations");
conn.run(|c| embedded_migrations::run(c)).await.expect("diesel migrations");
Db::get_one(&rocket).await
.expect("database connection")
.run(|conn| { conn.run_pending_migrations(MIGRATIONS).expect("diesel migrations"); })
.await;
rocket rocket
} }

View File

@ -1,7 +1,5 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_sync_db_pools; #[macro_use] extern crate rocket_sync_db_pools;
#[macro_use] extern crate diesel_migrations;
#[macro_use] extern crate diesel;
#[cfg(test)] mod tests; #[cfg(test)] mod tests;

View File

@ -7,8 +7,8 @@ publish = false
[dependencies] [dependencies]
rocket = { path = "../../core/lib" } rocket = { path = "../../core/lib" }
diesel = { version = "1.3", features = ["sqlite", "r2d2"] } diesel = { version = "2.0.0", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.3" diesel_migrations = "2.0.0"
[dev-dependencies] [dev-dependencies]
parking_lot = "0.12" parking_lot = "0.12"

View File

@ -1,7 +1,6 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_migrations;
#[macro_use] extern crate rocket_sync_db_pools; #[macro_use] extern crate rocket_sync_db_pools;
#[macro_use] extern crate diesel;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -93,13 +92,14 @@ async fn index(flash: Option<FlashMessage<'_>>, conn: DbConn) -> Template {
} }
async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> { async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
// This macro from `diesel_migrations` defines an `embedded_migrations` use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
// module containing a function named `run`. This allows the example to be
// run and tested without any outside setup of the database.
embed_migrations!();
let conn = DbConn::get_one(&rocket).await.expect("database connection"); const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
conn.run(|c| embedded_migrations::run(c)).await.expect("can run migrations");
DbConn::get_one(&rocket).await
.expect("database connection")
.run(|conn| { conn.run_pending_migrations(MIGRATIONS).expect("diesel migrations"); })
.await;
rocket rocket
} }

View File

@ -12,14 +12,14 @@ mod schema {
} }
use self::schema::tasks; use self::schema::tasks;
use self::schema::tasks::dsl::{tasks as all_tasks, completed as task_completed};
use crate::DbConn; use crate::DbConn;
#[derive(Serialize, Queryable, Insertable, Debug, Clone)] #[derive(Serialize, Queryable, Insertable, Debug, Clone)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
#[table_name="tasks"] #[diesel(table_name = tasks)]
pub struct Task { pub struct Task {
#[serde(skip_deserializing)]
pub id: Option<i32>, pub id: Option<i32>,
pub description: String, pub description: String,
pub completed: bool pub completed: bool
@ -33,7 +33,7 @@ pub struct Todo {
impl Task { impl Task {
pub async fn all(conn: &DbConn) -> QueryResult<Vec<Task>> { pub async fn all(conn: &DbConn) -> QueryResult<Vec<Task>> {
conn.run(|c| { conn.run(|c| {
all_tasks.order(tasks::id.desc()).load::<Task>(c) tasks::table.order(tasks::id.desc()).load::<Task>(c)
}).await }).await
} }
@ -48,21 +48,24 @@ impl Task {
/// Returns the number of affected rows: 1. /// Returns the number of affected rows: 1.
pub async fn toggle_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> { pub async fn toggle_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> {
conn.run(move |c| { conn.run(move |c| {
let task = all_tasks.find(id).get_result::<Task>(c)?; let task = tasks::table.filter(tasks::id.eq(id)).get_result::<Task>(c)?;
let new_status = !task.completed; let new_status = !task.completed;
let updated_task = diesel::update(all_tasks.find(id)); let updated_task = diesel::update(tasks::table.filter(tasks::id.eq(id)));
updated_task.set(task_completed.eq(new_status)).execute(c) updated_task.set(tasks::completed.eq(new_status)).execute(c)
}).await }).await
} }
/// Returns the number of affected rows: 1. /// Returns the number of affected rows: 1.
pub async fn delete_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> { pub async fn delete_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> {
conn.run(move |c| diesel::delete(all_tasks.find(id)).execute(c)).await conn.run(move |c| diesel::delete(tasks::table)
.filter(tasks::id.eq(id))
.execute(c))
.await
} }
/// Returns the number of affected rows. /// Returns the number of affected rows.
#[cfg(test)] #[cfg(test)]
pub async fn delete_all(conn: &DbConn) -> QueryResult<usize> { pub async fn delete_all(conn: &DbConn) -> QueryResult<usize> {
conn.run(|c| diesel::delete(all_tasks).execute(c)).await conn.run(|c| diesel::delete(tasks::table).execute(c)).await
} }
} }