Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize the macro error message based on the metadata #2769

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 16 additions & 21 deletions sqlx-macros-core/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,33 +160,28 @@ pub fn expand_input<'a>(
..
} => QueryDataSource::live(db_url)?,

_ => {
Metadata { offline, .. } => {
// Try load the cached query metadata file.
let filename = format!("query-{}.json", hash_string(&input.sql));

// Check SQLX_OFFLINE_DIR, then local .sqlx, then workspace .sqlx.
let data_file_path = if let Some(sqlx_offline_dir_path) = env("SQLX_OFFLINE_DIR")
.ok()
.map(PathBuf::from)
let dirs = [
env("SQLX_OFFLINE_DIR").ok().map(PathBuf::from),
Some(METADATA.manifest_dir.join(".sqlx")),
Some(METADATA.workspace_root().join(".sqlx")),
];
let Some(data_file_path) = dirs
.iter()
.filter_map(|path| path.as_ref())
.map(|path| path.join(&filename))
.filter(|path| path.exists())
{
sqlx_offline_dir_path
} else if let Some(local_path) =
Some(METADATA.manifest_dir.join(".sqlx").join(&filename))
.filter(|path| path.exists())
{
local_path
} else if let Some(workspace_path) =
Some(METADATA.workspace_root().join(".sqlx").join(&filename))
.filter(|path| path.exists())
{
workspace_path
} else {
.find(|path| path.exists())
else {
return Err(
"`DATABASE_URL` must be set, or `cargo sqlx prepare` must have been run \
and .sqlx must exist, to use query macros"
.into(),
if *offline {
"`SQLX_OFFLINE=true` but there is no cached data for this query, run `cargo sqlx prepare` to update the query cache or unset `SQLX_OFFLINE`"
} else {
"set `DATABASE_URL` to use query macros online, or run `cargo sqlx prepare` to update the query cache"
}.into()
);
};

Expand Down