Skip to content

Commit 0408707

Browse files
jplatteabonander
authored andcommitted
WIP: [offline] Remove sqlx-data.json and sqlx prepare command
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 d3093d0 commit 0408707

File tree

10 files changed

+479
-452
lines changed

10 files changed

+479
-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;
@@ -51,18 +57,36 @@ pub async fn run(opt: Opt) -> Result<()> {
5157
},
5258

5359
Command::Prepare {
54-
check: false,
55-
merged,
56-
args,
60+
check,
61+
workspace,
5762
database_url,
58-
} => prepare::run(&database_url, merged, args)?,
59-
60-
Command::Prepare {
61-
check: true,
62-
merged,
6363
args,
64-
database_url,
65-
} => prepare::check(&database_url, merged, args)?,
64+
} => {
65+
let cargo_path = cargo::cargo_path()?;
66+
println!("cargo path: {:?}", cargo_path);
67+
68+
let manifest_dir = cargo::manifest_dir(&cargo_path)?;
69+
let metadata = cargo::metadata(&cargo_path)
70+
.context("`prepare` subcommand may only be invoked as `cargo sqlx prepare`")?;
71+
72+
let ctx = PrepareCtx {
73+
workspace,
74+
cargo: cargo_path,
75+
cargo_args: args,
76+
manifest_dir,
77+
target_dir: metadata.target_directory,
78+
workspace_root: metadata.workspace_root,
79+
database_url,
80+
};
81+
82+
println!("{:?}", ctx);
83+
84+
if check {
85+
prepare::check(&ctx)?
86+
} else {
87+
prepare::run(&ctx)?
88+
}
89+
}
6690
};
6791

6892
Ok(())

sqlx-cli/src/opt.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ pub enum Command {
2727
#[clap(long)]
2828
check: bool,
2929

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

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

3841
/// Location of the DB, by default will be read from the DATABASE_URL env var
39-
#[clap(long, short = 'D', env)]
40-
database_url: String,
42+
#[clap(long, short = 'D')]
43+
database_url: Option<String>,
4144
},
4245

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

0 commit comments

Comments
 (0)