Skip to content

Commit 0133038

Browse files
committed
feat(cli): add --config override to all relevant commands
1 parent a67bb1b commit 0133038

File tree

3 files changed

+106
-22
lines changed

3 files changed

+106
-22
lines changed

sqlx-cli/src/lib.rs

+34-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::io;
2-
use std::path::PathBuf;
32
use std::time::Duration;
43

54
use anyhow::{Context, Result};
@@ -54,18 +53,19 @@ pub async fn run(opt: Opt) -> Result<()> {
5453
}
5554

5655
async fn do_run(opt: Opt) -> Result<()> {
57-
let config = config_from_current_dir().await?;
58-
5956
match opt.command {
6057
Command::Migrate(migrate) => match migrate.command {
61-
MigrateCommand::Add(opts) => migrate::add(config, opts).await?,
58+
MigrateCommand::Add(opts) => migrate::add(opts).await?,
6259
MigrateCommand::Run {
6360
source,
61+
config,
6462
dry_run,
6563
ignore_missing,
6664
mut connect_opts,
6765
target_version,
6866
} => {
67+
let config = config.load_config().await?;
68+
6969
connect_opts.populate_db_url(config)?;
7070

7171
migrate::run(
@@ -80,11 +80,14 @@ async fn do_run(opt: Opt) -> Result<()> {
8080
}
8181
MigrateCommand::Revert {
8282
source,
83+
config,
8384
dry_run,
8485
ignore_missing,
8586
mut connect_opts,
8687
target_version,
8788
} => {
89+
let config = config.load_config().await?;
90+
8891
connect_opts.populate_db_url(config)?;
8992

9093
migrate::revert(
@@ -99,43 +102,66 @@ async fn do_run(opt: Opt) -> Result<()> {
99102
}
100103
MigrateCommand::Info {
101104
source,
105+
config,
102106
mut connect_opts,
103107
} => {
108+
let config = config.load_config().await?;
109+
104110
connect_opts.populate_db_url(config)?;
105111

106112
migrate::info(config, &source, &connect_opts).await?
107113
}
108-
MigrateCommand::BuildScript { source, force } => {
114+
MigrateCommand::BuildScript {
115+
source,
116+
config,
117+
force,
118+
} => {
119+
let config = config.load_config().await?;
120+
109121
migrate::build_script(config, &source, force)?
110122
}
111123
},
112124

113125
Command::Database(database) => match database.command {
114-
DatabaseCommand::Create { mut connect_opts } => {
126+
DatabaseCommand::Create {
127+
config,
128+
mut connect_opts,
129+
} => {
130+
let config = config.load_config().await?;
131+
115132
connect_opts.populate_db_url(config)?;
116133
database::create(&connect_opts).await?
117134
}
118135
DatabaseCommand::Drop {
119136
confirmation,
137+
config,
120138
mut connect_opts,
121139
force,
122140
} => {
141+
let config = config.load_config().await?;
142+
123143
connect_opts.populate_db_url(config)?;
124144
database::drop(&connect_opts, !confirmation.yes, force).await?
125145
}
126146
DatabaseCommand::Reset {
127147
confirmation,
128148
source,
149+
config,
129150
mut connect_opts,
130151
force,
131152
} => {
153+
let config = config.load_config().await?;
154+
132155
connect_opts.populate_db_url(config)?;
133156
database::reset(config, &source, &connect_opts, !confirmation.yes, force).await?
134157
}
135158
DatabaseCommand::Setup {
136159
source,
160+
config,
137161
mut connect_opts,
138162
} => {
163+
let config = config.load_config().await?;
164+
139165
connect_opts.populate_db_url(config)?;
140166
database::setup(config, &source, &connect_opts).await?
141167
}
@@ -147,7 +173,9 @@ async fn do_run(opt: Opt) -> Result<()> {
147173
workspace,
148174
mut connect_opts,
149175
args,
176+
config,
150177
} => {
178+
let config = config.load_config().await?;
151179
connect_opts.populate_db_url(config)?;
152180
prepare::run(check, all, workspace, connect_opts, args).await?
153181
}
@@ -203,18 +231,3 @@ where
203231
)
204232
.await
205233
}
206-
207-
async fn config_from_current_dir() -> anyhow::Result<&'static Config> {
208-
// Tokio does file I/O on a background task anyway
209-
tokio::task::spawn_blocking(|| {
210-
let path = PathBuf::from("sqlx.toml");
211-
212-
if path.exists() {
213-
eprintln!("Found `sqlx.toml` in current directory; reading...");
214-
}
215-
216-
Config::read_with_or_default(move || Ok(path))
217-
})
218-
.await
219-
.context("unexpected error loading config")
220-
}

sqlx-cli/src/migrate.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use std::fs::{self, File};
1313
use std::path::Path;
1414
use std::time::Duration;
1515

16-
pub async fn add(config: &Config, opts: AddMigrationOpts) -> anyhow::Result<()> {
16+
pub async fn add(opts: AddMigrationOpts) -> anyhow::Result<()> {
17+
let config = opts.config.load_config().await?;
18+
1719
let source = opts.source.resolve(config);
1820

1921
fs::create_dir_all(source).context("Unable to create migrations directory")?;

sqlx-cli/src/opt.rs

+69
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use clap_complete::Shell;
1111
use sqlx::migrate::Migrator;
1212
use std::env;
1313
use std::ops::{Deref, Not};
14+
use std::path::PathBuf;
1415

1516
const HELP_STYLES: Styles = Styles::styled()
1617
.header(AnsiColor::Blue.on_default().bold())
@@ -67,6 +68,9 @@ pub enum Command {
6768

6869
#[clap(flatten)]
6970
connect_opts: ConnectOpts,
71+
72+
#[clap(flatten)]
73+
config: ConfigOpt,
7074
},
7175

7276
#[clap(alias = "mig")]
@@ -90,13 +94,19 @@ pub enum DatabaseCommand {
9094
Create {
9195
#[clap(flatten)]
9296
connect_opts: ConnectOpts,
97+
98+
#[clap(flatten)]
99+
config: ConfigOpt,
93100
},
94101

95102
/// Drops the database specified in your DATABASE_URL.
96103
Drop {
97104
#[clap(flatten)]
98105
confirmation: Confirmation,
99106

107+
#[clap(flatten)]
108+
config: ConfigOpt,
109+
100110
#[clap(flatten)]
101111
connect_opts: ConnectOpts,
102112

@@ -113,6 +123,9 @@ pub enum DatabaseCommand {
113123
#[clap(flatten)]
114124
source: MigrationSourceOpt,
115125

126+
#[clap(flatten)]
127+
config: ConfigOpt,
128+
116129
#[clap(flatten)]
117130
connect_opts: ConnectOpts,
118131

@@ -126,6 +139,9 @@ pub enum DatabaseCommand {
126139
#[clap(flatten)]
127140
source: MigrationSourceOpt,
128141

142+
#[clap(flatten)]
143+
config: ConfigOpt,
144+
129145
#[clap(flatten)]
130146
connect_opts: ConnectOpts,
131147
},
@@ -212,6 +228,9 @@ pub enum MigrateCommand {
212228
#[clap(flatten)]
213229
source: MigrationSourceOpt,
214230

231+
#[clap(flatten)]
232+
config: ConfigOpt,
233+
215234
/// List all the migrations to be run without applying
216235
#[clap(long)]
217236
dry_run: bool,
@@ -233,6 +252,9 @@ pub enum MigrateCommand {
233252
#[clap(flatten)]
234253
source: MigrationSourceOpt,
235254

255+
#[clap(flatten)]
256+
config: ConfigOpt,
257+
236258
/// List the migration to be reverted without applying
237259
#[clap(long)]
238260
dry_run: bool,
@@ -255,6 +277,9 @@ pub enum MigrateCommand {
255277
#[clap(flatten)]
256278
source: MigrationSourceOpt,
257279

280+
#[clap(flatten)]
281+
config: ConfigOpt,
282+
258283
#[clap(flatten)]
259284
connect_opts: ConnectOpts,
260285
},
@@ -266,6 +291,9 @@ pub enum MigrateCommand {
266291
#[clap(flatten)]
267292
source: MigrationSourceOpt,
268293

294+
#[clap(flatten)]
295+
config: ConfigOpt,
296+
269297
/// Overwrite the build script if it already exists.
270298
#[clap(long)]
271299
force: bool,
@@ -279,6 +307,9 @@ pub struct AddMigrationOpts {
279307
#[clap(flatten)]
280308
pub source: MigrationSourceOpt,
281309

310+
#[clap(flatten)]
311+
pub config: ConfigOpt,
312+
282313
/// If set, create an up-migration only. Conflicts with `--reversible`.
283314
#[clap(long, conflicts_with = "reversible")]
284315
simple: bool,
@@ -358,6 +389,20 @@ pub struct NoDotenvOpt {
358389
pub no_dotenv: bool,
359390
}
360391

392+
#[derive(Args, Debug)]
393+
pub struct ConfigOpt {
394+
/// Override the path to the config file.
395+
///
396+
/// Defaults to `sqlx.toml` in the current directory, if it exists.
397+
///
398+
/// Configuration file loading may be bypassed with `--config=/dev/null` on Linux,
399+
/// or `--config=NUL` on Windows.
400+
///
401+
/// Config file loading is enabled by the `sqlx-toml` feature.
402+
#[clap(long)]
403+
pub config: Option<PathBuf>,
404+
}
405+
361406
impl ConnectOpts {
362407
/// Require a database URL to be provided, otherwise
363408
/// return an error.
@@ -401,6 +446,30 @@ impl ConnectOpts {
401446
}
402447
}
403448

449+
impl ConfigOpt {
450+
pub async fn load_config(&self) -> anyhow::Result<&'static Config> {
451+
let path = self.config.clone();
452+
453+
// Tokio does file I/O on a background task anyway
454+
tokio::task::spawn_blocking(|| {
455+
if let Some(path) = path {
456+
let err_str = format!("error reading config from {path:?}");
457+
Config::try_read_with(|| Ok(path)).context(err_str)
458+
} else {
459+
let path = PathBuf::from("sqlx.toml");
460+
461+
if path.exists() {
462+
eprintln!("Found `sqlx.toml` in current directory; reading...");
463+
}
464+
465+
Ok(Config::read_with_or_default(move || Ok(path)))
466+
}
467+
})
468+
.await
469+
.context("unexpected error loading config")?
470+
}
471+
}
472+
404473
/// Argument for automatic confirmation.
405474
#[derive(Args, Copy, Clone, Debug)]
406475
pub struct Confirmation {

0 commit comments

Comments
 (0)