Skip to content

Commit fc805b4

Browse files
committed
fix issues
1 parent 10fdca6 commit fc805b4

File tree

5 files changed

+74
-28
lines changed

5 files changed

+74
-28
lines changed

refinery/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ edition = "2018"
1414

1515
[features]
1616
default = ["toml"]
17-
rusqlite-bundled = ["refinery-core/rusqlite-bundled"]
18-
rusqlite = ["refinery-core/rusqlite"]
19-
postgres = ["refinery-core/postgres", "refinery-core/postgres-native-tls", "refinery-core/native-tls"]
17+
enums = ["refinery-macros/enums"]
2018
mysql = ["refinery-core/mysql"]
21-
tokio-postgres = ["refinery-core/tokio-postgres", "refinery-core/postgres-native-tls", "refinery-core/native-tls"]
2219
mysql_async = ["refinery-core/mysql_async"]
20+
postgres = ["refinery-core/postgres"]
21+
rusqlite = ["refinery-core/rusqlite"]
22+
rusqlite-bundled = ["refinery-core/rusqlite-bundled"]
23+
serde = ["refinery-core/serde"]
2324
tiberius = ["refinery-core/tiberius"]
2425
tiberius-config = ["refinery-core/tiberius", "refinery-core/tiberius-config"]
25-
serde = ["refinery-core/serde"]
26+
tokio-postgres = ["refinery-core/tokio-postgres"]
2627
toml = ["refinery-core/toml"]
27-
enums = ["refinery-macros/enums"]
2828

2929
[dependencies]
3030
refinery-core = { version = "0.8.14", path = "../refinery_core" }

refinery_cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ path = "src/main.rs"
1616

1717
[features]
1818
default = ["mysql", "postgresql", "sqlite-bundled", "mssql"]
19-
postgresql = ["refinery-core/postgres", "refinery-core/postgres-native-tls", "refinery-core/native-tls"]
19+
postgresql = ["refinery-core/postgres"]
2020
mysql = ["refinery-core/mysql"]
2121
sqlite = ["refinery-core/rusqlite"]
2222
sqlite-bundled = ["sqlite", "refinery-core/rusqlite-bundled"]

refinery_core/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ edition = "2021"
1010

1111
[features]
1212
default = []
13+
mysql_async = ["dep:mysql_async"]
14+
postgres = ["dep:postgres", "dep:postgres-native-tls", "dep:native-tls"]
1315
rusqlite-bundled = ["rusqlite", "rusqlite/bundled"]
16+
serde = ["dep:serde"]
1417
tiberius = ["dep:tiberius", "futures", "tokio", "tokio/net"]
1518
tiberius-config = ["tiberius", "tokio", "tokio-util", "serde"]
16-
tokio-postgres = ["dep:tokio-postgres", "tokio", "tokio/rt"]
17-
mysql_async = ["dep:mysql_async"]
18-
serde = ["dep:serde"]
19+
tokio-postgres = ["dep:postgres-native-tls", "dep:native-tls", "dep:tokio-postgres", "tokio", "tokio/rt"]
1920
toml = ["serde", "dep:toml"]
2021

2122
[dependencies]

refinery_core/src/config.rs

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use crate::Error;
33
use std::convert::TryFrom;
44
use std::path::PathBuf;
55
use std::str::FromStr;
6+
#[cfg(any(
7+
feature = "postgres",
8+
feature = "tokio-postgres",
9+
feature = "tiberius-config"
10+
))]
611
use std::{borrow::Cow, collections::HashMap};
712
use url::Url;
813

@@ -35,7 +40,8 @@ impl Config {
3540
db_user: None,
3641
db_pass: None,
3742
db_name: None,
38-
use_tls: None,
43+
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
44+
use_tls: false,
3945
#[cfg(feature = "tiberius-config")]
4046
trust_cert: false,
4147
},
@@ -141,7 +147,8 @@ impl Config {
141147
self.main.db_port.as_deref()
142148
}
143149

144-
pub fn use_tls(&self) -> Option<bool> {
150+
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
151+
pub fn use_tls(&self) -> bool {
145152
self.main.use_tls
146153
}
147154

@@ -189,6 +196,16 @@ impl Config {
189196
},
190197
}
191198
}
199+
200+
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
201+
pub fn set_use_tls(self, use_tls: bool) -> Config {
202+
Config {
203+
main: Main {
204+
use_tls,
205+
..self.main
206+
},
207+
}
208+
}
192209
}
193210

194211
impl TryFrom<Url> for Config {
@@ -209,6 +226,11 @@ impl TryFrom<Url> for Config {
209226
}
210227
};
211228

229+
#[cfg(any(
230+
feature = "postgres",
231+
feature = "tokio-postgres",
232+
feature = "tiberius-config"
233+
))]
212234
let query_params = url
213235
.query_pairs()
214236
.collect::<HashMap<Cow<'_, str>, Cow<'_, str>>>();
@@ -228,12 +250,10 @@ impl TryFrom<Url> for Config {
228250
}
229251
}
230252

231-
let use_tls = match query_params
232-
.get("sslmode")
233-
.unwrap_or(&Cow::Borrowed("disable"))
234-
{
235-
&Cow::Borrowed("disable") => false,
236-
&Cow::Borrowed("require") => true,
253+
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
254+
let use_tls = match query_params.get("sslmode") {
255+
Some(&Cow::Borrowed("require")) => true,
256+
Some(&Cow::Borrowed("disable")) | None => false,
237257
_ => {
238258
return Err(Error::new(
239259
Kind::ConfigError("Invalid sslmode value, please use disable/require".into()),
@@ -257,7 +277,8 @@ impl TryFrom<Url> for Config {
257277
db_user: Some(url.username().to_string()),
258278
db_pass: url.password().map(|r| r.to_string()),
259279
db_name: Some(url.path().trim_start_matches('/').to_string()),
260-
use_tls: Some(use_tls),
280+
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
281+
use_tls,
261282
#[cfg(feature = "tiberius-config")]
262283
trust_cert,
263284
},
@@ -290,7 +311,9 @@ struct Main {
290311
db_user: Option<String>,
291312
db_pass: Option<String>,
292313
db_name: Option<String>,
293-
use_tls: Option<bool>,
314+
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
315+
#[serde(default)]
316+
use_tls: bool,
294317
#[cfg(feature = "tiberius-config")]
295318
#[serde(default)]
296319
trust_cert: bool,
@@ -474,18 +497,40 @@ mod tests {
474497
);
475498
}
476499

500+
#[cfg(any(feature = "postgres", feature = "tokio-postgres"))]
477501
#[test]
478502
fn builds_from_sslmode_str() {
479-
let config =
503+
use crate::config::ConfigDbType;
504+
505+
let config_disable =
480506
Config::from_str("postgres://root:1234@localhost:5432/refinery?sslmode=disable")
481507
.unwrap();
482-
assert!(config.use_tls().is_some());
483-
assert!(!config.use_tls().unwrap());
484-
let config =
508+
assert!(!config_disable.use_tls());
509+
510+
let config_require =
485511
Config::from_str("postgres://root:1234@localhost:5432/refinery?sslmode=require")
486512
.unwrap();
487-
assert!(config.use_tls().is_some());
488-
assert!(config.use_tls().unwrap());
513+
assert!(config_require.use_tls());
514+
515+
// Verify that manually created config matches parsed URL config
516+
let manual_config_disable = Config::new(ConfigDbType::Postgres)
517+
.set_db_user("root")
518+
.set_db_pass("1234")
519+
.set_db_host("localhost")
520+
.set_db_port("5432")
521+
.set_db_name("refinery")
522+
.set_use_tls(false);
523+
assert_eq!(config_disable.use_tls(), manual_config_disable.use_tls());
524+
525+
let manual_config_require = Config::new(ConfigDbType::Postgres)
526+
.set_db_user("root")
527+
.set_db_pass("1234")
528+
.set_db_host("localhost")
529+
.set_db_port("5432")
530+
.set_db_name("refinery")
531+
.set_use_tls(true);
532+
assert_eq!(config_require.use_tls(), manual_config_require.use_tls());
533+
489534
let config =
490535
Config::from_str("postgres://root:1234@localhost:5432/refinery?sslmode=invalidvalue");
491536
assert!(config.is_err());

refinery_core/src/drivers/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ macro_rules! with_connection {
9090
let path = build_db_url("postgresql", &$config);
9191

9292
let conn;
93-
if $config.use_tls().is_some() && $config.use_tls().unwrap() {
93+
if $config.use_tls() {
9494
let connector = native_tls::TlsConnector::new().unwrap();
9595
let connector = postgres_native_tls::MakeTlsConnector::new(connector);
9696
conn = postgres::Client::connect(path.as_str(), connector).migration_err("could not connect to database", None)?;
@@ -138,7 +138,7 @@ macro_rules! with_connection_async {
138138
cfg_if::cfg_if! {
139139
if #[cfg(feature = "tokio-postgres")] {
140140
let path = build_db_url("postgresql", $config);
141-
if $config.use_tls().is_some() && $config.use_tls().unwrap() {
141+
if $config.use_tls() {
142142
let connector = native_tls::TlsConnector::new().unwrap();
143143
let connector = postgres_native_tls::MakeTlsConnector::new(connector);
144144
let (client, connection) = tokio_postgres::connect(path.as_str(), connector).await.migration_err("could not connect to database", None)?;

0 commit comments

Comments
 (0)