This commit is contained in:
parent
569e9c935b
commit
424e723cd8
3 changed files with 40 additions and 16 deletions
22
src/main.rs
22
src/main.rs
|
|
@ -4,7 +4,29 @@ extern crate rocket;
|
|||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
|
||||
use rocket_sync_db_pools::{diesel, database};
|
||||
use rocket::{
|
||||
Rocket,
|
||||
Build,
|
||||
fairing::AdHoc
|
||||
};
|
||||
|
||||
embed_migrations!();
|
||||
|
||||
#[database("postgres_hilde")]
|
||||
struct HildeDbConn(diesel::PgConnection);
|
||||
|
||||
async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> {
|
||||
let conn = HildeDbConn::get_one(&rocket).await.expect("database connection");
|
||||
conn.run(|c| match embedded_migrations::run(c) {
|
||||
Ok(()) => Ok(rocket),
|
||||
Err(_) => Err(rocket),
|
||||
}).await
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
rocket::build()
|
||||
.attach(HildeDbConn::fairing())
|
||||
.attach(AdHoc::try_on_ignite("Run database migrations", run_db_migrations))
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue