FabulaVotes/src/database.rs
trotFunky 9911895b5b v1.0: First production version
This first version allows login of pre-existing users, creation and update of truths
by admins, vote on the truths by users, their display as well as a simple graph
for the vote results.
Everything persisting in a SQLite database.
2024-07-23 21:51:42 +01:00

26 lines
759 B
Rust

use rocket::{Build, fairing, Rocket};
use rocket::fairing::AdHoc;
use rocket_db_pools::Database;
#[derive(Database)]
#[database("data_sqlite")]
pub struct Db(sqlx::SqlitePool);
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
match Db::fetch(&rocket) {
Some(db) => match sqlx::migrate!("./db").run(&**db).await {
Ok(_) => Ok(rocket),
Err(e) => {
error!("Failed to initialize SQLx database: {}", e);
Err(rocket)
}
}
None => Err(rocket),
}
}
pub fn stage() -> AdHoc {
AdHoc::on_ignite("SQLx Stage", |rocket| async {
rocket.attach(Db::init())
.attach(AdHoc::try_on_ignite("SQLx Migrations", run_migrations))
})
}