Run DB migrations at startup
Naturally, we need the database schema in place in order to use it.bugfix/ci-buildah
parent
da9d336817
commit
e4ddfbd025
|
@ -2,5 +2,6 @@
|
||||||
!Cargo.*
|
!Cargo.*
|
||||||
!.sqlx/
|
!.sqlx/
|
||||||
!js/
|
!js/
|
||||||
|
!migrations/
|
||||||
!src/
|
!src/
|
||||||
!templates/
|
!templates/
|
||||||
|
|
|
@ -17,7 +17,7 @@ rocket_db_pools = { version = "0.2.0", features = ["sqlx_macros", "sqlx_postgres
|
||||||
rocket_dyn_templates = { version = "0.2.0", features = ["tera"] }
|
rocket_dyn_templates = { version = "0.2.0", features = ["tera"] }
|
||||||
rust_decimal = { version = "1.36.0", features = ["serde-with-str"] }
|
rust_decimal = { version = "1.36.0", features = ["serde-with-str"] }
|
||||||
serde = { version = "1.0.218", default-features = false, features = ["derive"] }
|
serde = { version = "1.0.218", default-features = false, features = ["derive"] }
|
||||||
sqlx = { version = "~0.7.4", default-features = false, features = ["chrono", "macros", "postgres", "rust_decimal", "time"] }
|
sqlx = { version = "~0.7.4", default-features = false, features = ["chrono", "macros", "migrate", "postgres", "rust_decimal", "time"] }
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
tracing = "0.1.41"
|
tracing = "0.1.41"
|
||||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||||
|
|
|
@ -13,6 +13,7 @@ WORKDIR /build
|
||||||
COPY Cargo.* .
|
COPY Cargo.* .
|
||||||
COPY src src
|
COPY src src
|
||||||
COPY .sqlx .sqlx
|
COPY .sqlx .sqlx
|
||||||
|
COPY migrations migrations
|
||||||
|
|
||||||
RUN --mount=type=cache,target=/root/.cargo \
|
RUN --mount=type=cache,target=/root/.cargo \
|
||||||
cargo build --release --locked
|
cargo build --release --locked
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -2,16 +2,17 @@ mod config;
|
||||||
mod firefly;
|
mod firefly;
|
||||||
mod receipts;
|
mod receipts;
|
||||||
|
|
||||||
|
use rocket::fairing::{self, AdHoc};
|
||||||
use rocket::form::Form;
|
use rocket::form::Form;
|
||||||
use rocket::fs::{FileServer, TempFile};
|
use rocket::fs::{FileServer, TempFile};
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket::tokio::io::{AsyncReadExt, BufReader};
|
use rocket::tokio::io::{AsyncReadExt, BufReader};
|
||||||
use rocket::State;
|
use rocket::{Rocket, State};
|
||||||
use rocket_db_pools::Database as RocketDatabase;
|
use rocket_db_pools::Database as RocketDatabase;
|
||||||
use rocket_dyn_templates::{context, Template};
|
use rocket_dyn_templates::{context, Template};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use firefly::{
|
use firefly::{
|
||||||
|
@ -229,6 +230,20 @@ async fn update_transaction(
|
||||||
(Status::Ok, "Successfully updated transaction".into())
|
(Status::Ok, "Successfully updated transaction".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn run_migrations(rocket: Rocket<rocket::Build>) -> fairing::Result {
|
||||||
|
if let Some(db) = Database::fetch(&rocket) {
|
||||||
|
info!("Applying database migrations");
|
||||||
|
if let Err(e) = sqlx::migrate!("./migrations").run(&db.0).await {
|
||||||
|
error!("Database migration failed: {}", e);
|
||||||
|
Err(rocket)
|
||||||
|
} else {
|
||||||
|
Ok(rocket)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(rocket)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[rocket::launch]
|
#[rocket::launch]
|
||||||
async fn rocket() -> _ {
|
async fn rocket() -> _ {
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
|
@ -265,4 +280,5 @@ async fn rocket() -> _ {
|
||||||
.mount("/static", FileServer::from("static"))
|
.mount("/static", FileServer::from("static"))
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.attach(Database::init())
|
.attach(Database::init())
|
||||||
|
.attach(AdHoc::try_on_ignite("Migrate Database", run_migrations))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue