mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 04:04:56 +00:00
Add support for crate specific .env files
This change will attempt to load an .env file from CARGO_MANIFEST_DIR, if it exists. For backwards compatibility, if the .env file does not exist, we will fall back to default dotenv behaviour. Resolves #267
This commit is contained in:
parent
581bcf0ceb
commit
e7c1486005
@ -11,6 +11,8 @@ use quote::quote;
|
||||
#[cfg(feature = "runtime-async-std")]
|
||||
use async_std::task::block_on;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use url::Url;
|
||||
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
@ -60,6 +62,16 @@ macro_rules! async_macro (
|
||||
let res: Result<proc_macro2::TokenStream> = block_on(async {
|
||||
use sqlx::connection::Connect;
|
||||
|
||||
// If a .env file exists at CARGO_MANIFEST_DIR, load environment variables from this,
|
||||
// otherwise fallback to default dotenv behaviour.
|
||||
if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") {
|
||||
let env_path = PathBuf::from(dir).join(".env");
|
||||
if env_path.exists() {
|
||||
dotenv::from_path(&env_path)
|
||||
.map_err(|e| format!("failed to load environment from {:?}, {}", env_path, e))?
|
||||
}
|
||||
}
|
||||
|
||||
let db_url = Url::parse(&dotenv::var("DATABASE_URL").map_err(|_| "DATABASE_URL not set")?)?;
|
||||
|
||||
match db_url.scheme() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user