Skip to content

Commit

Permalink
Fix Config::url handling
Browse files Browse the repository at this point in the history
This closes #290
  • Loading branch information
bikeshedder committed Dec 18, 2023
1 parent 7869a0b commit 24c6051
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 4 additions & 0 deletions postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## unreleased

- Fix `Config::url` handling

## v0.12.0

- Add `load_balance_hosts` to `Config` struct.
Expand Down
30 changes: 19 additions & 11 deletions postgres/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,29 @@ impl Config {
} else {
tokio_postgres::Config::new()
};
if let Some(user) = &self.user {
cfg.user(user.as_str());
} else if let Ok(user) = env::var("USER") {
if let Some(user) = self.user.as_ref().filter(|s| !s.is_empty()) {
cfg.user(user.as_str());
}
if !cfg.get_user().is_some_and(|s| !s.is_empty()) {
if let Ok(user) = env::var("USER") {
cfg.user(&user);
}
}
if let Some(password) = &self.password {
cfg.password(password);
}
match &self.dbname {
Some(dbname) => match dbname.as_str() {
"" => return Err(ConfigError::DbnameMissing),
dbname => cfg.dbname(dbname),
},
None => return Err(ConfigError::DbnameEmpty),
};
if let Some(dbname) = self.dbname.as_ref().filter(|s| !s.is_empty()) {
cfg.dbname(dbname);
}
match cfg.get_dbname() {
None => {
return Err(ConfigError::DbnameMissing);
}
Some(s) if s.is_empty() => {
return Err(ConfigError::DbnameEmpty);
}
_ => {}
}
if let Some(options) = &self.options {
cfg.options(options.as_str());
}
Expand All @@ -228,7 +236,7 @@ impl Config {
cfg.host(host.as_str());
}
}
if self.host.is_none() && self.hosts.is_none() {
if cfg.get_hosts().is_empty() {
// Systems that support it default to unix domain sockets.
#[cfg(unix)]
{
Expand Down

0 comments on commit 24c6051

Please sign in to comment.