Skip to content

Commit 035d630

Browse files
committed
Merge torrust#953: Config overhaul: lowercase for DatabaseDriver
ca348a8 chore: remove unused dependency (Jose Celano) 9d72f51 feat: [torrust#950] use lowercase for database driver values in configuration (Jose Celano) d970bb8 refactor: [torrust#950] rename DatabaseDriver to Driver (Jose Celano) 954295a refactor: [torrust#950] move DatabaseDriver to databases mod (Jose Celano) 9be9638 refactor: [torrust#950] decouple database driver enum (Jose Celano) Pull request description: Use lowercase for database driver values in the configuration: ```toml [core.database] driver = "sqlite3" #driver = "MySQL" ``` Instead of: ```toml [core.database] driver = "Sqlite3" #driver = "MySql" ``` We are normalizing all enum variants in the configuration to lowercase. It also decouples the internal database driver enum from the enum used in the configuration. ACKs for top commit: josecelano: ACK ca348a8 Tree-SHA512: 499ed0b628e385f7927d2bea50334c68eece6fe2e6b0170bf372e3db7d88837fb908fdbf0e94d7f7144141a1d985732b21694515e5551155bd8d7cbe9e58bb15
2 parents c747321 + ca348a8 commit 035d630

File tree

17 files changed

+85
-72
lines changed

17 files changed

+85
-72
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Containerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ RUN ["/busybox/cp", "-sp", "/busybox/sh","/busybox/cat","/busybox/ls","/busybox/
9696
COPY --from=gcc --chmod=0555 /usr/local/bin/su-exec /bin/su-exec
9797

9898
ARG TORRUST_TRACKER_CONFIG_TOML_PATH="/etc/torrust/tracker/tracker.toml"
99-
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER="Sqlite3"
99+
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER="sqlite3"
100100
ARG USER_ID=1000
101101
ARG UDP_PORT=6969
102102
ARG HTTP_PORT=7070

compose.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
image: torrust-tracker:release
55
tty: true
66
environment:
7-
- TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER:-MySQL}
7+
- TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER:-mysql}
88
- TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN=${TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN:-MyAccessToken}
99
networks:
1010
- server_side

docs/containers.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ The following environmental variables can be set:
149149

150150
- `TORRUST_TRACKER_CONFIG_TOML_PATH` - The in-container path to the tracker configuration file, (default: `"/etc/torrust/tracker/tracker.toml"`).
151151
- `TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN` - Override of the admin token. If set, this value overrides any value set in the config.
152-
- `TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER` - The database type used for the container, (options: `Sqlite3`, `MySQL`, default `Sqlite3`). Please Note: This dose not override the database configuration within the `.toml` config file.
152+
- `TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER` - The database type used for the container, (options: `sqlite3`, `mysql`, default `sqlite3`). Please Note: This dose not override the database configuration within the `.toml` config file.
153153
- `TORRUST_TRACKER_CONFIG_TOML` - Load config from this environmental variable instead from a file, (i.e: `TORRUST_TRACKER_CONFIG_TOML=$(cat tracker-tracker.toml)`).
154154
- `USER_ID` - The user id for the runtime crated `torrust` user. Please Note: This user id should match the ownership of the host-mapped volumes, (default `1000`).
155155
- `UDP_PORT` - The port for the UDP tracker. This should match the port used in the configuration, (default `6969`).
@@ -244,7 +244,7 @@ The docker-compose configuration includes the MySQL service configuration. If yo
244244

245245
```toml
246246
[core.database]
247-
driver = "MySQL"
247+
driver = "mysql"
248248
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
249249
```
250250

packages/configuration/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ serde_with = "3"
2424
thiserror = "1"
2525
toml = "0"
2626
torrust-tracker-located-error = { version = "3.0.0-alpha.12-develop", path = "../located-error" }
27-
torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../primitives" }
2827
url = "2.5.2"
2928

3029
[dev-dependencies]

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-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use serde::{Deserialize, Serialize};
2-
use torrust_tracker_primitives::DatabaseDriver;
32
use url::Url;
43

54
#[allow(clippy::struct_excessive_bools)]
65
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
76
pub struct Database {
87
// Database configuration
9-
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
8+
/// 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.
14-
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
13+
/// For `sqlite3`, the format is `path/to/database.db`, for example:
1514
/// `./storage/tracker/lib/database/sqlite3.db`.
1615
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
1716
/// example: `mysql://root:password@localhost:3306/torrust`.
@@ -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,25 @@ 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+
#[serde(rename_all = "lowercase")]
61+
pub enum Driver {
62+
/// The `Sqlite3` database driver.
63+
Sqlite3,
64+
/// The `MySQL` database driver.
65+
MySQL,
66+
}
67+
5968
#[cfg(test)]
6069
mod tests {
6170

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

6673
#[test]
6774
fn it_should_allow_masking_the_mysql_user_password() {
6875
let mut database = Database {
69-
driver: DatabaseDriver::MySQL,
76+
driver: Driver::MySQL,
7077
path: "mysql://root:password@localhost:3306/torrust".to_string(),
7178
};
7279

packages/configuration/src/v2/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
//! interval_min = 120
210210
//!
211211
//! [core.database]
212-
//! driver = "Sqlite3"
212+
//! driver = "sqlite3"
213213
//! path = "./storage/tracker/lib/database/sqlite3.db"
214214
//!
215215
//! [core.net]
@@ -420,7 +420,7 @@ mod tests {
420420
interval_min = 120
421421
422422
[core.database]
423-
driver = "Sqlite3"
423+
driver = "sqlite3"
424424
path = "./storage/tracker/lib/database/sqlite3.db"
425425
426426
[core.net]

packages/primitives/src/lib.rs

-19
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,4 @@ pub enum IPVersion {
4242
#[derive(Hash, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
4343
pub struct NumberOfBytes(pub i64);
4444

45-
/// The database management system used by the tracker.
46-
///
47-
/// Refer to:
48-
///
49-
/// - [Torrust Tracker Configuration](https://docs.rs/torrust-tracker-configuration).
50-
/// - [Torrust Tracker](https://docs.rs/torrust-tracker).
51-
///
52-
/// For more information about persistence.
53-
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)]
54-
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`.
58-
/// The Sqlite3 database driver.
59-
Sqlite3,
60-
/// The `MySQL` database driver.
61-
MySQL,
62-
}
63-
6445
pub type PersistentTorrents = BTreeMap<InfoHash, u32>;

share/container/entry_script_sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ chmod -R 2770 /var/lib/torrust /var/log/torrust /etc/torrust
2727

2828
# Install the database and config:
2929
if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" ]; then
30-
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" "Sqlite3"; then
30+
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" "sqlite3"; then
3131

3232
# Select Sqlite3 empty database
3333
default_database="/usr/share/torrust/default/database/tracker.sqlite3.db"
3434

3535
# Select Sqlite3 default configuration
3636
default_config="/usr/share/torrust/default/config/tracker.container.sqlite3.toml"
3737

38-
elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" "MySQL"; then
38+
elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" "mysql"; then
3939

4040
# (no database file needed for MySQL)
4141

@@ -44,7 +44,7 @@ if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" ]; then
4444

4545
else
4646
echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER\"."
47-
echo "Please Note: Supported Database Types: \"Sqlite3\", \"MySQL\"."
47+
echo "Please Note: Supported Database Types: \"sqlite3\", \"mysql\"."
4848
exit 1
4949
fi
5050
else

share/default/config/tracker.container.mysql.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version = "2"
22

33
[core.database]
4-
driver = "MySQL"
4+
driver = "mysql"
55
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
66

77
# Uncomment to enable services

src/core/databases/driver.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,38 @@
22
//!
33
//! See [`databases::driver::build`](crate::core::databases::driver::build)
44
//! function for more information.
5-
use torrust_tracker_primitives::DatabaseDriver;
5+
use serde::{Deserialize, Serialize};
66

77
use super::error::Error;
88
use super::mysql::Mysql;
99
use super::sqlite::Sqlite;
1010
use super::{Builder, Database};
1111

12+
/// The database management system used by the tracker.
13+
///
14+
/// Refer to:
15+
///
16+
/// - [Torrust Tracker Configuration](https://docs.rs/torrust-tracker-configuration).
17+
/// - [Torrust Tracker](https://docs.rs/torrust-tracker).
18+
///
19+
/// For more information about persistence.
20+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)]
21+
pub enum Driver {
22+
/// The Sqlite3 database driver.
23+
Sqlite3,
24+
/// The `MySQL` database driver.
25+
MySQL,
26+
}
27+
1228
/// It builds a new database driver.
1329
///
1430
/// Example for `SQLite3`:
1531
///
1632
/// ```rust,no_run
1733
/// use torrust_tracker::core::databases;
18-
/// use torrust_tracker_primitives::DatabaseDriver;
34+
/// use torrust_tracker::core::databases::driver::Driver;
1935
///
20-
/// let db_driver = DatabaseDriver::Sqlite3;
36+
/// let db_driver = Driver::Sqlite3;
2137
/// let db_path = "./storage/tracker/lib/database/sqlite3.db".to_string();
2238
/// let database = databases::driver::build(&db_driver, &db_path);
2339
/// ```
@@ -26,9 +42,9 @@ use super::{Builder, Database};
2642
///
2743
/// ```rust,no_run
2844
/// use torrust_tracker::core::databases;
29-
/// use torrust_tracker_primitives::DatabaseDriver;
45+
/// use torrust_tracker::core::databases::driver::Driver;
3046
///
31-
/// let db_driver = DatabaseDriver::MySQL;
47+
/// let db_driver = Driver::MySQL;
3248
/// let db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker".to_string();
3349
/// let database = databases::driver::build(&db_driver, &db_path);
3450
/// ```
@@ -45,10 +61,10 @@ use super::{Builder, Database};
4561
/// # Panics
4662
///
4763
/// This function will panic if unable to create database tables.
48-
pub fn build(driver: &DatabaseDriver, db_path: &str) -> Result<Box<dyn Database>, Error> {
64+
pub fn build(driver: &Driver, db_path: &str) -> Result<Box<dyn Database>, Error> {
4965
let database = match driver {
50-
DatabaseDriver::Sqlite3 => Builder::<Sqlite>::build(db_path),
51-
DatabaseDriver::MySQL => Builder::<Mysql>::build(db_path),
66+
Driver::Sqlite3 => Builder::<Sqlite>::build(db_path),
67+
Driver::MySQL => Builder::<Mysql>::build(db_path),
5268
}?;
5369

5470
database.create_database_tables().expect("Could not create database tables.");

src/core/databases/error.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,52 @@ use std::sync::Arc;
66

77
use r2d2_mysql::mysql::UrlError;
88
use torrust_tracker_located_error::{DynError, Located, LocatedError};
9-
use torrust_tracker_primitives::DatabaseDriver;
9+
10+
use super::driver::Driver;
1011

1112
#[derive(thiserror::Error, Debug, Clone)]
1213
pub enum Error {
1314
/// The query unexpectedly returned nothing.
1415
#[error("The {driver} query unexpectedly returned nothing: {source}")]
1516
QueryReturnedNoRows {
1617
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
17-
driver: DatabaseDriver,
18+
driver: Driver,
1819
},
1920

2021
/// The query was malformed.
2122
#[error("The {driver} query was malformed: {source}")]
2223
InvalidQuery {
2324
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
24-
driver: DatabaseDriver,
25+
driver: Driver,
2526
},
2627

2728
/// Unable to insert a record into the database
2829
#[error("Unable to insert record into {driver} database, {location}")]
2930
InsertFailed {
3031
location: &'static Location<'static>,
31-
driver: DatabaseDriver,
32+
driver: Driver,
3233
},
3334

3435
/// Unable to delete a record into the database
3536
#[error("Failed to remove record from {driver} database, error-code: {error_code}, {location}")]
3637
DeleteFailed {
3738
location: &'static Location<'static>,
3839
error_code: usize,
39-
driver: DatabaseDriver,
40+
driver: Driver,
4041
},
4142

4243
/// Unable to connect to the database
4344
#[error("Failed to connect to {driver} database: {source}")]
4445
ConnectionError {
4546
source: LocatedError<'static, UrlError>,
46-
driver: DatabaseDriver,
47+
driver: Driver,
4748
},
4849

4950
/// Unable to create a connection pool
5051
#[error("Failed to create r2d2 {driver} connection pool: {source}")]
5152
ConnectionPool {
5253
source: LocatedError<'static, r2d2::Error>,
53-
driver: DatabaseDriver,
54+
driver: Driver,
5455
},
5556
}
5657

@@ -60,11 +61,11 @@ impl From<r2d2_sqlite::rusqlite::Error> for Error {
6061
match err {
6162
r2d2_sqlite::rusqlite::Error::QueryReturnedNoRows => Error::QueryReturnedNoRows {
6263
source: (Arc::new(err) as DynError).into(),
63-
driver: DatabaseDriver::Sqlite3,
64+
driver: Driver::Sqlite3,
6465
},
6566
_ => Error::InvalidQuery {
6667
source: (Arc::new(err) as DynError).into(),
67-
driver: DatabaseDriver::Sqlite3,
68+
driver: Driver::Sqlite3,
6869
},
6970
}
7071
}
@@ -76,7 +77,7 @@ impl From<r2d2_mysql::mysql::Error> for Error {
7677
let e: DynError = Arc::new(err);
7778
Error::InvalidQuery {
7879
source: e.into(),
79-
driver: DatabaseDriver::MySQL,
80+
driver: Driver::MySQL,
8081
}
8182
}
8283
}
@@ -86,14 +87,14 @@ impl From<UrlError> for Error {
8687
fn from(err: UrlError) -> Self {
8788
Self::ConnectionError {
8889
source: Located(err).into(),
89-
driver: DatabaseDriver::MySQL,
90+
driver: Driver::MySQL,
9091
}
9192
}
9293
}
9394

94-
impl From<(r2d2::Error, DatabaseDriver)> for Error {
95+
impl From<(r2d2::Error, Driver)> for Error {
9596
#[track_caller]
96-
fn from(e: (r2d2::Error, DatabaseDriver)) -> Self {
97+
fn from(e: (r2d2::Error, Driver)) -> Self {
9798
let (err, driver) = e;
9899
Self::ConnectionPool {
99100
source: Located(err).into(),

src/core/databases/mysql.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ use r2d2_mysql::mysql::prelude::Queryable;
88
use r2d2_mysql::mysql::{params, Opts, OptsBuilder};
99
use r2d2_mysql::MySqlConnectionManager;
1010
use torrust_tracker_primitives::info_hash::InfoHash;
11-
use torrust_tracker_primitives::{DatabaseDriver, PersistentTorrents};
11+
use torrust_tracker_primitives::PersistentTorrents;
1212
use tracing::debug;
1313

14+
use super::driver::Driver;
1415
use super::{Database, Error};
1516
use crate::core::auth::{self, Key};
1617
use crate::shared::bit_torrent::common::AUTH_KEY_LENGTH;
1718

18-
const DRIVER: DatabaseDriver = DatabaseDriver::MySQL;
19+
const DRIVER: Driver = Driver::MySQL;
1920

2021
pub struct Mysql {
2122
pool: Pool<MySqlConnectionManager>,

0 commit comments

Comments
 (0)