Migrations should now run on startup

master^2
Jef Roosens 2021-04-17 14:43:24 +02:00
parent d43a34a5d6
commit 65c3d616de
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 23 additions and 1 deletions

View File

@ -12,3 +12,6 @@
# Config file # Config file
!Rocket.toml !Rocket.toml
# Database migrations
!migrations/

View File

@ -28,6 +28,7 @@ chrono-tz = "0.5.3"
regex = "1.4.5" regex = "1.4.5"
reqwest = { version = "0.11.2", features = ["blocking", "json", "cookies"] } reqwest = { version = "0.11.2", features = ["blocking", "json", "cookies"] }
diesel = { version = "1.4.6", features = ["postgres"] } diesel = { version = "1.4.6", features = ["postgres"] }
diesel_migrations = "1.4.0"
[dependencies.rocket_contrib] [dependencies.rocket_contrib]
version = "0.4.7" version = "0.4.7"

View File

@ -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 source code over to builder
COPY --chown=builder:builder Cargo.toml Cargo.lock ./ COPY --chown=builder:builder Cargo.toml Cargo.lock ./
COPY --chown=builder:builder src/ ./src/ COPY --chown=builder:builder src/ ./src/
COPY --chown=builder:builder migrations/ ./migrations/

View File

@ -4,6 +4,8 @@
extern crate rocket; extern crate rocket;
#[macro_use] #[macro_use]
extern crate rocket_contrib; extern crate rocket_contrib;
#[macro_use]
extern crate diesel_migrations;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -13,9 +15,10 @@ mod routes;
// Very temporary solution for CORS // Very temporary solution for CORS
// https://stackoverflow.com/questions/62412361/how-to-set-up-cors-or-options-for-rocket-rs // 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::fairing::{Fairing, Info, Kind};
use rocket::http::Header; use rocket::http::Header;
use rocket::{Request, Response}; use rocket::{Request, Response, Rocket};
use rocket_contrib::databases::diesel; use rocket_contrib::databases::diesel;
pub struct CORS; 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 // This defines a connection to the database
#[database("postgres_fej")] #[database("postgres_fej")]
struct FejDbConn(diesel::PgConnection); 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 { fn rocket() -> rocket::Rocket {
rocket::ignite() rocket::ignite()
.attach(CORS) .attach(CORS)
.attach(FejDbConn::fairing()) .attach(FejDbConn::fairing())
.attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
.mount("/ivago", routes::ivago()) .mount("/ivago", routes::ivago())
.register(catchers![catchers::not_found]) .register(catchers![catchers::not_found])
} }