forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.rs
More file actions
29 lines (26 loc) · 1000 Bytes
/
Copy pathdb.rs
File metadata and controls
29 lines (26 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use crate::config::Config;
use anyhow::{Context, Result};
use sqlx::{postgres::PgPoolOptions, PgPool};
use std::time::Duration;
/// Create and configure the PostgreSQL connection pool.
pub async fn create_pool(config: &Config) -> Result<PgPool> {
let pool = PgPoolOptions::new()
.max_connections(config.database_max_connections)
.min_connections(config.database_min_connections)
.acquire_timeout(Duration::from_secs(config.database_connect_timeout_secs))
.connect(&config.database_url)
.await
.context("Failed to connect to the database")?;
tracing::info!("Database pool established");
Ok(pool)
}
/// Run all embedded SQL migrations in order.
pub async fn run_migrations(pool: &PgPool) -> Result<()> {
tracing::info!("Running database migrations…");
sqlx::migrate!("./migrations")
.run(pool)
.await
.context("Failed to run database migrations")?;
tracing::info!("Migrations complete");
Ok(())
}