Skip to content

refactor(tracker-core): migrate consumers to narrow persistence traits (#1715)#1716

Merged
josecelano merged 9 commits intotorrust:developfrom
josecelano:1525-04b-migrate-consumers-to-narrow-traits
Apr 29, 2026
Merged

refactor(tracker-core): migrate consumers to narrow persistence traits (#1715)#1716
josecelano merged 9 commits intotorrust:developfrom
josecelano:1525-04b-migrate-consumers-to-narrow-traits

Conversation

@josecelano
Copy link
Copy Markdown
Member

Summary

Closes #1715 (subissue of #1525 — Overhaul Persistence).

Replaces all Arc<Box<dyn Database>> consumer wiring with the four narrow
traits introduced in #1714: AuthKeyStore, TorrentMetricsStore,
WhitelistStore, and SchemaMigrator.

Changes

New

  • DatabaseStores bundle in databases::setupinitialize_database now
    returns this struct instead of Arc<Box<dyn Database>>

Updated consumers

Consumer Narrow trait
DatabaseKeyRepository Arc<dyn AuthKeyStore>
DatabaseDownloadsMetricRepository Arc<dyn TorrentMetricsStore>
WhitelistRepository / whitelist::setup Arc<dyn WhitelistStore>
TrackerCoreContainer DatabaseStores fields
Benchmark binary operations narrow traits per operation
authentication/handler.rs tests MockAuthKeyStore directly
REST API server force_database_error tests narrow trait mocks

Cleanup

  • Database removed from databases::mod public re-export (now private to
    the driver layer)
  • All callers in http-tracker-core, udp-tracker-server, and
    axum-http-tracker-server updated
  • Spec file renamed to 1715-1525-04b-migrate-consumers-to-narrow-traits.md
    with cross-references updated in ADR and EPIC plan

Testing

  • cargo test --workspace --all-features — all tests pass
  • linter all — exit code 0

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
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 42.23602% with 372 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.39%. Comparing base (ed0d168) to head (356699b).
⚠️ Report is 10 commits behind head on develop.

Files with missing lines Patch % Lines
...ges/tracker-core/src/databases/driver/mysql/mod.rs 7.31% 76 Missing ⚠️
...-core/src/databases/driver/mysql/auth_key_store.rs 0.00% 55 Missing ⚠️
...rc/databases/driver/mysql/torrent_metrics_store.rs 0.00% 51 Missing ⚠️
...core/src/databases/driver/mysql/schema_migrator.rs 0.00% 46 Missing ⚠️
...core/src/databases/driver/mysql/whitelist_store.rs 0.00% 39 Missing ⚠️
...core/src/databases/driver/sqlite/auth_key_store.rs 65.27% 13 Missing and 12 partials ⚠️
...ore/src/databases/driver/sqlite/whitelist_store.rs 54.90% 13 Missing and 10 partials ⚠️
...c/databases/driver/sqlite/torrent_metrics_store.rs 70.31% 6 Missing and 13 partials ⚠️
...es/tracker-core/src/databases/driver/sqlite/mod.rs 75.00% 4 Missing and 7 partials ⚠️
...ore/src/databases/driver/sqlite/schema_migrator.rs 86.27% 0 Missing and 7 partials ⚠️
... and 7 more
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1716      +/-   ##
===========================================
+ Coverage    81.27%   81.39%   +0.12%     
===========================================
  Files          340      348       +8     
  Lines        25034    25017      -17     
  Branches     25034    25017      -17     
===========================================
+ Hits         20346    20363      +17     
+ Misses        4428     4395      -33     
+ Partials       260      259       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano josecelano marked this pull request as ready for review April 29, 2026 12:04
@josecelano josecelano requested a review from a team as a code owner April 29, 2026 12:04
@josecelano josecelano requested a review from Copilot April 29, 2026 12:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DatabaseStores in tracker-core and updates initialize_database to return it (with one Arc<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 of Database.
  • 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.

Comment thread packages/tracker-core/src/databases/driver/mysql/torrent_metrics_store.rs Outdated
Comment thread packages/tracker-core/src/databases/driver/sqlite/auth_key_store.rs Outdated
Comment thread packages/tracker-core/src/databases/driver/mysql/auth_key_store.rs Outdated
Comment thread packages/tracker-core/src/databases/setup.rs
Comment thread packages/tracker-core/src/whitelist/repository/persisted.rs Outdated
Comment thread packages/tracker-core/src/databases/driver/sqlite/whitelist_store.rs Outdated
Comment thread packages/tracker-core/src/databases/driver/mysql/whitelist_store.rs Outdated
Comment thread docs/packages.md Outdated
Comment thread packages/tracker-core/src/databases/driver/mysql/schema_migrator.rs Outdated
Comment thread packages/tracker-core/src/databases/driver/sqlite/torrent_metrics_store.rs Outdated
…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
@josecelano
Copy link
Copy Markdown
Member Author

ACK 356699b

@josecelano josecelano merged commit 7cff141 into torrust:develop Apr 29, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1525-04b: Migrate consumers to narrow persistence traits

2 participants