Migrations should now run on startup
parent
d43a34a5d6
commit
65c3d616de
|
@ -12,3 +12,6 @@
|
|||
|
||||
# Config file
|
||||
!Rocket.toml
|
||||
|
||||
# Database migrations
|
||||
!migrations/
|
||||
|
|
|
@ -28,6 +28,7 @@ chrono-tz = "0.5.3"
|
|||
regex = "1.4.5"
|
||||
reqwest = { version = "0.11.2", features = ["blocking", "json", "cookies"] }
|
||||
diesel = { version = "1.4.6", features = ["postgres"] }
|
||||
diesel_migrations = "1.4.0"
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
version = "0.4.7"
|
||||
|
|
|
@ -33,3 +33,4 @@ RUN { curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --
|
|||
# Copy source code over to builder
|
||||
COPY --chown=builder:builder Cargo.toml Cargo.lock ./
|
||||
COPY --chown=builder:builder src/ ./src/
|
||||
COPY --chown=builder:builder migrations/ ./migrations/
|
||||
|
|
|
@ -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])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue