Skip to content

Commit

Permalink
WIP: [offline] Remove sqlx-data.json and sqlx prepare command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jplatte committed Feb 2, 2021
1 parent 8080f14 commit ba643dc
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 251 deletions.
16 changes: 2 additions & 14 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions sqlx-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ serde_json = { version = "1.0.53", features = ["preserve_order"] }
serde = { version = "1.0.110", features = ["derive"] }
glob = "0.3.0"
openssl = { version = "0.10.30", optional = true }
# workaround for https://github.com/rust-lang/rust/issues/29497
remove_dir_all = "0.6.0"

[features]
default = [ "postgres", "sqlite", "mysql" ]
Expand Down
34 changes: 34 additions & 0 deletions sqlx-cli/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anyhow::Context;
use serde::Deserialize;
use std::env;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
use std::process::Command;

#[derive(Deserialize)]
pub struct CargoMetadata {
pub target_directory: PathBuf,
pub workspace_root: PathBuf,
}

/// Path to the `cargo` executable
pub fn cargo_path() -> anyhow::Result<OsString> {
env::var_os("CARGO").context("Failed to obtain value of `CARGO`")
}

pub fn manifest_dir() -> anyhow::Result<PathBuf> {
Ok(env::var_os("CARGO_MANIFEST_DIR")
.context("Failed to obtain value of `CARGO_MANIFEST_DIR`")?
.into())
}

pub fn metadata(cargo: &OsStr) -> anyhow::Result<CargoMetadata> {
let output = Command::new(&cargo)
.args(&["metadata", "--format-version=1"])
.output()
.context("Could not fetch metadata")?;

serde_json::from_slice(&output.stdout)
.context("Invalid `cargo metadata` output")
.map_err(Into::into)
}
34 changes: 25 additions & 9 deletions sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::opt::{Command, DatabaseCommand, MigrateCommand};
use anyhow::anyhow;
use anyhow::{anyhow, Context};
use dotenv::dotenv;
use prepare::PrepareCtx;
use std::env;

mod cargo;
mod database;
// mod migration;
// mod migrator;
Expand Down Expand Up @@ -46,16 +48,30 @@ pub async fn run(opt: Opt) -> anyhow::Result<()> {
},

Command::Prepare {
check: false,
merged,
check,
workspace,
args,
} => prepare::run(&database_url, merged, args)?,
} => {
let cargo_path = cargo::cargo_path()?;
let manifest_dir = cargo::manifest_dir()?;
let metadata = cargo::metadata(&cargo_path)
.context("`prepare` subcommand may only be invoked as `cargo sqlx prepare`")?;

Command::Prepare {
check: true,
merged,
args,
} => prepare::check(&database_url, merged, args)?,
let ctx = PrepareCtx {
workspace,
cargo: cargo_path,
cargo_args: args,
manifest_dir,
target_dir: metadata.target_directory,
workspace_root: metadata.workspace_root,
};

if check {
prepare::check(&ctx)?
} else {
prepare::run(&ctx)?
}
}
};

Ok(())
Expand Down
7 changes: 5 additions & 2 deletions sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ pub enum Command {
#[clap(long)]
check: bool,

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

/// Arguments to be passed to `cargo rustc ...`.
#[clap(last = true)]
Expand Down
Loading

0 comments on commit ba643dc

Please sign in to comment.