refactor(tracker-core): migrate consumers to narrow persistence traits (#1715)#1716
Merged
josecelano merged 9 commits intotorrust:developfrom Apr 29, 2026
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
Member
Author
|
ACK 356699b |
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
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