From c6378ca77f08a8aaf30fa25c4c02d151bf69edfa Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Sun, 19 Feb 2023 02:11:17 +0000 Subject: create initial database migration --- src/main.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 1850b01..8ad4bf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,41 @@ +use rocket::fairing::{self, AdHoc}; use rocket::fs::{relative, FileServer}; use rocket::http::Status; -use rocket::Request; use rocket::State; +use rocket::{Build, Request, Rocket}; +use rocket_db_pools::sqlx::{self, database}; +use rocket_db_pools::{Connection, Database}; use rocket_dyn_templates::{context, Template}; use std::borrow::Cow; mod scrobbles; mod skweets; +#[derive(Database)] +#[database("blossom")] +struct Blossom(sqlx::SqlitePool); + +async fn run_migrations(rocket: Rocket) -> fairing::Result { + match Blossom::fetch(&rocket) { + Some(db) => match sqlx::migrate!().run(&**db).await { + Ok(_) => Ok(rocket), + Err(e) => { + error!("failed to init blossom database: {}", e); + Err(rocket) + } + }, + None => Err(rocket), + } +} + +fn stage() -> AdHoc { + AdHoc::on_ignite("blossom database stage", |rocket| async { + rocket + .attach(Blossom::init()) + .attach(AdHoc::try_on_ignite("database migrations", run_migrations)) + }) +} + struct Clients { listenbrainz: listenbrainz::raw::Client, skinnyverse: mastodon_async::Mastodon, @@ -62,6 +90,7 @@ async fn main() -> Result<(), rocket::Error> { skinny_data.base = Cow::from("https://skinnyver.se"); let _rocket = rocket::build() + .attach(stage()) .manage(Clients { listenbrainz: listenbrainz::raw::Client::new(), skinnyverse: mastodon_async::Mastodon::from(skinny_data), -- cgit