refactor db
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 7m4s
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 7m4s
This commit is contained in:
parent
993395c208
commit
8a40930cde
22
src/database.rs
Normal file
22
src/database.rs
Normal 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
|
||||
}
|
||||
17
src/main.rs
17
src/main.rs
@ -1,23 +1,12 @@
|
||||
use std::process::exit;
|
||||
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use crate::database::init_database;
|
||||
|
||||
mod config;
|
||||
mod database;
|
||||
mod logging;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cfg = config::Config::load();
|
||||
let _logging_guard = logging::LoggerConfig::init(cfg.app_env);
|
||||
let db = match PgPoolOptions::new().connect(&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"),
|
||||
}
|
||||
let db = init_database(&cfg.db_url).await;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user