Repository navigation
refactor(tracker-core): migrate consumers to narrow persistence traits (#1715) - #1716
Conversation
Replace Arc<Box<dyn Database>> with the four narrow traits introduced in torrust#1714: AuthKeyStore, TorrentMetricsStore, WhitelistStore, SchemaMigrator. - Introduce DatabaseStores bundle in databases::setup; initialize_database now returns DatabaseStores instead of Arc<Box<dyn Database>> - TrackerCoreContainer wired through DatabaseStores fields - DatabaseKeyRepository accepts Arc<dyn AuthKeyStore> - DatabaseDownloadsMetricRepository accepts Arc<dyn TorrentMetricsStore> - WhitelistRepository / setup accept Arc<dyn WhitelistStore> - authentication::handler tests use MockAuthKeyStore directly - REST API server force_database_error uses narrow trait mocks - Benchmark binary operations use narrow traits - Database re-export removed from databases::mod (now private to driver) - Fix consumers in http-tracker-core, udp-tracker-server, axum-http-tracker-server that were missed by the Implementer agent Closes torrust#1715
There was a problem hiding this comment.
Pull request overview
Migrates consumer wiring across the workspace from the aggregate Database trait (Arc<Box<dyn Database>>) to the four narrow persistence traits (AuthKeyStore, TorrentMetricsStore, WhitelistStore, SchemaMigrator) by introducing a DatabaseStores bundle returned from initialize_database.
Changes:
- Introduces
DatabaseStoresintracker-coreand updatesinitialize_databaseto return it (with oneArc<dyn ...>per persistence context). - Refactors repositories/services/tests/benchmarks across
tracker-core, HTTP/UDP servers, and axum REST API tests to depend on narrow traits instead ofDatabase. - Splits SQLite/MySQL driver implementations into per-trait modules and updates docs/spec references + ADR discoverability links.
Reviewed changes
Copilot reviewed 51 out of 51 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/udp-tracker-server/src/handlers/mod.rs | Updates tests to pass torrent_metrics_store instead of full Database. |
| packages/udp-tracker-server/src/handlers/announce.rs | Updates tests to pass torrent_metrics_store instead of full Database. |
| packages/tracker-core/src/whitelist/test_helpers.rs | Wires whitelist helpers via DatabaseStores.whitelist_store. |
| packages/tracker-core/src/whitelist/setup.rs | Narrows dependency from Database to WhitelistStore. |
| packages/tracker-core/src/whitelist/repository/persisted.rs | Updates persisted whitelist repository to store Arc<dyn WhitelistStore>. |
| packages/tracker-core/src/whitelist/manager.rs | Updates whitelist manager tests to use DatabaseStores.whitelist_store. |
| packages/tracker-core/src/torrent/manager.rs | Updates torrent manager tests to use DatabaseStores.torrent_metrics_store. |
| packages/tracker-core/src/test_helpers.rs | Updates handler test wiring to use DatabaseStores.torrent_metrics_store. |
| packages/tracker-core/src/statistics/persisted/downloads.rs | Narrows downloads metrics repository to TorrentMetricsStore. |
| packages/tracker-core/src/databases/traits/torrent_metrics.rs | Adds ADR-linked rationale note for TorrentMetricsStore scope. |
| packages/tracker-core/src/databases/traits/mod.rs | Adds ADR link in module-level docs. |
| packages/tracker-core/src/databases/setup.rs | Introduces DatabaseStores + returns it from initialize_database. |
| packages/tracker-core/src/databases/mod.rs | Stops publicly re-exporting the aggregate Database trait. |
| packages/tracker-core/src/databases/driver/sqlite/whitelist_store.rs | New SQLite WhitelistStore impl module. |
| packages/tracker-core/src/databases/driver/sqlite/torrent_metrics_store.rs | New SQLite TorrentMetricsStore impl module. |
| packages/tracker-core/src/databases/driver/sqlite/schema_migrator.rs | New SQLite SchemaMigrator impl module. |
| packages/tracker-core/src/databases/driver/sqlite/mod.rs | New SQLite driver module root after split. |
| packages/tracker-core/src/databases/driver/sqlite/auth_key_store.rs | New SQLite AuthKeyStore impl module. |
| packages/tracker-core/src/databases/driver/sqlite.rs | Removes old monolithic SQLite driver file in favor of split modules. |
| packages/tracker-core/src/databases/driver/mysql/whitelist_store.rs | New MySQL WhitelistStore impl module. |
| packages/tracker-core/src/databases/driver/mysql/torrent_metrics_store.rs | New MySQL TorrentMetricsStore impl module. |
| packages/tracker-core/src/databases/driver/mysql/schema_migrator.rs | New MySQL SchemaMigrator impl module. |
| packages/tracker-core/src/databases/driver/mysql/mod.rs | New MySQL driver module root after split. |
| packages/tracker-core/src/databases/driver/mysql/auth_key_store.rs | New MySQL AuthKeyStore impl module. |
| packages/tracker-core/src/databases/driver/mysql.rs | Removes old monolithic MySQL driver file in favor of split modules. |
| packages/tracker-core/src/databases/driver/mod.rs | Removes driver factory build and narrows visibility of metric constant. |
| packages/tracker-core/src/container.rs | Refactors TrackerCoreContainer to hold DatabaseStores and wire narrow traits. |
| packages/tracker-core/src/bin/persistence_benchmark/driver_bench/operations/whitelist.rs | Narrows benchmark operation to WhitelistStore. |
| packages/tracker-core/src/bin/persistence_benchmark/driver_bench/operations/torrent.rs | Narrows benchmark operation to TorrentMetricsStore. |
| packages/tracker-core/src/bin/persistence_benchmark/driver_bench/operations/mod.rs | Narrows benchmark operation signatures to the relevant traits. |
| packages/tracker-core/src/bin/persistence_benchmark/driver_bench/operations/keys.rs | Narrows benchmark operation to AuthKeyStore. |
| packages/tracker-core/src/bin/persistence_benchmark/driver_bench/mod.rs | Updates benchmark wiring to pass the correct store fields. |
| packages/tracker-core/src/bin/persistence_benchmark/driver_bench/database/mod.rs | Stores DatabaseStores and uses SchemaMigrator for reset logic. |
| packages/tracker-core/src/authentication/mod.rs | Updates auth tests to pass auth_key_store. |
| packages/tracker-core/src/authentication/key/repository/persisted.rs | Narrows persisted key repository to AuthKeyStore. |
| packages/tracker-core/src/authentication/handler.rs | Refactors handler tests to use MockAuthKeyStore directly. |
| packages/http-tracker-core/src/services/scrape.rs | Updates tests to use torrent_metrics_store. |
| packages/http-tracker-core/src/services/announce.rs | Updates tests to use torrent_metrics_store. |
| packages/http-tracker-core/benches/helpers/util.rs | Updates benchmark helper wiring to use torrent_metrics_store. |
| packages/axum-rest-tracker-api-server/tests/server/v1/contract/context/whitelist.rs | Updates failure-injection to operate via schema_migrator. |
| packages/axum-rest-tracker-api-server/tests/server/v1/contract/context/auth_key.rs | Updates failure-injection to operate via schema_migrator. |
| packages/axum-rest-tracker-api-server/tests/server/mod.rs | Changes force_database_error to accept Arc<dyn SchemaMigrator>. |
| packages/axum-http-tracker-server/src/v1/handlers/announce.rs | Updates tests to use torrent_metrics_store. |
| docs/packages.md | Adds “Design Decisions” section linking to ADR; reflows tables. |
| docs/issues/1715-1525-04b-migrate-consumers-to-narrow-traits.md | Updates references to include the GitHub issue number. |
| docs/issues/1525-overhaul-persistence.md | Updates spec filename reference for subissue 04b. |
| docs/adrs/20260429000000_keep_database_as_aggregate_supertrait.md | Updates spec links + adds revisit criteria section. |
| .github/skills/dev/planning/create-adr/SKILL.md | Documents ADR/code cross-linking convention. |
| .github/skills/dev/github/link-subissue-to-parent-issue/SKILL.md | Adds a new skill doc for linking sub-issues via GitHub API. |
| .github/agents/implementer.agent.md | Codifies ADR discoverability convention. |
| .github/agents/github-operator.agent.md | Adds a GitHub-operator agent doc for workflow operations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…tion Address Copilot PR review suggestions on PR torrust#1716: - Add MalformedDatabaseRecord error variant for unparseable DB values - Convert InfoHash parse failures (binascii::ConvertError) to Err instead of unwrap/panic in torrent_metrics_store and whitelist_store drivers - Convert Key parse failures (ParseKeyError) to Err instead of unwrap in auth_key_store drivers (mysql and sqlite) - Replace expect() with ? in mysql schema_migrator create/drop methods - Extract build_database_stores() helper in setup.rs to deduplicate the two identical DatabaseStores construction branches - Remove stray ç character from whitelist/repository/persisted.rs doc - Remove duplicate Architectural Philosophy entry in docs/packages.md ToC
|
ACK 356699b |
da2ce7
left a comment
There was a problem hiding this comment.
This is a post-merge review of the change as it stands at a94c3ced8 on the default branch.
What still runs: #1718 replaced the driver bodies and later changes reworked the composition, but the structure this change introduced is intact. DatabaseStores and build_database_stores (packages/tracker-core/src/databases/setup.rs:23-44) still hand one migrated driver to four narrow Arc<dyn …> handles; DatabaseKeyRepository, DatabaseDownloadsMetricRepository, DatabaseWhitelist and initialize_whitelist_manager still take only their own store; PersistenceServices holds the bundle (packages/tracker-core/src/container.rs:58); the per-driver folder modules remain; and Error::MalformedDatabaseRecord (packages/tracker-core/src/databases/error.rs:78-82) is still how a corrupt stored key or info-hash is reported instead of a panic. The description mentions neither the split of the drivers into folder modules nor that new public error variant, and it says the REST API force_database_error tests use narrow trait mocks, whereas packages/axum-rest-api-server/tests/server/mod.rs:17 drops the real tables through SchemaMigrator.
[Minor] The description says Database is "now private to the driver layer", and the ADR 20260429000000_keep_database_as_aggregate_supertrait calls it a private, internal compile-time contract, but it is still public: pub mod database; and pub use database::Database; at packages/tracker-core/src/databases/traits/mod.rs:6 and packages/tracker-core/src/databases/traits/mod.rs:16, under the public databases::traits module (packages/tracker-core/src/databases/mod.rs:63). Only the re-export at databases::Database was removed. A downstream crate can still write use torrust_tracker_core::databases::traits::Database; and take an Arc<dyn Database>, which brings back the wide dependency this change set out to remove and keeps the aggregate trait in the published API. At a94c3ced8 nothing outside the crate uses it except an intra-doc link at packages/persistence-benchmark/src/bin/persistence_benchmark_runner.rs:4, and inside the crate only the driver test suites do. Smallest fix: make both lines pub(crate) and point that doc link at the narrow traits.
[Nit] The DatabaseStores doc at packages/tracker-core/src/databases/setup.rs:17 names initialize_database as the factory, but the composition path is now initialize_database_from_configuration (packages/tracker-core/src/databases/setup.rs:111, called at packages/tracker-core/src/container.rs:84), and initialize_database (packages/tracker-core/src/databases/setup.rs:91) is the convenience wrapper that panics. The initialize_database doc that this change rewrote still says, at packages/tracker-core/src/databases/setup.rs:50-53, that it picks "either Sqlite3 or MySQL" from config.database.driver and config.database.path; at this commit config.database is an optional enum with three drivers and has neither field. Smallest fix: name initialize_database_from_configuration at line 17 and describe the enum variants at lines 50-53.
Checked and found sound: the four stores share one driver and one pool, and dropping the tables through the migrator breaks all four (pinned by the test at packages/tracker-core/src/databases/setup.rs:173); every consumer the description lists takes only its narrow store; the factory returns typed errors instead of panicking, with the panic kept only in the legacy wrapper; corrupt stored keys, info-hashes and negative expiries become MalformedDatabaseRecord instead of panics; and the message for a stored key that does not parse says what is wrong with it, never the key (packages/tracker-core/src/databases/driver/mod.rs:43).
Summary
Closes #1715 (subissue of #1525 — Overhaul Persistence).
Replaces all
Arc<Box<dyn Database>>consumer wiring with the four narrowtraits introduced in #1714:
AuthKeyStore,TorrentMetricsStore,WhitelistStore, andSchemaMigrator.Changes
New
DatabaseStoresbundle indatabases::setup—initialize_databasenowreturns this struct instead of
Arc<Box<dyn Database>>Updated consumers
DatabaseKeyRepositoryArc<dyn AuthKeyStore>DatabaseDownloadsMetricRepositoryArc<dyn TorrentMetricsStore>WhitelistRepository/whitelist::setupArc<dyn WhitelistStore>TrackerCoreContainerDatabaseStoresfieldsauthentication/handler.rstestsMockAuthKeyStoredirectlyforce_database_errortestsCleanup
Databaseremoved fromdatabases::modpublic re-export (now private tothe driver layer)
http-tracker-core,udp-tracker-server, andaxum-http-tracker-serverupdated1715-1525-04b-migrate-consumers-to-narrow-traits.mdwith cross-references updated in ADR and EPIC plan
Testing
cargo test --workspace --all-features— all tests passlinter all— exit code 0