refactor db
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 7m4s

This commit is contained in:
Dmitri 2026-04-24 21:11:29 +02:00
parent 993395c208
commit 8a40930cde
Signed by: kanopo
GPG Key ID: 759ADD40E3132AC7
2 changed files with 25 additions and 14 deletions

22
src/database.rs Normal file
View File

@ -0,0 +1,22 @@
use std::process::exit;
use sqlx::{Pool, Postgres, postgres::PgPoolOptions};
pub async fn init_database(db_url: &str) -> Pool<Postgres> {
let db = match PgPoolOptions::new().connect(db_url).await {
Ok(p) => p,
Err(_) => {
tracing::error!("Failed to connect to the database");
exit(1);
}
};
match sqlx::migrate!().run(&db).await {
Ok(_) => tracing::info!("Migration completed succesfully"),
Err(_) => {
tracing::error!("Failed to apply migrations");
exit(1)
}
}
db
}

View File

@ -1,23 +1,12 @@
use std::process::exit; use crate::database::init_database;
use sqlx::postgres::PgPoolOptions;
mod config; mod config;
mod database;
mod logging; mod logging;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let cfg = config::Config::load(); let cfg = config::Config::load();
let _logging_guard = logging::LoggerConfig::init(cfg.app_env); let _logging_guard = logging::LoggerConfig::init(cfg.app_env);
let db = match PgPoolOptions::new().connect(&cfg.db_url).await { let db = init_database(&cfg.db_url).await;
Ok(p) => p,
Err(_) => {
tracing::error!("Failed to connect to the database");
exit(1);
}
};
match sqlx::migrate!().run(&db).await {
Ok(_) => tracing::info!("Migration completed succesfully"),
Err(_) => tracing::error!("Failed to apply migrations"),
}
} }