2024-12-04 23:17:19 +03:00
|
|
|
pub mod comment;
|
2024-12-05 02:21:20 +03:00
|
|
|
pub mod comment_interaction;
|
2024-12-14 03:11:39 +03:00
|
|
|
pub mod contact;
|
2024-11-30 19:36:20 +03:00
|
|
|
pub mod interaction;
|
2024-12-09 15:18:32 +03:00
|
|
|
pub mod permission;
|
2024-11-30 19:36:20 +03:00
|
|
|
pub mod post;
|
2024-12-05 02:21:20 +03:00
|
|
|
pub mod post_interaction;
|
2024-12-04 23:17:19 +03:00
|
|
|
pub mod role;
|
2024-11-30 19:36:20 +03:00
|
|
|
pub mod user;
|
|
|
|
|
2024-12-02 23:56:43 +03:00
|
|
|
use std::time::Duration;
|
2024-12-01 22:17:38 +03:00
|
|
|
|
2024-12-03 21:33:42 +03:00
|
|
|
use sqlx::{postgres::PgPoolOptions, Connection, Pool, Postgres};
|
2024-11-26 22:30:25 +03:00
|
|
|
use tokio::time::sleep;
|
|
|
|
|
2024-12-02 23:56:43 +03:00
|
|
|
use crate::DatabaseConfig;
|
2024-11-26 22:30:25 +03:00
|
|
|
|
2024-12-03 21:33:42 +03:00
|
|
|
pub async fn set_database_up(database_connection: &Pool<Postgres>) {
|
|
|
|
sqlx::migrate!().run(database_connection).await.unwrap();
|
|
|
|
}
|
|
|
|
pub async fn establish_connection() -> Pool<Postgres> {
|
2024-11-26 22:30:25 +03:00
|
|
|
let database_config = DatabaseConfig::default();
|
2024-12-02 23:56:43 +03:00
|
|
|
let connection_string = format!(
|
|
|
|
"{}://{}:{}@{}/{}",
|
|
|
|
database_config.backend,
|
|
|
|
database_config.username,
|
|
|
|
database_config.password,
|
|
|
|
database_config.address,
|
|
|
|
database_config.database
|
|
|
|
);
|
2024-12-03 21:33:42 +03:00
|
|
|
PgPoolOptions::new()
|
|
|
|
.max_connections(database_config.connection_pool_size)
|
|
|
|
.test_before_acquire(false)
|
|
|
|
.connect(&connection_string)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2024-11-26 22:30:25 +03:00
|
|
|
}
|
|
|
|
|
2024-12-03 21:33:42 +03:00
|
|
|
pub async fn is_alive(database_connection: &Pool<Postgres>) -> bool {
|
2024-11-26 22:30:25 +03:00
|
|
|
tokio::select! {
|
2024-12-03 21:33:42 +03:00
|
|
|
database_connection = database_connection.acquire() => {
|
|
|
|
match database_connection {
|
|
|
|
Ok(mut database_connection) => {
|
|
|
|
match database_connection.ping().await {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|
2024-12-02 23:56:43 +03:00
|
|
|
_ = sleep(Duration::from_secs(1)) => false,
|
2024-11-26 22:30:25 +03:00
|
|
|
}
|
|
|
|
}
|