added migrations

This commit is contained in:
Artemis 2025-01-23 22:32:47 +01:00
parent 9b50c37bea
commit 6933f7df1a
6 changed files with 30 additions and 3 deletions

View file

@ -11,6 +11,7 @@ sqlx = { version = "0.7", default-features = false, features = [
"sqlite",
"macros",
"chrono",
"migrate",
] }
chrono = { version = "0.4", features = ["serde"] }
regex = "1.11"

5
build.rs Normal file
View file

@ -0,0 +1,5 @@
// generated by `sqlx migrate build-script`
fn main() {
// trigger recompilation when a new migration is added
println!("cargo:rerun-if-changed=migrations");
}

19
src/db/migrate.rs Normal file
View file

@ -0,0 +1,19 @@
use rocket::{fairing, Build, Rocket};
use rocket_db_pools::Database;
use super::schema::DollTags;
pub async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
let db = match DollTags::fetch(&rocket) {
Some(v) => v,
None => return Err(rocket),
};
match sqlx::migrate!().run(&**db).await {
Ok(_) => Ok(rocket),
Err(e) => {
error!("Failed to update database\n{:?}", e);
Err(rocket)
}
}
}

View file

@ -1,2 +1,3 @@
pub mod doll;
pub mod migrate;
pub mod schema;

View file

@ -3,9 +3,11 @@ extern crate rocket;
use std::collections::HashMap;
use db::doll::{self, check_ids};
use db::migrate::run_migrations;
use db::schema::{CreateDollProfile, DollTags, DollTagsDb};
use regex::Regex;
use rng::generate_ids;
use rocket::fairing::AdHoc;
use rocket::form::Form;
use rocket::fs::{relative, FileServer};
use rocket::response::Redirect;
@ -152,9 +154,7 @@ async fn show_profile(
ident: Option<&str>,
microchip_id: Option<&str>,
) -> Result<Template, Redirect> {
let internal_id = ident
.and_then(|v| id_public_to_db(v))
.unwrap_or(0);
let internal_id = ident.and_then(|v| id_public_to_db(v)).unwrap_or(0);
let microchip_id = microchip_id.unwrap_or("");
println!("id: {}\nmicrochip id: {}", internal_id, microchip_id);
@ -191,6 +191,7 @@ fn rocket() -> _ {
});
}))
.attach(DollTags::init())
.attach(AdHoc::try_on_ignite("SQLx migrations", run_migrations))
.mount("/assets", FileServer::from(relative!("/assets")))
.mount(
"/",