1
1
use serde:: { Deserialize , Serialize } ;
2
- use torrust_tracker_primitives:: DatabaseDriver ;
3
2
use url:: Url ;
4
3
5
4
#[ allow( clippy:: struct_excessive_bools) ]
@@ -8,7 +7,7 @@ pub struct Database {
8
7
// Database configuration
9
8
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
10
9
#[ serde( default = "Database::default_driver" ) ]
11
- pub driver : DatabaseDriver ,
10
+ pub driver : Driver ,
12
11
13
12
/// Database connection string. The format depends on the database driver.
14
13
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
@@ -29,8 +28,8 @@ impl Default for Database {
29
28
}
30
29
31
30
impl Database {
32
- fn default_driver ( ) -> DatabaseDriver {
33
- DatabaseDriver :: Sqlite3
31
+ fn default_driver ( ) -> Driver {
32
+ Driver :: Sqlite3
34
33
}
35
34
36
35
fn default_path ( ) -> String {
@@ -44,10 +43,10 @@ impl Database {
44
43
/// Will panic if the database path for `MySQL` is not a valid URL.
45
44
pub fn mask_secrets ( & mut self ) {
46
45
match self . driver {
47
- DatabaseDriver :: Sqlite3 => {
46
+ Driver :: Sqlite3 => {
48
47
// Nothing to mask
49
48
}
50
- DatabaseDriver :: MySQL => {
49
+ Driver :: MySQL => {
51
50
let mut url = Url :: parse ( & self . path ) . expect ( "path for MySQL driver should be a valid URL" ) ;
52
51
url. set_password ( Some ( "***" ) ) . expect ( "url password should be changed" ) ;
53
52
self . path = url. to_string ( ) ;
@@ -56,17 +55,27 @@ impl Database {
56
55
}
57
56
}
58
57
58
+ /// The database management system used by the tracker.
59
+ #[ derive( Serialize , Deserialize , PartialEq , Eq , PartialOrd , Ord , Debug , Hash , Clone ) ]
60
+ pub enum Driver {
61
+ // todo:
62
+ // - Rename serialized values to lowercase: `sqlite3` and `mysql`.
63
+ // - Add serde default values.
64
+ /// The `Sqlite3` database driver.
65
+ Sqlite3 ,
66
+ /// The `MySQL` database driver.
67
+ MySQL ,
68
+ }
69
+
59
70
#[ cfg( test) ]
60
71
mod tests {
61
72
62
- use torrust_tracker_primitives:: DatabaseDriver ;
63
-
64
- use super :: Database ;
73
+ use super :: { Database , Driver } ;
65
74
66
75
#[ test]
67
76
fn it_should_allow_masking_the_mysql_user_password ( ) {
68
77
let mut database = Database {
69
- driver : DatabaseDriver :: MySQL ,
78
+ driver : Driver :: MySQL ,
70
79
path : "mysql://root:password@localhost:3306/torrust" . to_string ( ) ,
71
80
} ;
72
81
0 commit comments