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 authored and abonander committed Mar 30, 2022
1 parent d3093d0 commit 0408707
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 452 deletions.
47 changes: 47 additions & 0 deletions sqlx-cli/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use anyhow::Context;
use serde::Deserialize;
use std::env;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
use std::process::Command;
use std::str;

#[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(cargo: &OsStr) -> anyhow::Result<PathBuf> {
let stdout = Command::new(&cargo)
.args(&["locate-project", "--message-format=plain"])
.output()
.context("could not locate manifest dir")?
.stdout;

let mut manifest_path: PathBuf = str::from_utf8(&stdout)
.context("output of `cargo locate-project` was not valid UTF-8")?
// get rid of the trailing newline
.trim()
.into();

manifest_path.pop();

Ok(manifest_path)
}

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)
}
44 changes: 34 additions & 10 deletions sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use anyhow::Result;

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

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

Command::Prepare {
check: false,
merged,
args,
check,
workspace,
database_url,
} => prepare::run(&database_url, merged, args)?,

Command::Prepare {
check: true,
merged,
args,
database_url,
} => prepare::check(&database_url, merged, args)?,
} => {
let cargo_path = cargo::cargo_path()?;
println!("cargo path: {:?}", cargo_path);

let manifest_dir = cargo::manifest_dir(&cargo_path)?;
let metadata = cargo::metadata(&cargo_path)
.context("`prepare` subcommand may only be invoked as `cargo sqlx prepare`")?;

let ctx = PrepareCtx {
workspace,
cargo: cargo_path,
cargo_args: args,
manifest_dir,
target_dir: metadata.target_directory,
workspace_root: metadata.workspace_root,
database_url,
};

println!("{:?}", ctx);

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

Ok(())
Expand Down
11 changes: 7 additions & 4 deletions sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ 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)]
args: Vec<String>,

/// Location of the DB, by default will be read from the DATABASE_URL env var
#[clap(long, short = 'D', env)]
database_url: String,
#[clap(long, short = 'D')]
database_url: Option<String>,
},

#[clap(alias = "mig")]
Expand Down
Loading

0 comments on commit 0408707

Please sign in to comment.