Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/launchbadge/sqlx into chore…
Browse files Browse the repository at this point in the history
…/use-cargo-check-prepare
  • Loading branch information
cycraig committed Aug 27, 2022
2 parents 3fdd70f + 0823e11 commit eb9d3f4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
5 changes: 3 additions & 2 deletions sqlx-cli/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ pub struct Metadata {
///
/// Typically `target` at the workspace root, but can be overridden
target_directory: PathBuf,
/// Crate in the current working directory, empty if run from a workspace root.
/// Crate in the current working directory, empty if run from a virtual
/// workspace root.
current_package: Option<Package>,
}

Expand Down Expand Up @@ -110,7 +111,7 @@ impl FromStr for Metadata {
let cargo_metadata: CargoMetadata = serde_json::from_str(s)?;

// Extract the package in the current working directory, will be empty if running
// from a workspace root.
// from a virtual workspace root.
let current_package: Option<Package> = cargo_metadata.root_package().map(Package::from);

let CargoMetadata {
Expand Down
18 changes: 17 additions & 1 deletion sqlx-cli/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,23 @@ hint: This command only works in the manifest directory of a Cargo package."#
}

// Combine the queries into one file.
let pattern = metadata.target_directory().join("sqlx/query-*.json");
let package_dir = if merge {
// Merge queries from all workspace crates.
"**"
} else {
// Use a separate sub-directory for each crate in a workspace. This avoids a race condition
// where `prepare` can pull in queries from multiple crates if they happen to be generated
// simultaneously (e.g. Rust Analyzer building in the background).
metadata
.current_package()
.map(|pkg| pkg.name())
.context("Resolving the crate package for the current working directory failed")?
};
let pattern = metadata
.target_directory()
.join("sqlx")
.join(package_dir)
.join("query-*.json");

let mut data = BTreeMap::new();

Expand Down
2 changes: 1 addition & 1 deletion sqlx-cli/tests/assets/sample_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -30506,7 +30506,7 @@
"features": []
}
],
"root": null
"root": "b_in_workspace_lib 0.1.0 (path+file:///home/user/problematic/workspace/b_in_workspace_lib)"
},
"target_directory": "/home/user/problematic/workspace/target",
"version": 1,
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/sqlite/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl SqliteConnectOptions {

/// Sets the [`vfs`](https://www.sqlite.org/vfs.html) parameter of the database connection.
///
/// The default value is empty, and sqlite will use the default VFS object dependeing on the
/// The default value is empty, and sqlite will use the default VFS object depending on the
/// operating system.
pub fn vfs(mut self, vfs_name: impl Into<Cow<'static, str>>) -> Self {
self.vfs = Some(vfs_name.into());
Expand Down
17 changes: 16 additions & 1 deletion sqlx-macros/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct Metadata {
offline: bool,
database_url: Option<String>,
#[cfg(feature = "offline")]
package_name: String,
#[cfg(feature = "offline")]
target_dir: PathBuf,
#[cfg(feature = "offline")]
workspace_root: Arc<Mutex<Option<PathBuf>>>,
Expand Down Expand Up @@ -74,6 +76,11 @@ static METADATA: Lazy<Metadata> = Lazy::new(|| {
.expect("`CARGO_MANIFEST_DIR` must be set")
.into();

#[cfg(feature = "offline")]
let package_name: String = env("CARGO_PKG_NAME")
.expect("`CARGO_PKG_NAME` must be set")
.into();

#[cfg(feature = "offline")]
let target_dir = env("CARGO_TARGET_DIR").map_or_else(|_| "target".into(), |dir| dir.into());

Expand Down Expand Up @@ -110,6 +117,8 @@ static METADATA: Lazy<Metadata> = Lazy::new(|| {
offline,
database_url,
#[cfg(feature = "offline")]
package_name,
#[cfg(feature = "offline")]
target_dir,
#[cfg(feature = "offline")]
workspace_root: Arc::new(Mutex::new(None)),
Expand Down Expand Up @@ -402,7 +411,13 @@ where
// If the build is offline, the cache is our input so it's pointless to also write data for it.
#[cfg(feature = "offline")]
if !offline {
let save_dir = METADATA.target_dir.join("sqlx");
// Use a separate sub-directory for each crate in a workspace. This avoids a race condition
// where `prepare` can pull in queries from multiple crates if they happen to be generated
// simultaneously (e.g. Rust Analyzer building in the background).
let save_dir = METADATA
.target_dir
.join("sqlx")
.join(&METADATA.package_name);
std::fs::create_dir_all(&save_dir)?;
data.save_in(save_dir, input.src_span)?;
}
Expand Down

0 comments on commit eb9d3f4

Please sign in to comment.