Skip to content

Commit 21d9254

Browse files
committed
refactor: convert docker_compose context to directory module with builder improvements
- Convert context.rs to directory module (context/mod.rs, builder.rs, database.rs, ports.rs) - Extract database driver name constants (DRIVER_SQLITE, DRIVER_MYSQL) - Rename DockerComposeContextBuilder::with_sqlite to ::new for clarity - Simplify with_mysql to accept MysqlSetupConfig directly instead of 5 parameters - Add MysqlSetupConfig to public re-exports - Update all usages across production and test code
1 parent bc39c9c commit 21d9254

File tree

8 files changed

+213
-181
lines changed

8 files changed

+213
-181
lines changed

src/application/steps/rendering/docker_compose_templates.rs

Lines changed: 7 additions & 5 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::{
@@ -185,13 +185,15 @@ impl<S> RenderDockerComposeTemplatesStep<S> {
185185
password.clone(),
186186
);
187187

188-
let builder = DockerComposeContext::builder(ports).with_mysql(
188+
let mysql_config = MysqlSetupConfig {
189189
root_password,
190-
database_name,
191-
username,
190+
database: database_name,
191+
user: username,
192192
password,
193193
port,
194-
);
194+
};
195+
196+
let builder = DockerComposeContext::builder(ports).with_mysql(mysql_config);
195197

196198
(env_context, builder)
197199
}

src/infrastructure/templating/docker_compose/template/renderer/docker_compose.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ mod tests {
189189
use tempfile::TempDir;
190190

191191
use crate::infrastructure::templating::docker_compose::template::wrappers::docker_compose::{
192-
DockerComposeContext, TrackerPorts,
192+
DockerComposeContext, MysqlSetupConfig, TrackerPorts,
193193
};
194194

195195
#[test]
@@ -219,14 +219,15 @@ mod tests {
219219
http_tracker_ports: vec![7070],
220220
http_api_port: 1212,
221221
};
222+
let mysql_config = MysqlSetupConfig {
223+
root_password: "rootpass123".to_string(),
224+
database: "tracker_db".to_string(),
225+
user: "tracker_user".to_string(),
226+
password: "userpass123".to_string(),
227+
port: 3306,
228+
};
222229
let mysql_context = DockerComposeContext::builder(ports)
223-
.with_mysql(
224-
"rootpass123".to_string(),
225-
"tracker_db".to_string(),
226-
"tracker_user".to_string(),
227-
"userpass123".to_string(),
228-
3306,
229-
)
230+
.with_mysql(mysql_config)
230231
.build();
231232

232233
let renderer = DockerComposeRenderer::new(template_manager);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Builder for `DockerComposeContext`
2+
3+
// Internal crate
4+
use crate::domain::prometheus::PrometheusConfig;
5+
6+
use super::database::{DatabaseConfig, MysqlSetupConfig, DRIVER_MYSQL, DRIVER_SQLITE};
7+
use super::{DockerComposeContext, TrackerPorts};
8+
9+
/// Builder for `DockerComposeContext`
10+
///
11+
/// Provides a fluent API for constructing Docker Compose contexts with optional features.
12+
/// Defaults to `SQLite` database configuration.
13+
pub struct DockerComposeContextBuilder {
14+
ports: TrackerPorts,
15+
database: DatabaseConfig,
16+
prometheus_config: Option<PrometheusConfig>,
17+
}
18+
19+
impl DockerComposeContextBuilder {
20+
/// Creates a new builder with default `SQLite` configuration
21+
pub(super) fn new(ports: TrackerPorts) -> Self {
22+
Self {
23+
ports,
24+
database: DatabaseConfig {
25+
driver: DRIVER_SQLITE.to_string(),
26+
mysql: None,
27+
},
28+
prometheus_config: None,
29+
}
30+
}
31+
32+
/// Switches to `MySQL` database configuration
33+
///
34+
/// # Arguments
35+
///
36+
/// * `mysql_config` - `MySQL` setup configuration
37+
#[must_use]
38+
pub fn with_mysql(mut self, mysql_config: MysqlSetupConfig) -> Self {
39+
self.database = DatabaseConfig {
40+
driver: DRIVER_MYSQL.to_string(),
41+
mysql: Some(mysql_config),
42+
};
43+
self
44+
}
45+
46+
/// Adds Prometheus configuration
47+
///
48+
/// # Arguments
49+
///
50+
/// * `prometheus_config` - Prometheus configuration
51+
#[must_use]
52+
pub fn with_prometheus(mut self, prometheus_config: PrometheusConfig) -> Self {
53+
self.prometheus_config = Some(prometheus_config);
54+
self
55+
}
56+
57+
/// Builds the `DockerComposeContext`
58+
#[must_use]
59+
pub fn build(self) -> DockerComposeContext {
60+
DockerComposeContext {
61+
database: self.database,
62+
ports: self.ports,
63+
prometheus_config: self.prometheus_config,
64+
}
65+
}
66+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//! Database configuration for Docker Compose templates
2+
3+
// External crates
4+
use serde::Serialize;
5+
6+
/// `SQLite` driver name
7+
pub const DRIVER_SQLITE: &str = "sqlite3";
8+
9+
/// `MySQL` driver name
10+
pub const DRIVER_MYSQL: &str = "mysql";
11+
12+
/// Database configuration for docker-compose template
13+
#[derive(Serialize, Debug, Clone)]
14+
pub struct DatabaseConfig {
15+
/// Database driver: "sqlite3" or "mysql"
16+
pub driver: String,
17+
/// MySQL-specific configuration (only present when driver == "mysql")
18+
#[serde(skip_serializing_if = "Option::is_none")]
19+
pub mysql: Option<MysqlSetupConfig>,
20+
}
21+
22+
impl DatabaseConfig {
23+
/// Get the database driver name
24+
#[must_use]
25+
pub fn driver(&self) -> &str {
26+
&self.driver
27+
}
28+
29+
/// Get the `MySQL` setup configuration if present
30+
#[must_use]
31+
pub fn mysql(&self) -> Option<&MysqlSetupConfig> {
32+
self.mysql.as_ref()
33+
}
34+
}
35+
36+
/// `MySQL` setup configuration for Docker Compose initialization
37+
///
38+
/// This configuration is used to set up a new `MySQL` database in Docker Compose.
39+
/// It includes the root password needed for database initialization, unlike the
40+
/// domain `MysqlConfig` which is used for connecting to an existing database.
41+
///
42+
/// Key differences from domain `MysqlConfig`:
43+
/// - Includes `root_password` for database initialization
44+
/// - Used for Docker Compose environment variable setup
45+
/// - Does not include `host` (always the service name in Docker Compose)
46+
#[derive(Serialize, Debug, Clone)]
47+
pub struct MysqlSetupConfig {
48+
/// `MySQL` root password for database initialization
49+
pub root_password: String,
50+
/// `MySQL` database name to create
51+
pub database: String,
52+
/// `MySQL` user to create
53+
pub user: String,
54+
/// `MySQL` password for the created user
55+
pub password: String,
56+
/// `MySQL` port
57+
pub port: u16,
58+
}

0 commit comments

Comments
 (0)