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(())
}

View File

@ -4,7 +4,7 @@ use actix_remote_ip::RemoteIPConfig;
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use geneit_backend::app_config::AppConfig;
use geneit_backend::connections::s3_connection;
use geneit_backend::connections::{db_connection, s3_connection};
use geneit_backend::controllers::{
auth_controller, couples_controller, data_controller, families_controller, members_controller,
photos_controller, server_controller, users_controller,
@ -22,6 +22,10 @@ async fn main() -> std::io::Result<()> {
.await
.expect("Failed to initialize S3 bucket!");
// Initialize database connection
log::debug!("Initialize database connection");
db_connection::initialize_conn().expect("Failed to initialize database connection!");
HttpServer::new(|| {
App::new()
.wrap(