Skip to content

Commit fc206e1

Browse files
committed
fix(config): restore fallback to default config for macros
1 parent de4e2fc commit fc206e1

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

sqlx-core/src/config/mod.rs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,14 @@ impl ConfigError {
124124
/// Create a [`ConfigError`] from a [`std::io::Error`].
125125
///
126126
/// Maps to either `NotFound` or `Io`.
127-
pub fn from_io(path: PathBuf, error: io::Error) -> Self {
127+
pub fn from_io(path: impl Into<PathBuf>, error: io::Error) -> Self {
128128
if error.kind() == io::ErrorKind::NotFound {
129-
Self::NotFound { path }
129+
Self::NotFound { path: path.into() }
130130
} else {
131-
Self::Io { path, error }
131+
Self::Io {
132+
path: path.into(),
133+
error,
134+
}
132135
}
133136
}
134137

@@ -147,52 +150,58 @@ static CACHE: OnceCell<Config> = OnceCell::new();
147150
/// Internal methods for loading a `Config`.
148151
#[allow(clippy::result_large_err)]
149152
impl Config {
150-
/// Get the cached config, or to read `$CARGO_MANIFEST_DIR/sqlx.toml`.
153+
/// Get the cached config, or read `$CARGO_MANIFEST_DIR/sqlx.toml`.
151154
///
152155
/// On success, the config is cached in a `static` and returned by future calls.
153156
///
154157
/// Errors if `CARGO_MANIFEST_DIR` is not set, or if the config file could not be read.
155-
pub fn try_from_crate() -> Result<&'static Self, ConfigError> {
156-
Self::try_read_with(get_crate_path)
158+
///
159+
/// If the file does not exist, the cache is populated with `Config::default()`.
160+
pub fn try_from_crate_or_default() -> Result<&'static Self, ConfigError> {
161+
CACHE.get_or_try_init(|| {
162+
Self::read_from(get_crate_path()?).or_else(|e| {
163+
if let ConfigError::NotFound { .. } = e {
164+
Ok(Config::default())
165+
} else {
166+
Err(e)
167+
}
168+
})
169+
})
157170
}
158171

159-
/// Get the cached config, or attempt to read `sqlx.toml` from the current working directory.
172+
/// Get the cached config, or read `$CARGO_MANIFEST_DIR/sqlx.toml`.
160173
///
161174
/// On success, the config is cached in a `static` and returned by future calls.
162175
///
163-
/// Errors if the config file does not exist, or could not be read.
164-
pub fn try_from_current_dir() -> Result<&'static Self, ConfigError> {
165-
Self::try_read_with(|| Ok("sqlx.toml".into()))
176+
/// Errors if `CARGO_MANIFEST_DIR` is not set, or if the config file could not be read.
177+
///
178+
/// If the file does not exist, the cache is populated with `Config::default()`.
179+
pub fn try_from_crate_or_default() -> Result<&'static Self, ConfigError> {
180+
CACHE.get_or_try_init(|| {
181+
Self::read_from(get_crate_path()?).or_else(|e| {
182+
if let ConfigError::NotFound { .. } = e {
183+
Ok(Config::default())
184+
} else {
185+
Err(e)
186+
}
187+
})
188+
})
166189
}
167190

168191
/// Get the cached config, or attempt to read it from the path given.
169192
///
170193
/// On success, the config is cached in a `static` and returned by future calls.
171194
///
172195
/// Errors if the config file does not exist, or could not be read.
173-
pub fn try_from_path(path: impl Into<PathBuf>) -> Result<&'static Self, ConfigError> {
174-
Self::try_read_with(|| Ok(path.into()))
196+
pub fn try_from_path(path: PathBuf) -> Result<&'static Self, ConfigError> {
197+
CACHE.get_or_try_init(|| Self::read_from(path))
175198
}
176199

177200
/// Get the cached config, or return the default.
178201
pub fn get_or_default() -> &'static Self {
179202
CACHE.get_or_init(Config::default)
180203
}
181204

182-
/// Get the cached config, or attempt to read it from the path returned by the closure.
183-
///
184-
/// On success, the config is cached in a `static` and returned by future calls.
185-
///
186-
/// Errors if the config file does not exist, or could not be read.
187-
fn try_read_with(
188-
make_path: impl FnOnce() -> Result<PathBuf, ConfigError>,
189-
) -> Result<&'static Self, ConfigError> {
190-
CACHE.get_or_try_init(|| {
191-
let path = make_path()?;
192-
Self::read_from(path)
193-
})
194-
}
195-
196205
#[cfg(feature = "sqlx-toml")]
197206
fn read_from(path: PathBuf) -> Result<Self, ConfigError> {
198207
// The `toml` crate doesn't provide an incremental reader.
@@ -216,8 +225,8 @@ impl Config {
216225
#[cfg(not(feature = "sqlx-toml"))]
217226
fn read_from(path: PathBuf) -> Result<Self, ConfigError> {
218227
match path.try_exists() {
219-
Ok(true) => Err(ConfigError::ParseDisabled { path }),
220-
Ok(false) => Err(ConfigError::NotFound { path }),
228+
Ok(true) => Err(ConfigError::ParseDisabled { path: path.into() }),
229+
Ok(false) => Err(ConfigError::NotFound { path: path.into() }),
221230
Err(e) => Err(ConfigError::from_io(path, e)),
222231
}
223232
}

sqlx-macros-core/src/migrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn default_path(config: &Config) -> &str {
9292
}
9393

9494
pub fn expand(path_arg: Option<LitStr>) -> crate::Result<TokenStream> {
95-
let config = Config::try_from_crate()?;
95+
let config = Config::try_from_crate_or_default()?;
9696

9797
let path = match path_arg {
9898
Some(path_arg) => crate::common::resolve_path(path_arg.value(), path_arg.span())?,

sqlx-macros-core/src/query/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ fn init_metadata(manifest_dir: &String) -> crate::Result<Metadata> {
123123
.map(|s| s.eq_ignore_ascii_case("true") || s == "1")
124124
.unwrap_or(false);
125125

126-
let var_name = Config::try_from_crate()?.common.database_url_var();
126+
let var_name = Config::try_from_crate_or_default()?
127+
.common
128+
.database_url_var();
127129

128130
let database_url = env(var_name).ok().or(database_url);
129131

@@ -256,7 +258,7 @@ fn expand_with_data<DB: DatabaseExt>(
256258
where
257259
Describe<DB>: DescribeExt,
258260
{
259-
let config = Config::try_from_crate()?;
261+
let config = Config::try_from_crate_or_default()?;
260262

261263
// validate at the minimum that our args match the query's input parameters
262264
let num_parameters = match data.describe.parameters() {

sqlx-macros-core/src/test_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn expand_simple(input: syn::ItemFn) -> TokenStream {
7777

7878
#[cfg(feature = "migrate")]
7979
fn expand_advanced(args: AttributeArgs, input: syn::ItemFn) -> crate::Result<TokenStream> {
80-
let config = sqlx_core::config::Config::try_from_crate()?;
80+
let config = sqlx_core::config::Config::try_from_crate_or_default()?;
8181

8282
let ret = &input.sig.output;
8383
let name = &input.sig.ident;

0 commit comments

Comments
 (0)