34 lines
940 B
Rust
34 lines
940 B
Rust
use sqlx::postgres::{PgConnectOptions, PgConnectionInfo, PgPoolOptions};
|
|
use sqlx::{ConnectOptions, PgPool};
|
|
use tracing::info;
|
|
use tracing::log::LevelFilter;
|
|
|
|
pub struct Database {
|
|
pool: PgPool,
|
|
}
|
|
|
|
impl Database {
|
|
pub async fn connect(conn_string: &str) -> anyhow::Result<Self> {
|
|
let mut connect_options: PgConnectOptions = conn_string.parse()?;
|
|
connect_options.log_statements(LevelFilter::Debug);
|
|
info!("Connecting to the database");
|
|
let pool = PgPoolOptions::new().connect_with(connect_options).await?;
|
|
let pgver = pool
|
|
.acquire()
|
|
.await?
|
|
.server_version_num()
|
|
.map(|v| v.to_string());
|
|
info!(
|
|
"Database connected, PostgreSQL version {}",
|
|
pgver.as_deref().unwrap_or("unknown")
|
|
);
|
|
Ok(Self {
|
|
pool
|
|
})
|
|
}
|
|
|
|
pub fn pool(&self) -> PgPool {
|
|
self.pool.clone()
|
|
}
|
|
}
|