allow cargo sqlx prepare to pass trailing args to Cargo

remove unused import
This commit is contained in:
Austin Bonander
2020-05-18 14:25:57 -07:00
committed by Ryan Leckey
parent 2a6f1a24ea
commit 35fd6f64cc
3 changed files with 19 additions and 9 deletions

View File

@@ -1,4 +1,3 @@
use crate::migrator::DatabaseMigrator;
use dialoguer::Confirmation;
use anyhow::bail;

View File

@@ -23,7 +23,7 @@ pub enum Command {
///
/// Saves data for all invocations of `query!()` and friends in the project so that it may be
/// built in offline mode, i.e. so compilation does not require connecting to a running database.
/// Outputs to `sqlx-data.json` in the current directory.
/// Outputs to `sqlx-data.json` in the current directory, overwriting it if it already exists.
///
/// Offline mode can be activated simply by removing `DATABASE_URL` from the environment or
/// building without a `.env` file.
@@ -36,6 +36,10 @@ pub enum Command {
/// Intended for use in CI.
#[structopt(long)]
check: bool,
/// Any arguments to pass to `cargo check`.
#[structopt(name = "Cargo args", last = true)]
cargo_args: Vec<String>,
},
}
@@ -77,8 +81,14 @@ pub async fn run(cmd: Command) -> anyhow::Result<()> {
DatabaseCommand::Create => db::run_create().await?,
DatabaseCommand::Drop => db::run_drop().await?,
},
Command::Prepare { check: false } => prepare::run()?,
Command::Prepare { check: true } => prepare::check()?,
Command::Prepare {
check: false,
cargo_args,
} => prepare::run(cargo_args)?,
Command::Prepare {
check: true,
cargo_args,
} => prepare::check(cargo_args)?,
};
Ok(())

View File

@@ -12,7 +12,7 @@ use url::Url;
type QueryData = BTreeMap<String, serde_json::Value>;
type JsonObject = serde_json::Map<String, serde_json::Value>;
pub fn run() -> anyhow::Result<()> {
pub fn run(cargo_args: Vec<String>) -> anyhow::Result<()> {
#[derive(serde::Serialize)]
struct DataFile {
db: &'static str,
@@ -21,7 +21,7 @@ pub fn run() -> anyhow::Result<()> {
}
let db_kind = get_db_kind()?;
let data = run_prepare_step()?;
let data = run_prepare_step(cargo_args)?;
serde_json::to_writer_pretty(
File::create("sqlx-data.json").context("failed to create/open `sqlx-data.json`")?,
@@ -37,9 +37,9 @@ pub fn run() -> anyhow::Result<()> {
Ok(())
}
pub fn check() -> anyhow::Result<()> {
pub fn check(cargo_args: Vec<String>) -> anyhow::Result<()> {
let db_kind = get_db_kind()?;
let data = run_prepare_step()?;
let data = run_prepare_step(cargo_args)?;
let data_file = fs::read("sqlx-data.json").context(
"failed to open `sqlx-data.json`; you may need to run `cargo sqlx prepare` first",
@@ -70,13 +70,14 @@ pub fn check() -> anyhow::Result<()> {
Ok(())
}
fn run_prepare_step() -> anyhow::Result<QueryData> {
fn run_prepare_step(cargo_args: Vec<String>) -> anyhow::Result<QueryData> {
// path to the Cargo executable
let cargo = env::var("CARGO")
.context("`prepare` subcommand may only be invoked as `cargo sqlx prepare``")?;
let check_status = Command::new(&cargo)
.arg("check")
.args(cargo_args)
// set an always-changing env var that the macros depend on via `env!()`
.env(
"__SQLX_RECOMPILE_TRIGGER",