S
Shuttle13mo ago
Dohman

Integrating Diesel into Rocket 0.5

@womble73 I created a very basic shuttle instance based off the documentation. I have been distracted on another project, but I really struggled to understand how I would integrate diesel into the mix as Rocket 0.5 has moved over to using stages as preferred methods of loading databases etc. So in my example the stage for loading postgres looks like this:
use rocket::{fairing::AdHoc, Rocket, Build};

use rocket_sync_db_pools::diesel;
use crate::diesel_migrations::{MigrationHarness, EmbeddedMigrations};

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

#[database("db")]
pub struct Db(diesel::PgConnection);

async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
let conn = Db::get_one(&rocket).await.expect("database connection");
conn.run(|c| { c.run_pending_migrations(MIGRATIONS).unwrap(); }).await;
rocket
}

pub(crate) fn stage() -> AdHoc {
AdHoc::on_ignite("Diesel Postgres Stage", |rocket| async {
rocket.attach(Db::fairing())
.attach(AdHoc::on_ignite("Diesel Migrations", run_migrations))
})
}
use rocket::{fairing::AdHoc, Rocket, Build};

use rocket_sync_db_pools::diesel;
use crate::diesel_migrations::{MigrationHarness, EmbeddedMigrations};

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

#[database("db")]
pub struct Db(diesel::PgConnection);

async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
let conn = Db::get_one(&rocket).await.expect("database connection");
conn.run(|c| { c.run_pending_migrations(MIGRATIONS).unwrap(); }).await;
rocket
}

pub(crate) fn stage() -> AdHoc {
AdHoc::on_ignite("Diesel Postgres Stage", |rocket| async {
rocket.attach(Db::fairing())
.attach(AdHoc::on_ignite("Diesel Migrations", run_migrations))
})
}
2 Replies
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Dohman
Dohman13mo ago
Let us know how it goes!
Want results from more Discord servers?
Add your server