Integrating Diesel into Rocket 0.5
# help
g
@User 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:
Copy code
rust
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))
    })
}
a
Heya 🙂 you could provision a db by calling it using SQLx, then get the conn string from deployment then apply migrations manually but it's quite hacky
t
Thanks, I will give it a go. I really liked the way Rocket and Diesel played together. If I can get the connection string I can place it straight into an environment variable. So I will play around and see how far I get.