Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option for using unix_domain_socket #158

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 190 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Default for Config {
path: None,
password: None,
database: None,
unix_domain_socket: None,
}],
key_config: KeyConfig::default(),
log_level: LogLevel::default(),
Expand All @@ -74,6 +75,7 @@ pub struct Connection {
port: Option<u64>,
path: Option<std::path::PathBuf>,
password: Option<String>,
unix_domain_socket: Option<std::path::PathBuf>,
pub database: Option<String>,
}

Expand Down Expand Up @@ -199,22 +201,27 @@ impl Connection {
.password
.as_ref()
.map_or(String::new(), |p| p.to_string());
let unix_domain_socket = self
.valid_unix_domain_socket()
.map_or(String::new(), |uds| format!("?socket={}", uds));

match self.database.as_ref() {
Some(database) => Ok(format!(
"mysql://{user}:{password}@{host}:{port}/{database}",
"mysql://{user}:{password}@{host}:{port}/{database}{unix_domain_socket}",
Copy link
Contributor Author

@kyoto7250 kyoto7250 Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user = user,
password = password,
host = host,
database = database,
port = port,
database = database
unix_domain_socket = unix_domain_socket,
)),
None => Ok(format!(
"mysql://{user}:{password}@{host}:{port}",
"mysql://{user}:{password}@{host}:{port}{unix_domain_socket}",
user = user,
password = password,
host = host,
port = port,
unix_domain_socket = unix_domain_socket,
)),
}
}
Expand All @@ -236,22 +243,40 @@ impl Connection {
.as_ref()
.map_or(String::new(), |p| p.to_string());

match self.database.as_ref() {
Some(database) => Ok(format!(
"postgres://{user}:{password}@{host}:{port}/{database}",
user = user,
password = password,
host = host,
port = port,
database = database
)),
None => Ok(format!(
"postgres://{user}:{password}@{host}:{port}",
user = user,
password = password,
host = host,
port = port,
)),
if let Some(unix_domain_socket) = self.valid_unix_domain_socket() {
match self.database.as_ref() {
Some(database) => Ok(format!(
"postgres://?dbname={database}&host={unix_domain_socket}&user={user}&password={password}",
database = database,
unix_domain_socket = unix_domain_socket,
user = user,
password = password,
)),
None => Ok(format!(
"postgres://?host={unix_domain_socket}&user={user}&password={password}",
unix_domain_socket = unix_domain_socket,
user = user,
password = password,
)),
}
} else {
match self.database.as_ref() {
Some(database) => Ok(format!(
"postgres://{user}:{password}@{host}:{port}/{database}",
user = user,
password = password,
host = host,
port = port,
database = database,
)),
None => Ok(format!(
"postgres://{user}:{password}@{host}:{port}",
user = user,
password = password,
host = host,
port = port,
)),
}
}
}
DatabaseType::Sqlite => {
Expand Down Expand Up @@ -287,6 +312,23 @@ impl Connection {
pub fn is_postgres(&self) -> bool {
matches!(self.r#type, DatabaseType::Postgres)
}

fn valid_unix_domain_socket(&self) -> Option<String> {
if cfg!(windows) {
// NOTE:
// windows also supports UDS, but `rust` does not support UDS in windows now.
// https://github.com/rust-lang/rust/issues/56533
return None;
}
return self.unix_domain_socket.as_ref().and_then(|uds| {
let path = expand_path(uds)?;
let path_str = path.to_str()?;
if path_str.is_empty() {
return None;
}
Some(path_str.to_owned())
});
}
}

pub fn get_app_config_path() -> anyhow::Result<std::path::PathBuf> {
Expand Down Expand Up @@ -325,7 +367,7 @@ fn expand_path(path: &Path) -> Option<PathBuf> {

#[cfg(test)]
mod test {
use super::{expand_path, KeyConfig, Path, PathBuf};
use super::{expand_path, Connection, DatabaseType, KeyConfig, Path, PathBuf};
use serde_json::Value;
use std::env;

Expand All @@ -349,6 +391,134 @@ mod test {
}
}

#[test]
#[cfg(unix)]
fn test_dataset_url_in_unix() {
let mut mysql_conn = Connection {
r#type: DatabaseType::MySql,
name: None,
user: Some("root".to_owned()),
host: Some("localhost".to_owned()),
port: Some(3306),
path: None,
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
};

assert_eq!(
mysql_conn.database_url().unwrap(),
"mysql://root:password@localhost:3306/city".to_owned()
);

mysql_conn.unix_domain_socket = Some(Path::new("/tmp/mysql.sock").to_path_buf());
assert_eq!(
mysql_conn.database_url().unwrap(),
"mysql://root:password@localhost:3306/city?socket=/tmp/mysql.sock".to_owned()
);

let mut postgres_conn = Connection {
r#type: DatabaseType::Postgres,
name: None,
user: Some("root".to_owned()),
host: Some("localhost".to_owned()),
port: Some(3306),
path: None,
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
};

assert_eq!(
postgres_conn.database_url().unwrap(),
"postgres://root:password@localhost:3306/city".to_owned()
);
postgres_conn.unix_domain_socket = Some(Path::new("/tmp").to_path_buf());
assert_eq!(
postgres_conn.database_url().unwrap(),
"postgres://?dbname=city&host=/tmp&user=root&password=password".to_owned()
);

let sqlite_conn = Connection {
r#type: DatabaseType::Sqlite,
name: None,
user: None,
host: None,
port: None,
path: Some(PathBuf::from("/home/user/sqlite3.db")),
password: None,
database: None,
unix_domain_socket: None,
};

let sqlite_result = sqlite_conn.database_url().unwrap();
assert_eq!(sqlite_result, "sqlite:///home/user/sqlite3.db".to_owned());
}

#[test]
#[cfg(windows)]
fn test_database_url_in_windows() {
let mut mysql_conn = Connection {
r#type: DatabaseType::MySql,
name: None,
user: Some("root".to_owned()),
host: Some("localhost".to_owned()),
port: Some(3306),
path: None,
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
};

assert_eq!(
mysql_conn.database_url().unwrap(),
"mysql://root:password@localhost:3306/city".to_owned()
);

mysql_conn.unix_domain_socket = Some(Path::new("/tmp/mysql.sock").to_path_buf());
assert_eq!(
mysql_conn.database_url().unwrap(),
"mysql://root:password@localhost:3306/city".to_owned()
);

let mut postgres_conn = Connection {
r#type: DatabaseType::Postgres,
name: None,
user: Some("root".to_owned()),
host: Some("localhost".to_owned()),
port: Some(3306),
path: None,
password: Some("password".to_owned()),
database: Some("city".to_owned()),
unix_domain_socket: None,
};

assert_eq!(
postgres_conn.database_url().unwrap(),
"postgres://root:password@localhost:3306/city".to_owned()
);
postgres_conn.unix_domain_socket = Some(Path::new("/tmp").to_path_buf());
assert_eq!(
postgres_conn.database_url().unwrap(),
"postgres://root:password@localhost:3306/city".to_owned()
);

let sqlite_conn = Connection {
r#type: DatabaseType::Sqlite,
name: None,
user: None,
host: None,
port: None,
path: Some(PathBuf::from("/home/user/sqlite3.db")),
password: None,
database: None,
unix_domain_socket: None,
};

let sqlite_result = sqlite_conn.database_url().unwrap();
assert_eq!(sqlite_result, "sqlite://\\home\\user\\sqlite3.db".to_owned());
}

#[test]
#[cfg(unix)]
fn test_expand_path() {
Expand Down