Skip to content

Commit

Permalink
Update diesel to 2.0, sqlx to 0.6.
Browse files Browse the repository at this point in the history
Closes #2209.
Closes #2335.
Closes #2249.
  • Loading branch information
Tilen Pintarič authored and SergioBenitez committed Sep 18, 2022
1 parent 7079361 commit f0d678d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion contrib/db_pools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ features = ["tokio-runtime"]
optional = true

[dependencies.sqlx]
version = "0.5"
version = "0.6"
default-features = false
features = ["runtime-tokio-rustls"]
optional = true
Expand Down
2 changes: 1 addition & 1 deletion contrib/db_pools/lib/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ mod sqlx {

sqlx::pool::PoolOptions::new()
.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))
.min_connections(config.min_connections.unwrap_or_default())
.connect_with(opts)
Expand Down
6 changes: 3 additions & 3 deletions contrib/sync_db_pools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ r2d2 = "0.8"
tokio = { version = "1.6.1", features = ["rt", "rt-multi-thread"] }
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 }
r2d2_postgres = { version = "0.18", optional = true }

rusqlite = { version = "0.25", optional = true }
r2d2_sqlite = { version = "0.18", optional = true }
rusqlite = { version = "0.27.0", optional = true }
r2d2_sqlite = { version = "0.20.0", optional = true }

memcache = { version = "0.15", optional = true }
r2d2-memcache = { version = "0.6", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions examples/databases/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ publish = false

[dependencies]
rocket = { path = "../../core/lib", features = ["json"] }
diesel = { version = "1.3", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.3"
diesel = { version = "2.0.0", features = ["sqlite", "r2d2"] }
diesel_migrations = "2.0.0"

[dependencies.sqlx]
version = "0.5.1"
version = "0.6.0"
default-features = false
features = ["macros", "offline", "migrate"]

Expand Down
15 changes: 8 additions & 7 deletions examples/databases/src/diesel_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Result<T, E = Debug<diesel::result::Error>> = std::result::Result<T, E>;

#[derive(Debug, Clone, Deserialize, Serialize, Queryable, Insertable)]
#[serde(crate = "rocket::serde")]
#[table_name="posts"]
#[diesel(table_name = posts)]
struct Post {
#[serde(skip_deserializing)]
id: Option<i32>,
Expand Down Expand Up @@ -84,13 +84,14 @@ async fn destroy(db: Db) -> Result<()> {
}

async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
// This macro from `diesel_migrations` defines an `embedded_migrations`
// module containing a function named `run` that runs the migrations in the
// specified directory, initializing the database.
embed_migrations!("db/diesel/migrations");
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};

let conn = Db::get_one(&rocket).await.expect("database connection");
conn.run(|c| embedded_migrations::run(c)).await.expect("diesel migrations");
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("db/diesel/migrations");

Db::get_one(&rocket).await
.expect("database connection")
.run(|conn| { conn.run_pending_migrations(MIGRATIONS).expect("diesel migrations"); })
.await;

rocket
}
Expand Down
2 changes: 0 additions & 2 deletions examples/databases/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_sync_db_pools;
#[macro_use] extern crate diesel_migrations;
#[macro_use] extern crate diesel;

#[cfg(test)] mod tests;

Expand Down
4 changes: 2 additions & 2 deletions examples/todo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ publish = false

[dependencies]
rocket = { path = "../../core/lib" }
diesel = { version = "1.3", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.3"
diesel = { version = "2.0.0", features = ["sqlite", "r2d2"] }
diesel_migrations = "2.0.0"

[dev-dependencies]
parking_lot = "0.12"
Expand Down
16 changes: 8 additions & 8 deletions examples/todo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[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 diesel;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -93,13 +92,14 @@ async fn index(flash: Option<FlashMessage<'_>>, conn: DbConn) -> Template {
}

async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
// This macro from `diesel_migrations` defines an `embedded_migrations`
// module containing a function named `run`. This allows the example to be
// run and tested without any outside setup of the database.
embed_migrations!();
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");

let conn = DbConn::get_one(&rocket).await.expect("database connection");
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
}
Expand Down
19 changes: 11 additions & 8 deletions examples/todo/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ mod schema {
}

use self::schema::tasks;
use self::schema::tasks::dsl::{tasks as all_tasks, completed as task_completed};

use crate::DbConn;

#[derive(Serialize, Queryable, Insertable, Debug, Clone)]
#[serde(crate = "rocket::serde")]
#[table_name="tasks"]
#[diesel(table_name = tasks)]
pub struct Task {
#[serde(skip_deserializing)]
pub id: Option<i32>,
pub description: String,
pub completed: bool
Expand All @@ -33,7 +33,7 @@ pub struct Todo {
impl Task {
pub async fn all(conn: &DbConn) -> QueryResult<Vec<Task>> {
conn.run(|c| {
all_tasks.order(tasks::id.desc()).load::<Task>(c)
tasks::table.order(tasks::id.desc()).load::<Task>(c)
}).await
}

Expand All @@ -48,21 +48,24 @@ impl Task {
/// Returns the number of affected rows: 1.
pub async fn toggle_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> {
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 updated_task = diesel::update(all_tasks.find(id));
updated_task.set(task_completed.eq(new_status)).execute(c)
let updated_task = diesel::update(tasks::table.filter(tasks::id.eq(id)));
updated_task.set(tasks::completed.eq(new_status)).execute(c)
}).await
}

/// Returns the number of affected rows: 1.
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.
#[cfg(test)]
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
}
}

0 comments on commit f0d678d

Please sign in to comment.