Skip to content

Commit 803f078

Browse files
committed
Merge #240: refactor: improve database configuration type safety and module organization
b2bf805 refactor: extract driver name constants in domain database module (Jose Celano) 21d9254 refactor: convert docker_compose context to directory module with builder improvements (Jose Celano) bc39c9c refactor: rename DockerComposeContextBuilder::new to with_sqlite (Jose Celano) b97d446 refactor: rename MysqlConfig to MysqlSetupConfig in docker_compose context (Jose Celano) 634a1e7 refactor: convert database.rs into folder module with separate driver modules (Jose Celano) 0057ea3 refactor: extract SqliteConfig and MysqlConfig from DatabaseConfig enum (Jose Celano) Pull request description: ## Summary This PR refactors the database configuration structure across the application to improve type safety, eliminate magic strings, and enhance module organization. ## Changes ### 1. Database Configuration Type Extraction - Extracted `SqliteConfig` and `MysqlConfig` from the `DatabaseConfig` enum - Converted `database.rs` to `database/` folder module with separate driver modules - Improved type safety and modularity ### 2. Infrastructure Layer Improvements - Renamed `MysqlConfig` → `MysqlSetupConfig` in docker_compose context (clearer naming for initialization) - Converted `docker_compose/context.rs` to directory module with submodules: - `builder.rs` - Builder pattern implementation - `database.rs` - Database configuration and constants - `ports.rs` - Port configuration - Renamed `DockerComposeContextBuilder::with_sqlite()` → `::new()` (SQLite is the default) ### 3. Builder API Enhancements - Simplified `with_mysql()` to accept `MysqlSetupConfig` struct instead of 5 individual parameters - Extracted driver name constants: `DRIVER_SQLITE = "sqlite3"`, `DRIVER_MYSQL = "mysql"` - Eliminated magic strings throughout the codebase ### 4. Domain Layer Consistency - Added driver name constants to `domain/tracker/database/mod.rs` - Updated `driver_name()` method to use constants - Ensures consistency between domain and infrastructure layers ## Testing All pre-commit checks passed: - ✅ 1,515 unit tests - ✅ 8 E2E integration tests - ✅ 373 doctests - ✅ Linting (clippy, rustfmt, shellcheck, markdown, yaml, toml, cspell) - ✅ E2E infrastructure lifecycle tests - ✅ E2E deployment workflow tests ## Benefits - **Type Safety**: Stronger typing with dedicated config structs - **Maintainability**: Eliminated magic strings with constants - **Clarity**: Better module organization and clearer naming - **Consistency**: Unified approach across domain and infrastructure layers - **Usability**: Simpler builder API with struct parameters ## Migration No breaking changes for end users. Internal refactoring only. ACKs for top commit: josecelano: ACK b2bf805 Tree-SHA512: 6fe279891b0a28b212938a33e91fe38f0f5b4dcb1c6bc6b92e22069bcbbb6032e1416aa989436853f0c317e8eee837025d412e3d5bccb32d212e8803b52cd8db
2 parents 47d708a + b2bf805 commit 803f078

File tree

19 files changed

+472
-318
lines changed

19 files changed

+472
-318
lines changed

src/application/command_handlers/create/config/tracker/tracker_core_section.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use schemars::JsonSchema;
88
use serde::{Deserialize, Serialize};
99

1010
use crate::application::command_handlers::create::config::errors::CreateConfigError;
11-
use crate::domain::tracker::{DatabaseConfig, TrackerCoreConfig};
11+
use crate::domain::tracker::{DatabaseConfig, MysqlConfig, SqliteConfig, TrackerCoreConfig};
1212

1313
/// Database configuration section (application DTO)
1414
///
@@ -69,22 +69,22 @@ impl DatabaseSection {
6969
/// future validation.
7070
pub fn to_database_config(&self) -> Result<DatabaseConfig, CreateConfigError> {
7171
match self {
72-
Self::Sqlite { database_name } => Ok(DatabaseConfig::Sqlite {
72+
Self::Sqlite { database_name } => Ok(DatabaseConfig::Sqlite(SqliteConfig {
7373
database_name: database_name.clone(),
74-
}),
74+
})),
7575
Self::Mysql {
7676
host,
7777
port,
7878
database_name,
7979
username,
8080
password,
81-
} => Ok(DatabaseConfig::Mysql {
81+
} => Ok(DatabaseConfig::Mysql(MysqlConfig {
8282
host: host.clone(),
8383
port: *port,
8484
database_name: database_name.clone(),
8585
username: username.clone(),
8686
password: password.clone(),
87-
}),
87+
})),
8888
}
8989
}
9090
}
@@ -143,9 +143,9 @@ mod tests {
143143

144144
assert_eq!(
145145
config.database,
146-
DatabaseConfig::Sqlite {
146+
DatabaseConfig::Sqlite(SqliteConfig {
147147
database_name: "tracker.db".to_string()
148-
}
148+
})
149149
);
150150
assert!(!config.private);
151151
}
@@ -217,13 +217,13 @@ mod tests {
217217

218218
assert_eq!(
219219
config.database,
220-
DatabaseConfig::Mysql {
220+
DatabaseConfig::Mysql(MysqlConfig {
221221
host: "localhost".to_string(),
222222
port: 3306,
223223
database_name: "tracker".to_string(),
224224
username: "tracker_user".to_string(),
225225
password: "secure_password".to_string(),
226-
}
226+
})
227227
);
228228
assert!(!config.private);
229229
}

src/application/command_handlers/create/config/tracker/tracker_section.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ mod tests {
123123

124124
use super::*;
125125
use crate::application::command_handlers::create::config::tracker::tracker_core_section::DatabaseSection;
126-
use crate::domain::tracker::DatabaseConfig;
126+
use crate::domain::tracker::{DatabaseConfig, SqliteConfig};
127127

128128
#[test]
129129
fn it_should_convert_to_domain_config_when_transforming_tracker_section() {
@@ -150,9 +150,9 @@ mod tests {
150150

151151
assert_eq!(
152152
config.core.database,
153-
DatabaseConfig::Sqlite {
153+
DatabaseConfig::Sqlite(SqliteConfig {
154154
database_name: "tracker.db".to_string()
155-
}
155+
})
156156
);
157157
assert!(!config.core.private);
158158
assert_eq!(config.udp_trackers.len(), 1);

src/application/steps/rendering/docker_compose_templates.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::domain::environment::Environment;
3333
use crate::domain::template::TemplateManager;
3434
use crate::domain::tracker::{DatabaseConfig, TrackerConfig};
3535
use crate::infrastructure::templating::docker_compose::template::wrappers::docker_compose::{
36-
DockerComposeContext, DockerComposeContextBuilder, TrackerPorts,
36+
DockerComposeContext, DockerComposeContextBuilder, MysqlSetupConfig, TrackerPorts,
3737
};
3838
use crate::infrastructure::templating::docker_compose::template::wrappers::env::EnvContext;
3939
use crate::infrastructure::templating::docker_compose::{
@@ -111,20 +111,14 @@ impl<S> RenderDockerComposeTemplatesStep<S> {
111111
// Create contexts based on database configuration
112112
let database_config = self.environment.database_config();
113113
let (env_context, builder) = match database_config {
114-
DatabaseConfig::Sqlite { .. } => Self::create_sqlite_contexts(admin_token, ports),
115-
DatabaseConfig::Mysql {
116-
port,
117-
database_name,
118-
username,
119-
password,
120-
..
121-
} => Self::create_mysql_contexts(
114+
DatabaseConfig::Sqlite(..) => Self::create_sqlite_contexts(admin_token, ports),
115+
DatabaseConfig::Mysql(mysql_config) => Self::create_mysql_contexts(
122116
admin_token,
123117
ports,
124-
*port,
125-
database_name.clone(),
126-
username.clone(),
127-
password.clone(),
118+
mysql_config.port,
119+
mysql_config.database_name.clone(),
120+
mysql_config.username.clone(),
121+
mysql_config.password.clone(),
128122
),
129123
};
130124

@@ -191,13 +185,15 @@ impl<S> RenderDockerComposeTemplatesStep<S> {
191185
password.clone(),
192186
);
193187

194-
let builder = DockerComposeContext::builder(ports).with_mysql(
188+
let mysql_config = MysqlSetupConfig {
195189
root_password,
196-
database_name,
197-
username,
190+
database: database_name,
191+
user: username,
198192
password,
199193
port,
200-
);
194+
};
195+
196+
let builder = DockerComposeContext::builder(ports).with_mysql(mysql_config);
201197

202198
(env_context, builder)
203199
}

src/domain/environment/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ pub use user_inputs::UserInputs;
127127

128128
// Re-export tracker types for convenience
129129
pub use crate::domain::tracker::{
130-
DatabaseConfig, HttpApiConfig, HttpTrackerConfig, TrackerConfig, TrackerCoreConfig,
131-
UdpTrackerConfig,
130+
DatabaseConfig, HttpApiConfig, HttpTrackerConfig, MysqlConfig, SqliteConfig, TrackerConfig,
131+
TrackerCoreConfig, UdpTrackerConfig,
132132
};
133133

134134
// Re-export Prometheus types for convenience

src/domain/tracker/config.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::net::SocketAddr;
77

88
use serde::{Deserialize, Serialize};
99

10-
use super::DatabaseConfig;
10+
use super::{DatabaseConfig, SqliteConfig};
1111

1212
/// Tracker deployment configuration
1313
///
@@ -18,15 +18,15 @@ use super::DatabaseConfig;
1818
///
1919
/// ```rust
2020
/// use torrust_tracker_deployer_lib::domain::tracker::{
21-
/// TrackerConfig, TrackerCoreConfig, DatabaseConfig,
21+
/// TrackerConfig, TrackerCoreConfig, DatabaseConfig, SqliteConfig,
2222
/// UdpTrackerConfig, HttpTrackerConfig, HttpApiConfig
2323
/// };
2424
///
2525
/// let tracker_config = TrackerConfig {
2626
/// core: TrackerCoreConfig {
27-
/// database: DatabaseConfig::Sqlite {
27+
/// database: DatabaseConfig::Sqlite(SqliteConfig {
2828
/// database_name: "tracker.db".to_string(),
29-
/// },
29+
/// }),
3030
/// private: false,
3131
/// },
3232
/// udp_trackers: vec![
@@ -116,9 +116,9 @@ impl Default for TrackerConfig {
116116
fn default() -> Self {
117117
Self {
118118
core: TrackerCoreConfig {
119-
database: DatabaseConfig::Sqlite {
119+
database: DatabaseConfig::Sqlite(SqliteConfig {
120120
database_name: "tracker.db".to_string(),
121-
},
121+
}),
122122
private: false,
123123
},
124124
udp_trackers: vec![UdpTrackerConfig {
@@ -158,9 +158,9 @@ mod tests {
158158
fn it_should_create_tracker_config() {
159159
let config = TrackerConfig {
160160
core: TrackerCoreConfig {
161-
database: DatabaseConfig::Sqlite {
161+
database: DatabaseConfig::Sqlite(SqliteConfig {
162162
database_name: "tracker.db".to_string(),
163-
},
163+
}),
164164
private: true,
165165
},
166166
udp_trackers: vec![UdpTrackerConfig {
@@ -185,9 +185,9 @@ mod tests {
185185
fn it_should_serialize_tracker_config() {
186186
let config = TrackerConfig {
187187
core: TrackerCoreConfig {
188-
database: DatabaseConfig::Sqlite {
188+
database: DatabaseConfig::Sqlite(SqliteConfig {
189189
database_name: "test.db".to_string(),
190-
},
190+
}),
191191
private: false,
192192
},
193193
udp_trackers: vec![],

0 commit comments

Comments
 (0)