Add --database-url to command line

This commit is contained in:
Stuart Hinson 2020-08-17 07:16:39 -07:00 committed by Austin Bonander
parent dfb096eaa7
commit 2ec5ff06b0
3 changed files with 9 additions and 3 deletions

View File

@ -17,7 +17,7 @@ $ cargo install --version=0.1.0-beta.1 sqlx-cli --no-default-features --features
### Usage
All commands require `DATABASE_URL` to be set, either in the environment or in a `.env` file
All commands require that a database url is provided. This can be done either with the `--database-url` command line option or by setting `DATABASE_URL`, either in the environment or in a `.env` file
in the current working directory.
`database` and `migrate` subcommands support only Postgres; MySQL and SQLite are TODO.

View File

@ -15,8 +15,11 @@ pub use crate::opt::Opt;
pub async fn run(opt: Opt) -> anyhow::Result<()> {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.map_err(|_| anyhow!("The DATABASE_URL environment variable must be set"))?;
let database_url = match opt.database_url {
Some(db_url) => db_url,
None => env::var("DATABASE_URL")
.map_err(|_| anyhow!("The DATABASE_URL environment variable must be set"))?,
};
match opt.command {
Command::Migrate(migrate) => match migrate.command {

View File

@ -4,6 +4,9 @@ use clap::Clap;
pub struct Opt {
#[clap(subcommand)]
pub command: Command,
#[clap(short = "D", long)]
pub database_url: Option<String>,
}
#[derive(Clap, Debug)]