feat: sqlx

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-03 21:33:42 +03:00
parent 40149b372d
commit 20af44c357
16 changed files with 135 additions and 194 deletions

View file

@ -5,12 +5,15 @@ pub mod user;
use std::time::Duration;
use sea_orm::{Database, DatabaseConnection};
use sqlx::{postgres::PgPoolOptions, Connection, Pool, Postgres};
use tokio::time::sleep;
use crate::DatabaseConfig;
pub async fn establish_connection() -> DatabaseConnection {
pub async fn set_database_up(database_connection: &Pool<Postgres>) {
sqlx::migrate!().run(database_connection).await.unwrap();
}
pub async fn establish_connection() -> Pool<Postgres> {
let database_config = DatabaseConfig::default();
let connection_string = format!(
"{}://{}:{}@{}/{}",
@ -20,12 +23,27 @@ pub async fn establish_connection() -> DatabaseConnection {
database_config.address,
database_config.database
);
Database::connect(connection_string).await.unwrap()
PgPoolOptions::new()
.max_connections(database_config.connection_pool_size)
.test_before_acquire(false)
.connect(&connection_string)
.await
.unwrap()
}
pub async fn is_alive() -> bool {
pub async fn is_alive(database_connection: &Pool<Postgres>) -> bool {
tokio::select! {
database_connection = database_connection.acquire() => {
match database_connection {
Ok(mut database_connection) => {
match database_connection.ping().await {
Ok(_) => true,
Err(_) => false,
}
},
Err(_) => false,
}
}
_ = sleep(Duration::from_secs(1)) => false,
}
}