Environment variables at runtime to connect to database

This commit is contained in:
Alphonse Paix
2025-08-22 16:01:20 +02:00
parent 80b8029844
commit 19ddc8958d
5 changed files with 62 additions and 27 deletions

View File

@@ -1,6 +1,5 @@
use once_cell::sync::Lazy;
use secrecy::ExposeSecret;
use sqlx::{Executor, PgPool};
use sqlx::{Connection, Executor, PgConnection, PgPool};
use tokio::net::TcpListener;
use uuid::Uuid;
use zero2prod::{
@@ -112,7 +111,7 @@ async fn spawn_app() -> TestApp {
}
async fn configure_database(config: &DatabaseSettings) -> PgPool {
let connection = PgPool::connect(config.connection_string_without_db().expose_secret())
let mut connection = PgConnection::connect_with(&config.without_db())
.await
.expect("Failed to connect to Postgres");
connection
@@ -120,7 +119,7 @@ async fn configure_database(config: &DatabaseSettings) -> PgPool {
.await
.expect("Failed to create the database");
let connection_pool = PgPool::connect(config.connection_string().expose_secret())
let connection_pool = PgPool::connect_with(config.with_db())
.await
.expect("Failed to connect to Postgres");
sqlx::migrate!("./migrations")