Database migration are now automatically applied
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -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(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user