@@ -121,16 +121,16 @@ pub struct DatabaseURL(String);
121121
122122impl 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}
129129impl 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 {
0 commit comments