Skip to content

Commit

Permalink
Usability improvement: leading forward slash empty database
Browse files Browse the repository at this point in the history
A leading forward slash in the connection URL makes
get_opts_db_name_from_url set db_name to Some("") rather than None.
This makes code pulling the db from config requiring extra checks as
db_name.is_some() will evaluate to true.
Empty database parameter is invalid for MySQL.
  • Loading branch information
altmannmarcelo committed Sep 8, 2023
1 parent f3cdaa0 commit 2a716a6
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,11 +1340,14 @@ fn get_opts_pass_from_url(url: &Url) -> Option<String> {

fn get_opts_db_name_from_url(url: &Url) -> Option<String> {
if let Some(mut segments) = url.path_segments() {
segments.next().map(|db_name| {
percent_decode(db_name.as_ref())
.decode_utf8_lossy()
.into_owned()
})
segments
.next()
.map(|db_name| {
percent_decode(db_name.as_ref())
.decode_utf8_lossy()
.into_owned()
})
.and_then(|db| if db.is_empty() { None } else { Some(db) })
} else {
None
}
Expand Down Expand Up @@ -1813,4 +1816,18 @@ mod test {
let opts = Opts::from_url("mysql://localhost/foo?compression=9").unwrap();
assert_eq!(opts.compression(), Some(crate::Compression::new(9)));
}

#[test]
fn test_builder_eq_url_empty_db() {
let builder = super::OptsBuilder::default();
let builder_opts = Opts::from(builder);

let url: &str = "mysql://iq-controller@localhost";
let url_opts = super::Opts::from_str(url).unwrap();
assert_eq!(url_opts.db_name(), builder_opts.db_name());

let url: &str = "mysql://iq-controller@localhost/";
let url_opts = super::Opts::from_str(url).unwrap();
assert_eq!(url_opts.db_name(), builder_opts.db_name());
}
}

0 comments on commit 2a716a6

Please sign in to comment.