db_testkit/
env.rs

1use std::sync::OnceLock;
2
3use crate::error::{PoolError, Result};
4
5/// A static cell that ensures environment variables are loaded only once
6static ENV_LOADED: OnceLock<()> = OnceLock::new();
7
8/// Loads environment variables from a .env file if they haven't been loaded yet.
9///
10/// This function uses a static `OnceLock` to ensure that the environment is only
11/// loaded once, even if called multiple times.
12fn load_env() {
13    ENV_LOADED.get_or_init(|| {
14        dotenvy::dotenv().ok();
15    });
16}
17
18/// Gets the PostgreSQL database URL from environment variables.
19///
20/// This function looks for a `DATABASE_URL` environment variable that contains
21/// a valid PostgreSQL connection string.
22///
23/// # Returns
24///
25/// Returns a `Result` containing the database URL string, or an error if the
26/// environment variable is not found or is invalid.
27#[cfg(feature = "postgres")]
28pub fn get_postgres_url() -> Result<String> {
29    load_env();
30    std::env::var("DATABASE_URL").map_err(|_| {
31        PoolError::ConfigError("DATABASE_URL environment variable not found".to_string())
32    })
33}
34
35/// Gets the MySQL database URL from environment variables.
36///
37/// This function looks for a `DATABASE_URL` environment variable that contains
38/// a valid MySQL connection string.
39///
40/// # Returns
41///
42/// Returns a `Result` containing the database URL string, or an error if the
43/// environment variable is not found or is invalid.
44#[cfg(feature = "mysql")]
45pub fn get_mysql_url() -> Result<String> {
46    load_env();
47    std::env::var("DATABASE_URL").map_err(|_| {
48        PoolError::ConfigError("DATABASE_URL environment variable not found".to_string())
49    })
50}
51
52/// Gets the SQLx PostgreSQL database URL from environment variables.
53///
54/// This function looks for a `DATABASE_URL` environment variable that contains
55/// a valid PostgreSQL connection string compatible with SQLx.
56///
57/// # Returns
58///
59/// Returns a `Result` containing the database URL string, or an error if the
60/// environment variable is not found or is invalid.
61#[cfg(feature = "sqlx-postgres")]
62pub fn get_sqlx_postgres_url() -> Result<String> {
63    load_env();
64    std::env::var("DATABASE_URL").map_err(|_| {
65        PoolError::ConfigError("DATABASE_URL environment variable not found".to_string())
66    })
67}
68
69/// Gets the SQLite database URL from environment variables.
70///
71/// This function looks for a `DATABASE_URL` environment variable that contains
72/// a valid path where SQLite databases should be stored.
73///
74/// # Returns
75///
76/// Returns a `Result` containing the database URL string, or an error if the
77/// environment variable is not found or is invalid.
78#[cfg(feature = "sqlx-sqlite")]
79pub fn get_sqlx_sqlite_url() -> Result<String> {
80    load_env();
81    std::env::var("DATABASE_URL").map_err(|e| {
82        PoolError::ConfigError(format!(
83            "Failed to get DATABASE_URL from environment: {}",
84            e
85        ))
86    })
87}