Database migration are now automatically applied
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-05-14 21:36:49 +02:00
parent d9a4e3249d
commit f167e24c4f
5 changed files with 100 additions and 4 deletions

View File

@ -3,7 +3,11 @@
use crate::app_config::AppConfig;
use diesel::result::{DatabaseErrorKind, Error};
use diesel::{Connection, PgConnection};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use std::cell::RefCell;
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
thread_local! {
static POSTGRES_CONNECTION: RefCell<Option<PgConnection>> = const { RefCell::new(None) };
}
@ -39,3 +43,20 @@ where
}
}
}
/// Initialize database connection
pub fn initialize_conn() -> anyhow::Result<()> {
// Run pending diesel migrations
execute(|db| {
let res = db
.run_pending_migrations(MIGRATIONS)
.expect("Failed to run database migration!");
for migration in res {
log::info!("Executed database migration {migration}")
}
Ok(())
})?;
Ok(())
}