diff options
author | cel 🌸 <cel@blos.sm> | 2023-02-19 02:11:17 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-02-19 02:11:17 +0000 |
commit | c6378ca77f08a8aaf30fa25c4c02d151bf69edfa (patch) | |
tree | f3d21a7d15f2543c4ccd2eba505a91774833529c /src/main.rs | |
parent | 0b6d17e87106bb514a62d79d13e8465fd3bffc3a (diff) | |
download | blossom-c6378ca77f08a8aaf30fa25c4c02d151bf69edfa.tar.gz blossom-c6378ca77f08a8aaf30fa25c4c02d151bf69edfa.tar.bz2 blossom-c6378ca77f08a8aaf30fa25c4c02d151bf69edfa.zip |
create initial database migration
Diffstat (limited to '')
-rw-r--r-- | src/main.rs | 31 |
1 files changed, 30 insertions, 1 deletions
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<Build>) -> 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), |