Skip to content

Commit

Permalink
refactor: [#950] rename DatabaseDriver to Driver
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jul 5, 2024
1 parent 954295a commit d970bb8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
17 changes: 8 additions & 9 deletions src/core/databases/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use super::{Builder, Database};
/// - [Torrust Tracker](https://docs.rs/torrust-tracker).
///
/// For more information about persistence.
#[allow(clippy::module_name_repetitions)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)]
pub enum DatabaseDriver {
pub enum Driver {
/// The Sqlite3 database driver.
Sqlite3,
/// The `MySQL` database driver.
Expand All @@ -32,9 +31,9 @@ pub enum DatabaseDriver {
///
/// ```rust,no_run
/// use torrust_tracker::core::databases;
/// use torrust_tracker::core::databases::driver::DatabaseDriver;
/// use torrust_tracker::core::databases::driver::Driver;
///
/// let db_driver = DatabaseDriver::Sqlite3;
/// let db_driver = Driver::Sqlite3;
/// let db_path = "./storage/tracker/lib/database/sqlite3.db".to_string();
/// let database = databases::driver::build(&db_driver, &db_path);
/// ```
Expand All @@ -43,9 +42,9 @@ pub enum DatabaseDriver {
///
/// ```rust,no_run
/// use torrust_tracker::core::databases;
/// use torrust_tracker::core::databases::driver::DatabaseDriver;
/// use torrust_tracker::core::databases::driver::Driver;
///
/// let db_driver = DatabaseDriver::MySQL;
/// let db_driver = Driver::MySQL;
/// let db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker".to_string();
/// let database = databases::driver::build(&db_driver, &db_path);
/// ```
Expand All @@ -62,10 +61,10 @@ pub enum DatabaseDriver {
/// # Panics
///
/// This function will panic if unable to create database tables.
pub fn build(driver: &DatabaseDriver, db_path: &str) -> Result<Box<dyn Database>, Error> {
pub fn build(driver: &Driver, db_path: &str) -> Result<Box<dyn Database>, Error> {
let database = match driver {
DatabaseDriver::Sqlite3 => Builder::<Sqlite>::build(db_path),
DatabaseDriver::MySQL => Builder::<Mysql>::build(db_path),
Driver::Sqlite3 => Builder::<Sqlite>::build(db_path),
Driver::MySQL => Builder::<Mysql>::build(db_path),
}?;

database.create_database_tables().expect("Could not create database tables.");
Expand Down
26 changes: 13 additions & 13 deletions src/core/databases/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@ use std::sync::Arc;
use r2d2_mysql::mysql::UrlError;
use torrust_tracker_located_error::{DynError, Located, LocatedError};

use super::driver::DatabaseDriver;
use super::driver::Driver;

#[derive(thiserror::Error, Debug, Clone)]
pub enum Error {
/// The query unexpectedly returned nothing.
#[error("The {driver} query unexpectedly returned nothing: {source}")]
QueryReturnedNoRows {
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
driver: DatabaseDriver,
driver: Driver,
},

/// The query was malformed.
#[error("The {driver} query was malformed: {source}")]
InvalidQuery {
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
driver: DatabaseDriver,
driver: Driver,
},

/// Unable to insert a record into the database
#[error("Unable to insert record into {driver} database, {location}")]
InsertFailed {
location: &'static Location<'static>,
driver: DatabaseDriver,
driver: Driver,
},

/// Unable to delete a record into the database
#[error("Failed to remove record from {driver} database, error-code: {error_code}, {location}")]
DeleteFailed {
location: &'static Location<'static>,
error_code: usize,
driver: DatabaseDriver,
driver: Driver,
},

/// Unable to connect to the database
#[error("Failed to connect to {driver} database: {source}")]
ConnectionError {
source: LocatedError<'static, UrlError>,
driver: DatabaseDriver,
driver: Driver,
},

/// Unable to create a connection pool
#[error("Failed to create r2d2 {driver} connection pool: {source}")]
ConnectionPool {
source: LocatedError<'static, r2d2::Error>,
driver: DatabaseDriver,
driver: Driver,
},
}

Expand All @@ -61,11 +61,11 @@ impl From<r2d2_sqlite::rusqlite::Error> for Error {
match err {
r2d2_sqlite::rusqlite::Error::QueryReturnedNoRows => Error::QueryReturnedNoRows {
source: (Arc::new(err) as DynError).into(),
driver: DatabaseDriver::Sqlite3,
driver: Driver::Sqlite3,
},
_ => Error::InvalidQuery {
source: (Arc::new(err) as DynError).into(),
driver: DatabaseDriver::Sqlite3,
driver: Driver::Sqlite3,
},
}
}
Expand All @@ -77,7 +77,7 @@ impl From<r2d2_mysql::mysql::Error> for Error {
let e: DynError = Arc::new(err);
Error::InvalidQuery {
source: e.into(),
driver: DatabaseDriver::MySQL,
driver: Driver::MySQL,
}
}
}
Expand All @@ -87,14 +87,14 @@ impl From<UrlError> for Error {
fn from(err: UrlError) -> Self {
Self::ConnectionError {
source: Located(err).into(),
driver: DatabaseDriver::MySQL,
driver: Driver::MySQL,
}
}
}

impl From<(r2d2::Error, DatabaseDriver)> for Error {
impl From<(r2d2::Error, Driver)> for Error {
#[track_caller]
fn from(e: (r2d2::Error, DatabaseDriver)) -> Self {
fn from(e: (r2d2::Error, Driver)) -> Self {
let (err, driver) = e;
Self::ConnectionPool {
source: Located(err).into(),
Expand Down
4 changes: 2 additions & 2 deletions src/core/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use torrust_tracker_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::PersistentTorrents;
use tracing::debug;

use super::driver::DatabaseDriver;
use super::driver::Driver;
use super::{Database, Error};
use crate::core::auth::{self, Key};
use crate::shared::bit_torrent::common::AUTH_KEY_LENGTH;

const DRIVER: DatabaseDriver = DatabaseDriver::MySQL;
const DRIVER: Driver = Driver::MySQL;

pub struct Mysql {
pool: Pool<MySqlConnectionManager>,
Expand Down
6 changes: 3 additions & 3 deletions src/core/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use r2d2_sqlite::SqliteConnectionManager;
use torrust_tracker_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::{DurationSinceUnixEpoch, PersistentTorrents};

use super::driver::DatabaseDriver;
use super::driver::Driver;
use super::{Database, Error};
use crate::core::auth::{self, Key};

const DRIVER: DatabaseDriver = DatabaseDriver::Sqlite3;
const DRIVER: Driver = Driver::Sqlite3;

pub struct Sqlite {
pool: Pool<SqliteConnectionManager>,
Expand All @@ -29,7 +29,7 @@ impl Database for Sqlite {
/// Will return `r2d2::Error` if `db_path` is not able to create `SqLite` database.
fn new(db_path: &str) -> Result<Sqlite, Error> {
let cm = SqliteConnectionManager::file(db_path);
Pool::new(cm).map_or_else(|err| Err((err, DatabaseDriver::Sqlite3).into()), |pool| Ok(Sqlite { pool }))
Pool::new(cm).map_or_else(|err| Err((err, Driver::Sqlite3).into()), |pool| Ok(Sqlite { pool }))
}

/// Refer to [`databases::Database::create_database_tables`](crate::core::databases::Database::create_database_tables).
Expand Down
6 changes: 3 additions & 3 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ use std::panic::Location;
use std::sync::Arc;
use std::time::Duration;

use databases::driver::DatabaseDriver;
use databases::driver::Driver;
use derive_more::Constructor;
use tokio::sync::mpsc::error::SendError;
use torrust_tracker_clock::clock::Time;
Expand Down Expand Up @@ -567,8 +567,8 @@ impl Tracker {
stats_repository: statistics::Repo,
) -> Result<Tracker, databases::error::Error> {
let driver = match config.database.driver {
database::Driver::Sqlite3 => DatabaseDriver::Sqlite3,
database::Driver::MySQL => DatabaseDriver::MySQL,
database::Driver::Sqlite3 => Driver::Sqlite3,
database::Driver::MySQL => Driver::MySQL,
};

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

0 comments on commit d970bb8

Please sign in to comment.