Skip to content

Commit

Permalink
refactor: [#950] decouple database driver enum
Browse files Browse the repository at this point in the history
Use a different enum for configuration and domain.
  • Loading branch information
josecelano committed Jul 5, 2024
1 parent c747321 commit 9be9638
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub type HttpApi = v2::tracker_api::HttpApi;
pub type HttpTracker = v2::http_tracker::HttpTracker;
pub type UdpTracker = v2::udp_tracker::UdpTracker;
pub type Database = v2::database::Database;
pub type Driver = v2::database::Driver;
pub type Threshold = v2::logging::Threshold;

pub type AccessTokens = HashMap<String, String>;
Expand Down
29 changes: 19 additions & 10 deletions packages/configuration/src/v2/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::DatabaseDriver;
use url::Url;

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

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

impl Database {
fn default_driver() -> DatabaseDriver {
DatabaseDriver::Sqlite3
fn default_driver() -> Driver {
Driver::Sqlite3
}

fn default_path() -> String {
Expand All @@ -44,10 +43,10 @@ impl Database {
/// Will panic if the database path for `MySQL` is not a valid URL.
pub fn mask_secrets(&mut self) {
match self.driver {
DatabaseDriver::Sqlite3 => {
Driver::Sqlite3 => {
// Nothing to mask
}
DatabaseDriver::MySQL => {
Driver::MySQL => {
let mut url = Url::parse(&self.path).expect("path for MySQL driver should be a valid URL");
url.set_password(Some("***")).expect("url password should be changed");
self.path = url.to_string();
Expand All @@ -56,17 +55,27 @@ impl Database {
}
}

/// The database management system used by the tracker.
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)]
pub enum Driver {
// todo:
// - Rename serialized values to lowercase: `sqlite3` and `mysql`.
// - Add serde default values.
/// The `Sqlite3` database driver.
Sqlite3,
/// The `MySQL` database driver.
MySQL,
}

#[cfg(test)]
mod tests {

use torrust_tracker_primitives::DatabaseDriver;

use super::Database;
use super::{Database, Driver};

#[test]
fn it_should_allow_masking_the_mysql_user_password() {
let mut database = Database {
driver: DatabaseDriver::MySQL,
driver: Driver::MySQL,
path: "mysql://root:password@localhost:3306/torrust".to_string(),
};

Expand Down
3 changes: 0 additions & 3 deletions packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ pub struct NumberOfBytes(pub i64);
/// For more information about persistence.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)]
pub enum DatabaseDriver {
// TODO:
// - Move to the database crate once that gets its own crate.
// - Rename serialized values to lowercase: `sqlite3` and `mysql`.
/// The Sqlite3 database driver.
Sqlite3,
/// The `MySQL` database driver.
Expand Down
10 changes: 8 additions & 2 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,12 @@ use std::time::Duration;
use derive_more::Constructor;
use tokio::sync::mpsc::error::SendError;
use torrust_tracker_clock::clock::Time;
use torrust_tracker_configuration::v2::database;
use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
use torrust_tracker_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::peer;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DatabaseDriver};
use torrust_tracker_torrent_repository::entry::EntrySync;
use torrust_tracker_torrent_repository::repository::Repository;
use tracing::debug;
Expand Down Expand Up @@ -564,7 +565,12 @@ impl Tracker {
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
stats_repository: statistics::Repo,
) -> Result<Tracker, databases::error::Error> {
let database = Arc::new(databases::driver::build(&config.database.driver, &config.database.path)?);
let driver = match config.database.driver {
database::Driver::Sqlite3 => DatabaseDriver::Sqlite3,
database::Driver::MySQL => DatabaseDriver::MySQL,
};

let database = Arc::new(databases::driver::build(&driver, &config.database.path)?);

Ok(Tracker {
config: config.clone(),
Expand Down

0 comments on commit 9be9638

Please sign in to comment.