Skip to content

Commit

Permalink
fix(cli) move database_url #1391 (#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
leshow authored Aug 30, 2021
1 parent 3749e0e commit 0e51272
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
39 changes: 23 additions & 16 deletions sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::opt::{Command, DatabaseCommand, MigrateCommand};
use anyhow::anyhow;
use anyhow::Result;
use dotenv::dotenv;
use std::env;

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

mod database;
// mod migration;
Expand All @@ -12,15 +12,9 @@ mod prepare;

pub use crate::opt::Opt;

pub async fn run(opt: Opt) -> anyhow::Result<()> {
pub async fn run(opt: Opt) -> Result<()> {
dotenv().ok();

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 {
MigrateCommand::Add {
Expand All @@ -30,34 +24,47 @@ pub async fn run(opt: Opt) -> anyhow::Result<()> {
MigrateCommand::Run {
dry_run,
ignore_missing,
database_url,
} => migrate::run(&migrate.source, &database_url, dry_run, ignore_missing).await?,
MigrateCommand::Revert {
dry_run,
ignore_missing,
database_url,
} => migrate::revert(&migrate.source, &database_url, dry_run, ignore_missing).await?,
MigrateCommand::Info => migrate::info(&migrate.source, &database_url).await?,
MigrateCommand::Info { database_url } => {
migrate::info(&migrate.source, &database_url).await?
}
MigrateCommand::BuildScript { force } => migrate::build_script(&migrate.source, force)?,
},

Command::Database(database) => match database.command {
DatabaseCommand::Create => database::create(&database_url).await?,
DatabaseCommand::Drop { yes } => database::drop(&database_url, !yes).await?,
DatabaseCommand::Reset { yes, source } => {
database::reset(&source, &database_url, !yes).await?
DatabaseCommand::Create { database_url } => database::create(&database_url).await?,
DatabaseCommand::Drop { yes, database_url } => {
database::drop(&database_url, !yes).await?
}
DatabaseCommand::Setup { source } => database::setup(&source, &database_url).await?,
DatabaseCommand::Reset {
yes,
source,
database_url,
} => database::reset(&source, &database_url, !yes).await?,
DatabaseCommand::Setup {
source,
database_url,
} => database::setup(&source, &database_url).await?,
},

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

Command::Prepare {
check: true,
merged,
args,
database_url,
} => prepare::check(&database_url, merged, args)?,
};

Expand Down
39 changes: 34 additions & 5 deletions sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use clap::Clap;
pub struct Opt {
#[clap(subcommand)]
pub command: Command,

#[clap(short = 'D', long)]
pub database_url: Option<String>,
}

#[derive(Clap, Debug)]
Expand Down Expand Up @@ -36,6 +33,10 @@ pub enum Command {
/// 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(alias = "mig")]
Expand All @@ -52,14 +53,22 @@ pub struct DatabaseOpt {
#[derive(Clap, Debug)]
pub enum DatabaseCommand {
/// Creates the database specified in your DATABASE_URL.
Create,
Create {
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[clap(long, short = 'D', env)]
database_url: String,
},

/// Drops the database specified in your DATABASE_URL.
Drop {
/// Automatic confirmation. Without this option, you will be prompted before dropping
/// your database.
#[clap(short)]
yes: bool,

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

/// Drops the database specified in your DATABASE_URL, re-creates it, and runs any pending migrations.
Expand All @@ -72,13 +81,21 @@ pub enum DatabaseCommand {
/// Path to folder containing migrations.
#[clap(long, default_value = "migrations")]
source: String,

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

/// Creates the database specified in your DATABASE_URL and runs any pending migrations.
Setup {
/// Path to folder containing migrations.
#[clap(long, default_value = "migrations")]
source: String,

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

Expand Down Expand Up @@ -115,6 +132,10 @@ pub enum MigrateCommand {
/// Ignore applied migrations that missing in the resolved migrations
#[clap(long)]
ignore_missing: bool,

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

/// Revert the latest migration with a down file.
Expand All @@ -126,10 +147,18 @@ pub enum MigrateCommand {
/// Ignore applied migrations that missing in the resolved migrations
#[clap(long)]
ignore_missing: bool,

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

/// List all available migrations.
Info,
Info {
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[clap(long, env)]
database_url: String,
},

/// Generate a `build.rs` to trigger recompilation when a new migration is added.
///
Expand Down

0 comments on commit 0e51272

Please sign in to comment.