Skip to content

Commit 0e51272

Browse files
authored
fix(cli) move database_url #1391 (#1400)
1 parent 3749e0e commit 0e51272

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

sqlx-cli/src/lib.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::opt::{Command, DatabaseCommand, MigrateCommand};
2-
use anyhow::anyhow;
1+
use anyhow::Result;
32
use dotenv::dotenv;
4-
use std::env;
3+
4+
use crate::opt::{Command, DatabaseCommand, MigrateCommand};
55

66
mod database;
77
// mod migration;
@@ -12,15 +12,9 @@ mod prepare;
1212

1313
pub use crate::opt::Opt;
1414

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

18-
let database_url = match opt.database_url {
19-
Some(db_url) => db_url,
20-
None => env::var("DATABASE_URL")
21-
.map_err(|_| anyhow!("The DATABASE_URL environment variable must be set"))?,
22-
};
23-
2418
match opt.command {
2519
Command::Migrate(migrate) => match migrate.command {
2620
MigrateCommand::Add {
@@ -30,34 +24,47 @@ pub async fn run(opt: Opt) -> anyhow::Result<()> {
3024
MigrateCommand::Run {
3125
dry_run,
3226
ignore_missing,
27+
database_url,
3328
} => migrate::run(&migrate.source, &database_url, dry_run, ignore_missing).await?,
3429
MigrateCommand::Revert {
3530
dry_run,
3631
ignore_missing,
32+
database_url,
3733
} => migrate::revert(&migrate.source, &database_url, dry_run, ignore_missing).await?,
38-
MigrateCommand::Info => migrate::info(&migrate.source, &database_url).await?,
34+
MigrateCommand::Info { database_url } => {
35+
migrate::info(&migrate.source, &database_url).await?
36+
}
3937
MigrateCommand::BuildScript { force } => migrate::build_script(&migrate.source, force)?,
4038
},
4139

4240
Command::Database(database) => match database.command {
43-
DatabaseCommand::Create => database::create(&database_url).await?,
44-
DatabaseCommand::Drop { yes } => database::drop(&database_url, !yes).await?,
45-
DatabaseCommand::Reset { yes, source } => {
46-
database::reset(&source, &database_url, !yes).await?
41+
DatabaseCommand::Create { database_url } => database::create(&database_url).await?,
42+
DatabaseCommand::Drop { yes, database_url } => {
43+
database::drop(&database_url, !yes).await?
4744
}
48-
DatabaseCommand::Setup { source } => database::setup(&source, &database_url).await?,
45+
DatabaseCommand::Reset {
46+
yes,
47+
source,
48+
database_url,
49+
} => database::reset(&source, &database_url, !yes).await?,
50+
DatabaseCommand::Setup {
51+
source,
52+
database_url,
53+
} => database::setup(&source, &database_url).await?,
4954
},
5055

5156
Command::Prepare {
5257
check: false,
5358
merged,
5459
args,
60+
database_url,
5561
} => prepare::run(&database_url, merged, args)?,
5662

5763
Command::Prepare {
5864
check: true,
5965
merged,
6066
args,
67+
database_url,
6168
} => prepare::check(&database_url, merged, args)?,
6269
};
6370

sqlx-cli/src/opt.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ use clap::Clap;
44
pub struct Opt {
55
#[clap(subcommand)]
66
pub command: Command,
7-
8-
#[clap(short = 'D', long)]
9-
pub database_url: Option<String>,
107
}
118

129
#[derive(Clap, Debug)]
@@ -36,6 +33,10 @@ pub enum Command {
3633
/// Arguments to be passed to `cargo rustc ...`.
3734
#[clap(last = true)]
3835
args: Vec<String>,
36+
37+
/// Location of the DB, by default will be read from the DATABASE_URL env var
38+
#[clap(long, short = 'D', env)]
39+
database_url: String,
3940
},
4041

4142
#[clap(alias = "mig")]
@@ -52,14 +53,22 @@ pub struct DatabaseOpt {
5253
#[derive(Clap, Debug)]
5354
pub enum DatabaseCommand {
5455
/// Creates the database specified in your DATABASE_URL.
55-
Create,
56+
Create {
57+
/// Location of the DB, by default will be read from the DATABASE_URL env var
58+
#[clap(long, short = 'D', env)]
59+
database_url: String,
60+
},
5661

5762
/// Drops the database specified in your DATABASE_URL.
5863
Drop {
5964
/// Automatic confirmation. Without this option, you will be prompted before dropping
6065
/// your database.
6166
#[clap(short)]
6267
yes: bool,
68+
69+
/// Location of the DB, by default will be read from the DATABASE_URL env var
70+
#[clap(long, short = 'D', env)]
71+
database_url: String,
6372
},
6473

6574
/// Drops the database specified in your DATABASE_URL, re-creates it, and runs any pending migrations.
@@ -72,13 +81,21 @@ pub enum DatabaseCommand {
7281
/// Path to folder containing migrations.
7382
#[clap(long, default_value = "migrations")]
7483
source: String,
84+
85+
/// Location of the DB, by default will be read from the DATABASE_URL env var
86+
#[clap(long, short = 'D', env)]
87+
database_url: String,
7588
},
7689

7790
/// Creates the database specified in your DATABASE_URL and runs any pending migrations.
7891
Setup {
7992
/// Path to folder containing migrations.
8093
#[clap(long, default_value = "migrations")]
8194
source: String,
95+
96+
/// Location of the DB, by default will be read from the DATABASE_URL env var
97+
#[clap(long, short = 'D', env)]
98+
database_url: String,
8299
},
83100
}
84101

@@ -115,6 +132,10 @@ pub enum MigrateCommand {
115132
/// Ignore applied migrations that missing in the resolved migrations
116133
#[clap(long)]
117134
ignore_missing: bool,
135+
136+
/// Location of the DB, by default will be read from the DATABASE_URL env var
137+
#[clap(long, short = 'D', env)]
138+
database_url: String,
118139
},
119140

120141
/// Revert the latest migration with a down file.
@@ -126,10 +147,18 @@ pub enum MigrateCommand {
126147
/// Ignore applied migrations that missing in the resolved migrations
127148
#[clap(long)]
128149
ignore_missing: bool,
150+
151+
/// Location of the DB, by default will be read from the DATABASE_URL env var
152+
#[clap(long, short = 'D', env)]
153+
database_url: String,
129154
},
130155

131156
/// List all available migrations.
132-
Info,
157+
Info {
158+
/// Location of the DB, by default will be read from the DATABASE_URL env var
159+
#[clap(long, env)]
160+
database_url: String,
161+
},
133162

134163
/// Generate a `build.rs` to trigger recompilation when a new migration is added.
135164
///

0 commit comments

Comments
 (0)