Skip to content

Commit 035d630

Browse files
committed
Merge #953: Config overhaul: lowercase for DatabaseDriver
ca348a8 chore: remove unused dependency (Jose Celano) 9d72f51 feat: [#950] use lowercase for database driver values in configuration (Jose Celano) d970bb8 refactor: [#950] rename DatabaseDriver to Driver (Jose Celano) 954295a refactor: [#950] move DatabaseDriver to databases mod (Jose Celano) 9be9638 refactor: [#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

17 files changed

Lines changed: 85 additions & 72 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Containerfile

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 19 additions & 12 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 19 deletions
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

Lines changed: 3 additions & 3 deletions
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

0 commit comments

Comments
 (0)