Skip to content

Commit 9be9638

Browse files
committed
refactor: [#950] decouple database driver enum
Use a different enum for configuration and domain.
1 parent c747321 commit 9be9638

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

packages/configuration/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub type HttpApi = v2::tracker_api::HttpApi;
4141
pub type HttpTracker = v2::http_tracker::HttpTracker;
4242
pub type UdpTracker = v2::udp_tracker::UdpTracker;
4343
pub type Database = v2::database::Database;
44+
pub type Driver = v2::database::Driver;
4445
pub type Threshold = v2::logging::Threshold;
4546

4647
pub type AccessTokens = HashMap<String, String>;

packages/configuration/src/v2/database.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use serde::{Deserialize, Serialize};
2-
use torrust_tracker_primitives::DatabaseDriver;
32
use url::Url;
43

54
#[allow(clippy::struct_excessive_bools)]
@@ -8,7 +7,7 @@ pub struct Database {
87
// Database configuration
98
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
109
#[serde(default = "Database::default_driver")]
11-
pub driver: DatabaseDriver,
10+
pub driver: Driver,
1211

1312
/// Database connection string. The format depends on the database driver.
1413
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
@@ -29,8 +28,8 @@ impl Default for Database {
2928
}
3029

3130
impl Database {
32-
fn default_driver() -> DatabaseDriver {
33-
DatabaseDriver::Sqlite3
31+
fn default_driver() -> Driver {
32+
Driver::Sqlite3
3433
}
3534

3635
fn default_path() -> String {
@@ -44,10 +43,10 @@ impl Database {
4443
/// Will panic if the database path for `MySQL` is not a valid URL.
4544
pub fn mask_secrets(&mut self) {
4645
match self.driver {
47-
DatabaseDriver::Sqlite3 => {
46+
Driver::Sqlite3 => {
4847
// Nothing to mask
4948
}
50-
DatabaseDriver::MySQL => {
49+
Driver::MySQL => {
5150
let mut url = Url::parse(&self.path).expect("path for MySQL driver should be a valid URL");
5251
url.set_password(Some("***")).expect("url password should be changed");
5352
self.path = url.to_string();
@@ -56,17 +55,27 @@ impl Database {
5655
}
5756
}
5857

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+
5970
#[cfg(test)]
6071
mod tests {
6172

62-
use torrust_tracker_primitives::DatabaseDriver;
63-
64-
use super::Database;
73+
use super::{Database, Driver};
6574

6675
#[test]
6776
fn it_should_allow_masking_the_mysql_user_password() {
6877
let mut database = Database {
69-
driver: DatabaseDriver::MySQL,
78+
driver: Driver::MySQL,
7079
path: "mysql://root:password@localhost:3306/torrust".to_string(),
7180
};
7281

packages/primitives/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ pub struct NumberOfBytes(pub i64);
5252
/// For more information about persistence.
5353
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)]
5454
pub enum DatabaseDriver {
55-
// TODO:
56-
// - Move to the database crate once that gets its own crate.
57-
// - Rename serialized values to lowercase: `sqlite3` and `mysql`.
5855
/// The Sqlite3 database driver.
5956
Sqlite3,
6057
/// The `MySQL` database driver.

src/core/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,12 @@ use std::time::Duration;
456456
use derive_more::Constructor;
457457
use tokio::sync::mpsc::error::SendError;
458458
use torrust_tracker_clock::clock::Time;
459+
use torrust_tracker_configuration::v2::database;
459460
use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
460461
use torrust_tracker_primitives::info_hash::InfoHash;
461-
use torrust_tracker_primitives::peer;
462462
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
463463
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
464+
use torrust_tracker_primitives::{peer, DatabaseDriver};
464465
use torrust_tracker_torrent_repository::entry::EntrySync;
465466
use torrust_tracker_torrent_repository::repository::Repository;
466467
use tracing::debug;
@@ -564,7 +565,12 @@ impl Tracker {
564565
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
565566
stats_repository: statistics::Repo,
566567
) -> Result<Tracker, databases::error::Error> {
567-
let database = Arc::new(databases::driver::build(&config.database.driver, &config.database.path)?);
568+
let driver = match config.database.driver {
569+
database::Driver::Sqlite3 => DatabaseDriver::Sqlite3,
570+
database::Driver::MySQL => DatabaseDriver::MySQL,
571+
};
572+
573+
let database = Arc::new(databases::driver::build(&driver, &config.database.path)?);
568574

569575
Ok(Tracker {
570576
config: config.clone(),

0 commit comments

Comments
 (0)