Implement single database pool initialization

This commit is contained in:
dal 2025-04-07 16:31:24 -06:00
parent 686e336921
commit 8c8cca7b62
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 36 additions and 16 deletions

View File

@ -14,26 +14,46 @@ use uuid::Uuid;
// Used to initialize the database pool once for all tests
static INIT: Once = Once::new();
static mut POOL_INITIALIZED: bool = false;
// Initialize database pool
async fn initialize() -> Result<()> {
INIT.call_once(|| {
println!("Database pool initialization called");
// Set environment variables for database connection
std::env::set_var("DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
std::env::set_var("TEST_DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
});
unsafe {
if POOL_INITIALIZED {
return Ok(());
}
// We only initialize the pools once but try to do it in each test to ensure they exist
match init_pools().await {
Ok(_) => {
println!("Database pool initialized successfully");
INIT.call_once(|| {
println!("Database pool initialization called");
// Set environment variables for database connection
std::env::set_var("DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
std::env::set_var("TEST_DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
// Create a runtime for initialization
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
// Initialize the pools
rt.block_on(async {
match init_pools().await {
Ok(_) => {
println!("Database pool initialized successfully");
POOL_INITIALIZED = true;
},
Err(e) => {
println!("Database pool initialization error: {}", e);
},
}
});
});
if POOL_INITIALIZED {
Ok(())
},
Err(e) => {
println!("Database pool initialization error: {}", e);
Err(anyhow::anyhow!("Failed to initialize database pool: {}", e))
},
} else {
Err(anyhow::anyhow!("Failed to initialize database pool"))
}
}
}