Skip to content

feat(configuration): make v3 persistence optional - #2099

Merged
josecelano merged 8 commits into
torrust:developfrom
josecelano:999-avoid-unneeded-database-initialization-phase-3
Aug 26, 2026
Merged

josecelano merged 8 commits into
torrust:developfrom
josecelano:999-avoid-unneeded-database-initialization-phase-3

Conversation

@josecelano

@josecelano josecelano commented Aug 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Implements Issue #999 Phase 3 by making the v3 database representation optional and preparing application composition for optional persistence.

  • Changes v3_0_0::Core.database to Option<Database>; an omitted [core.database] loads and persists as None.
  • Resolves persistence at the TrackerCoreContainer composition seam, with the supplied database configuration driving driver initialization and migrations.
  • Preserves current runtime behavior through a named v2 compatibility bridge that explicitly supplies Some(Database).
  • Adds the bootstrap-owned PersistenceRequirementError matrix and exact-message unit tests without invoking it from the active bridged runtime.
  • Publishes the optional-persistence composition ADR and updates Issue Database configuration needed even when it's not used #999 evidence.

Deliberately deferred

  • Active persistence-free runtime behavior: actual v3 None is not passed to production composition yet.
  • M1–M6 manual scenarios and no-persistence container startup.
  • REST API disabled-capability responses and metric provenance work under API API V2: new API with the correct response codes #144.

Validation

  • cargo test -p torrust-tracker-core
  • cargo test -p torrust-tracker --lib
  • cargo test -p torrust-tracker-configuration
  • cargo check --workspace --all-targets
  • linter all
  • TORRUST_GIT_HOOKS_LOG_DIR=.tmp ./contrib/dev-tools/git/hooks/pre-commit.sh

Refs #999

Copilot AI lite review requested due to automatic review settings August 26, 2026 09:27
@josecelano josecelano self-assigned this Aug 26, 2026
@josecelano
josecelano requested a review from da2ce7 August 26, 2026 09:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR implements Phase 3 of Issue #999 by making schema v3 persistence representable as optional (Option<Database>), while preserving current v2 runtime behavior via an explicit compatibility bridge and moving persistence resolution to the tracker-core composition seam.

Changes:

  • Make v3 core.database optional and ensure omitted [core.database] loads/persists as None.
  • Resolve persistence at TrackerCoreContainer::initialize_from(...) by accepting Option<&Database> and returning Option<Self>, updating all current call sites to explicitly supply persistence.
  • Add bootstrap-owned persistence requirement validation (error matrix + unit tests) and publish the ADR + update Issue #999 evidence.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/container.rs Introduces a temporary v2 database compatibility bridge and passes explicit persistence into tracker-core composition.
src/bootstrap/persistence.rs Adds bootstrap-owned persistence requirement validation and exact-message unit tests.
src/bootstrap/mod.rs Exposes the new persistence bootstrap module.
packages/udp-server/src/testing/environment.rs Updates UDP server test env composition to pass explicit persistence into tracker-core.
packages/udp-core/src/container.rs Updates UDP core container composition to pass explicit persistence into tracker-core.
packages/tracker-core/tests/common/test_env.rs Updates tracker-core integration test environment to pass explicit persistence into tracker-core.
packages/tracker-core/src/databases/setup.rs Refactors DB initialization to a database-config-driven helper used at the optional persistence boundary.
packages/tracker-core/src/container.rs Makes tracker-core container construction optional based on supplied persistence and adds coverage for None/Some.
packages/rest-api-runtime-adapter/src/v1/container.rs Updates REST runtime adapter composition to pass explicit persistence into tracker-core.
packages/http-core/src/container.rs Updates HTTP core container composition to pass explicit persistence into tracker-core.
packages/configuration/src/v3_0_0/mod.rs Updates v3 loading/persistence logic and tests to treat [core.database] as optional and omit it when absent.
packages/configuration/src/v3_0_0/core.rs Changes v3 Core.database to Option<Database> with updated defaults and documentation.
packages/axum-rest-api-server/src/testing/environment.rs Updates REST API server test env composition to pass explicit persistence into tracker-core.
packages/axum-http-server/src/testing/environment.rs Updates HTTP server test env composition to pass explicit persistence into tracker-core.
packages/axum-http-server/src/server.rs Updates HTTP server tests to pass explicit persistence into tracker-core.
docs/issues/open/999-1978-optional-database-configuration/solution.md Documents the selected Phase 3 composition seam and alternatives.
docs/issues/open/999-1978-optional-database-configuration/ISSUE.md Updates Phase 3 tasks, progress tracking, and acceptance evidence status.
docs/adrs/index.md Registers the new optional-persistence ADR in the ADR index.
docs/adrs/20260825193119_make_persistence_an_optional_application_composition_capability.md Publishes the ADR defining persistence as an optional application-composition capability.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/bootstrap/persistence.rs Resolved
Comment thread src/container.rs Resolved
@codecov

codecov Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.91304% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.87%. Comparing base (eaec5dc) to head (f2dbc26).

Files with missing lines Patch % Lines
packages/tracker-core/src/databases/setup.rs 71.42% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2099      +/-   ##
===========================================
+ Coverage    83.84%   83.87%   +0.03%     
===========================================
  Files          349      349              
  Lines        26498    26635     +137     
  Branches     26498    26635     +137     
===========================================
+ Hits         22217    22340     +123     
- Misses        3948     3961      +13     
- Partials       333      334       +1     

☔ View full report in Codecov by Harness.
📢 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 requested a review from a team as a code owner August 26, 2026 10:10
@josecelano

Copy link
Copy Markdown
Member Author

ACK f2dbc26

@josecelano
josecelano merged commit 02e02eb into torrust:develop Aug 26, 2026
27 checks passed

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a post-merge review of the change as it stands at a94c3ced8 on the default branch.

What still runs: the v3 Core.database is Option<Database>, left None when [core.database] is omitted on load and left out of the file on save. TrackerCoreContainer::initialize_from still takes Option<&Database> and resolves persistence at that seam through initialize_database_from_configuration; since 56ea123 it composes without persistence and returns a typed error instead of Option<Self>. The v2 compatibility bridge in src/container.rs was removed by 547f848, and setup in src/bootstrap/app.rs has called validate_persistence_requirements before composition since d1ebe12.

[Minor] packages/http-core/src/container.rs:45: HttpTrackerCoreContainer::initialize still replaces an absent database with Database::default() (core_config.database.clone().unwrap_or_default()), which is left over from the compatibility bridge. Given a v3 Core that omits [core.database], it opens and migrates ./storage/tracker/lib/database/sqlite3.db relative to the working directory, or panics with "HTTP tracker core initialization requires a configured database or SQLite fallback" when that directory is missing. Given the same Core, the UDP constructor (packages/udp-core/src/container.rs:57) and the REST API constructor (packages/rest-api-runtime-adapter/src/v1/container.rs:68) compose without persistence. The fallback contradicts agreements 1 and 6 of ADR 20260825193119: an absent database means persistence is unavailable, and no database file is created. No in-tree caller passes an absent database, so the application path is not affected, but the constructor is public API of a published crate. Smallest fix: pass core_config.database.as_ref() as the sibling constructors do, and update the # Panics text and the expect message.

[Minor] packages/configuration/src/v3_0_0/database.rs:80: the network database host is an unvalidated String. connection_url(), which initialize_database_from_configuration calls at packages/tracker-core/src/databases/setup.rs:119 and :125, passes it to Url::set_host and calls expect on the result. The url crate follows the WHATWG host-setter rules, so host = "::1" is rejected and startup panics with "database hosts must be representable in a URL", where the composition seam should return its typed Error::Persistence. A value such as host = "fe80::1" or host = "db.internal:3307" is cut at the first colon, so the tracker quietly tries fe80, or db.internal on the port field's value. A host containing a space or a / also panics. The configuration package's rule that domain-constrained fields use typed newtypes applies to this field. Smallest fix: validate the host during deserialization (a domain name or an IP address, with IPv6 bracketed when the URL is built), and make connection_url return a Result instead of calling expect.

[Nit] src/bootstrap/persistence.rs:3: the module comment still says the check "is intentionally not called" while the runtime uses v2, but setup calls it at src/bootstrap/app.rs:62. The # Errors section at lines 36 to 39 says an error is returned only when [core.database] is omitted. Since 6248ff8, however, the check first returns PersistentTorrentCompletedStatRequiresTrackerUsageStatistics whether or not a database is configured, and that variant is not the persistence requirement the enum doc at line 9 describes. Smallest fix: rewrite the module comment and the # Errors section to say what the check does now.

[Nit] packages/udp-core/src/container.rs:38 and packages/rest-api-runtime-adapter/src/v1/container.rs:46: both # Panics sections still speak of a "persistence-required tracker-core container", and the expect messages at packages/udp-core/src/container.rs:60 and packages/rest-api-runtime-adapter/src/v1/container.rs:71 say initialization "requires persistence". Neither constructor requires persistence any more. They panic only when a configured database cannot be initialized or migrated, or when persistent completed statistics are enabled without a database. The same message remains in the test environments at packages/axum-http-server/src/testing/environment.rs:243, packages/udp-server/src/testing/environment.rs:300 and packages/axum-http-server/src/server.rs:451. Smallest fix: name those two causes in the docs and the messages.

[Nit] packages/tracker-core/src/databases/setup.rs:46: the doc of initialize_database still says it selects "either Sqlite3 or MySQL" from config.database.driver and config.database.path, fields the v3 Option<Database> enum does not have. Its # Panics section leaves out the panic at line 95 when core.database is None, even though the test at line 228 checks that panic. Smallest fix: update the description and add the absent-database case to # Panics.

Checked and found sound: loading and saving the optional [core.database] (load leaves it None, saving leaves the table out, the Figment defaults carry no SQLite path that could merge into a network table, and secrets reach the file only through serialize_for_persistence); the shipped v3 configurations, all consistent with the requirement check; setup as the only runtime entry, running the check after semantic validation and before global services and composition; AppContainer::initialize, which passes the configured database through and maps composition failures to a typed error; the seam, which builds no driver, store or file when the database is absent and refuses persistent completed statistics without one; and the REST API, which withholds the key and whitelist routes when persistence is absent. Three defects present at this pull request's head have since been fixed: the composition API returned Option<Self> and every caller panicked on it (replaced by a typed Result in 56ea123); the bridge in src/container.rs (removed in 547f848); and the omitted-database load test, which read process environment overrides (isolated in 8ab58ef).

This branch was previously deployed

1 inactive deployment
coverage — f2dbc260 Deployed Aug 26, 2026 by josecelano via Generate Coverage Report #1823
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.

3 participants