Migrations should now run on startup
This commit is contained in:
parent
d43a34a5d6
commit
65c3d616de
4 changed files with 23 additions and 1 deletions
|
|
@ -4,6 +4,8 @@
|
|||
extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate rocket_contrib;
|
||||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
@ -13,9 +15,10 @@ mod routes;
|
|||
|
||||
// Very temporary solution for CORS
|
||||
// https://stackoverflow.com/questions/62412361/how-to-set-up-cors-or-options-for-rocket-rs
|
||||
use rocket::fairing::AdHoc;
|
||||
use rocket::fairing::{Fairing, Info, Kind};
|
||||
use rocket::http::Header;
|
||||
use rocket::{Request, Response};
|
||||
use rocket::{Request, Response, Rocket};
|
||||
use rocket_contrib::databases::diesel;
|
||||
|
||||
pub struct CORS;
|
||||
|
|
@ -39,14 +42,28 @@ impl Fairing for CORS {
|
|||
}
|
||||
}
|
||||
|
||||
// Macro from diesel_migrations that sets up migrations
|
||||
embed_migrations!();
|
||||
|
||||
// This defines a connection to the database
|
||||
#[database("postgres_fej")]
|
||||
struct FejDbConn(diesel::PgConnection);
|
||||
|
||||
// I'd like to thank Stackoverflow for helping me with this
|
||||
// https://stackoverflow.com/questions/61047355/how-to-run-diesel-migration-with-rocket-in-production
|
||||
fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
|
||||
let conn = FejDbConn::get_one(&rocket).expect("database connection");
|
||||
match embedded_migrations::run(&*conn) {
|
||||
Ok(()) => Ok(rocket),
|
||||
Err(e) => Err(rocket),
|
||||
}
|
||||
}
|
||||
|
||||
fn rocket() -> rocket::Rocket {
|
||||
rocket::ignite()
|
||||
.attach(CORS)
|
||||
.attach(FejDbConn::fairing())
|
||||
.attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
|
||||
.mount("/ivago", routes::ivago())
|
||||
.register(catchers![catchers::not_found])
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue