Skip to content

Commit 1fa2381

Browse files
jplatteabonander
authored andcommitted
feat(macros): move to one-file-per-query for offline mode
Query data is now stored in .sqlx/{query_hash}.json directly by the macro invocations, rather than first writing to target/sqlx/{input_span_hash}.json and then collecting those into sqlx-data.json separately.
1 parent babd353 commit 1fa2381

File tree

10 files changed

+497
-452
lines changed

10 files changed

+497
-452
lines changed

sqlx-cli/src/cargo.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use anyhow::Context;
2+
use serde::Deserialize;
3+
use std::env;
4+
use std::ffi::{OsStr, OsString};
5+
use std::path::PathBuf;
6+
use std::process::Command;
7+
use std::str;
8+
9+
#[derive(Deserialize)]
10+
pub struct CargoMetadata {
11+
pub target_directory: PathBuf,
12+
pub workspace_root: PathBuf,
13+
}
14+
15+
/// Path to the `cargo` executable
16+
pub fn cargo_path() -> anyhow::Result<OsString> {
17+
env::var_os("CARGO").context("Failed to obtain value of `CARGO`")
18+
}
19+
20+
pub fn manifest_dir(cargo: &OsStr) -> anyhow::Result<PathBuf> {
21+
let stdout = Command::new(&cargo)
22+
.args(&["locate-project", "--message-format=plain"])
23+
.output()
24+
.context("could not locate manifest dir")?
25+
.stdout;
26+
27+
let mut manifest_path: PathBuf = str::from_utf8(&stdout)
28+
.context("output of `cargo locate-project` was not valid UTF-8")?
29+
// get rid of the trailing newline
30+
.trim()
31+
.into();
32+
33+
manifest_path.pop();
34+
35+
Ok(manifest_path)
36+
}
37+
38+
pub fn metadata(cargo: &OsStr) -> anyhow::Result<CargoMetadata> {
39+
let output = Command::new(&cargo)
40+
.args(&["metadata", "--format-version=1"])
41+
.output()
42+
.context("Could not fetch metadata")?;
43+
44+
serde_json::from_slice(&output.stdout)
45+
.context("Invalid `cargo metadata` output")
46+
.map_err(Into::into)
47+
}

sqlx-cli/src/lib.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ use anyhow::Result;
22

33
use crate::opt::{Command, DatabaseCommand, MigrateCommand};
44

5+
use anyhow::{anyhow, Context};
6+
use dotenv::dotenv;
7+
use prepare::PrepareCtx;
8+
use std::env;
9+
10+
mod cargo;
511
mod database;
612
// mod migration;
713
// mod migrator;
@@ -74,18 +80,36 @@ pub async fn run(opt: Opt) -> Result<()> {
7480
},
7581

7682
Command::Prepare {
77-
check: false,
78-
merged,
79-
args,
83+
check,
84+
workspace,
8085
database_url,
81-
} => prepare::run(&database_url, merged, args)?,
82-
83-
Command::Prepare {
84-
check: true,
85-
merged,
8686
args,
87-
database_url,
88-
} => prepare::check(&database_url, merged, args)?,
87+
} => {
88+
let cargo_path = cargo::cargo_path()?;
89+
println!("cargo path: {:?}", cargo_path);
90+
91+
let manifest_dir = cargo::manifest_dir(&cargo_path)?;
92+
let metadata = cargo::metadata(&cargo_path)
93+
.context("`prepare` subcommand may only be invoked as `cargo sqlx prepare`")?;
94+
95+
let ctx = PrepareCtx {
96+
workspace,
97+
cargo: cargo_path,
98+
cargo_args: args,
99+
manifest_dir,
100+
target_dir: metadata.target_directory,
101+
workspace_root: metadata.workspace_root,
102+
database_url,
103+
};
104+
105+
println!("{:?}", ctx);
106+
107+
if check {
108+
prepare::check(&ctx)?
109+
} else {
110+
prepare::run(&ctx)?
111+
}
112+
}
89113
};
90114

91115
Ok(())

sqlx-cli/src/opt.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ pub enum Command {
2929
#[clap(long)]
3030
check: bool,
3131

32-
/// Generate a single top-level `sqlx-data.json` file when using a cargo workspace.
32+
/// Do a clean build of all crates in the workspace.
33+
///
34+
/// This option is intended for workspaces where multiple crates use SQLx; if there is only
35+
/// one, it is better to run `cargo sqlx prepare` without this option inside of that crate.
3336
#[clap(long)]
34-
merged: bool,
37+
workspace: bool,
3538

3639
/// Arguments to be passed to `cargo rustc ...`.
3740
#[clap(last = true)]
3841
args: Vec<String>,
3942

40-
#[clap(flatten)]
41-
database_url: DatabaseUrl,
43+
// `DatabaseUrl` doesn't allow it to be optional
44+
/// Location of the DB, by default will be read from the DATABASE_URL env var
45+
#[clap(long, short = 'D', env)]
46+
database_url: Option<String>,
4247
},
4348

4449
#[clap(alias = "mig")]

0 commit comments

Comments
 (0)