refactor: improve database configuration type safety and module organization#240
Merged
josecelano merged 6 commits intomainfrom Dec 16, 2025
Merged
refactor: improve database configuration type safety and module organization#240josecelano merged 6 commits intomainfrom
josecelano merged 6 commits intomainfrom
Conversation
Extracts database configuration types into standalone structs to enable
code reuse across the application.
Changes:
- Create SqliteConfig struct with database_name field
- Create MysqlConfig struct with host, port, database_name, username, password fields
- Update DatabaseConfig enum to use tuple variants with extracted structs
- Use #[serde(tag = "driver", content = "config")] for proper JSON serialization
- Update all usages across domain, application, and infrastructure layers
- Export new types through domain::tracker and domain::environment modules
- Update all tests to match new JSON structure
JSON format changed from flat to nested structure:
- Old: {"driver": "sqlite3", "database_name": "tracker.db"}
- New: {"driver": "sqlite3", "config": {"database_name": "tracker.db"}}
… modules Restructured src/domain/tracker/database.rs into a modular folder structure: - Created database/ directory with mod.rs, sqlite.rs, and mysql.rs - Extracted SqliteConfig into dedicated sqlite.rs module - Extracted MysqlConfig into dedicated mysql.rs module - Maintained DatabaseConfig enum in mod.rs with proper re-exports - Added module documentation explaining structure and extensibility This modular structure makes it easier to add new database drivers in the future by simply adding new modules (e.g., postgres.rs, mongodb.rs). Changed: - src/domain/tracker/database.rs → src/domain/tracker/database/mod.rs - New: src/domain/tracker/database/sqlite.rs (SqliteConfig + tests) - New: src/domain/tracker/database/mysql.rs (MysqlConfig + tests) - Tests: Preserved all existing tests distributed across modules
…ntext Renamed infrastructure::docker_compose::MysqlConfig to MysqlSetupConfig to clearly distinguish its purpose from the domain MysqlConfig type. Key distinction: - Domain MysqlConfig: Used for connecting to existing MySQL database * Has user credentials (username, password, host) * No root password needed - Infrastructure MysqlSetupConfig: Used for Docker Compose MySQL initialization * Has root password for database creation * Creates database, user, and sets permissions * No host field (always service name in Docker Compose) Changes: - Renamed MysqlConfig → MysqlSetupConfig - Removed unused from_domain() method and DomainMysqlConfig import - Updated documentation to explain initialization vs connection distinction - Removed test for unused from_domain() method - Updated all references in DatabaseConfig and builder This makes the code more self-documenting and prevents confusion between connecting to a database vs initializing a new one.
Renamed the private constructor method for better symmetry and clarity: - Old: DockerComposeContextBuilder::new(ports) - New: DockerComposeContextBuilder::with_sqlite(ports) This provides better symmetry with the with_mysql() method and makes it more explicit that the builder starts with SQLite configuration by default. The builder() public method now calls with_sqlite() instead of new().
…lder 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
Member
Author
|
ACK b2bf805 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
SqliteConfigandMysqlConfigfrom theDatabaseConfigenumdatabase.rstodatabase/folder module with separate driver modules2. Infrastructure Layer Improvements
MysqlConfig→MysqlSetupConfigin docker_compose context (clearer naming for initialization)docker_compose/context.rsto directory module with submodules:builder.rs- Builder pattern implementationdatabase.rs- Database configuration and constantsports.rs- Port configurationDockerComposeContextBuilder::with_sqlite()→::new()(SQLite is the default)3. Builder API Enhancements
with_mysql()to acceptMysqlSetupConfigstruct instead of 5 individual parametersDRIVER_SQLITE = "sqlite3",DRIVER_MYSQL = "mysql"4. Domain Layer Consistency
domain/tracker/database/mod.rsdriver_name()method to use constantsTesting
All pre-commit checks passed:
Benefits
Migration
No breaking changes for end users. Internal refactoring only.