Skip to content

Commit 2b984c7

Browse files
committed
chore(coprocessor): append always the default app_name
1 parent 8ea42e4 commit 2b984c7

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

coprocessor/fhevm-engine/fhevm-engine-common/src/utils.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ pub struct DatabaseURL(String);
121121

122122
impl From<&str> for DatabaseURL {
123123
fn from(s: &str) -> Self {
124-
let url: Self = Self(s.to_owned());
125-
url.parse().expect("DatabaseURL should be valid");
126-
url
124+
let url = s.to_owned();
125+
let app_name = Self::default_app_name();
126+
Self::new_with_app_name(&url, &app_name)
127127
}
128128
}
129129
impl From<String> for DatabaseURL {
130130
fn from(s: String) -> Self {
131-
let url: Self = Self(s);
132-
url.parse().expect("DatabaseURL should be valid");
133-
url
131+
let url = s.to_owned();
132+
let app_name = Self::default_app_name();
133+
Self::new_with_app_name(&url, &app_name)
134134
}
135135
}
136136

@@ -150,6 +150,11 @@ impl DatabaseURL {
150150
///
151151
/// application_name is useful for identifying the source of DB conns
152152
pub fn new_with_app_name(base: &str, app_name: &str) -> Self {
153+
let app_name = app_name.trim();
154+
if app_name.is_empty() {
155+
return Self(base.to_owned());
156+
}
157+
153158
// Append application_name if not present
154159
let mut url = base.to_owned();
155160
if !url.contains("application_name=") {
@@ -173,7 +178,7 @@ impl DatabaseURL {
173178
.file_name()
174179
.map(|s| s.to_string_lossy().into_owned())
175180
})
176-
.unwrap_or_else(|| "unknown".to_string())
181+
.unwrap_or_default()
177182
}
178183

179184
pub fn as_str(&self) -> &str {

coprocessor/fhevm-engine/fhevm-engine-common/tests/utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ async fn mask_database_url() {
3030
);
3131

3232
println!("DatabaseURL: {}", db_url);
33+
34+
let db_url: DatabaseURL =
35+
DatabaseURL::new_with_app_name("postgres://user:secret@dbhost:5432/mydb", " ");
36+
37+
assert_eq!(db_url.as_str(), "postgres://user:secret@dbhost:5432/mydb");
3338
}

0 commit comments

Comments
 (0)