Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

test(storage): drop env mutation from the storage-mode tests

FABIOTESS6611562ad114
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e3f15645..c0b780a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,24 @@ All notable changes to Open Ontologies are documented here.
## [Unreleased]
+### Added
+- **Opt-in persistent triple store.** A new `[storage]` section selects the
+ backend for the main graph: `mode = "memory"` (default, unchanged behaviour)
+ or `mode = "persistent"`, which opens a RocksDB-backed Oxigraph store at
+ `<data_dir>/triplestore` so triples survive a restart. Override with
+ `OPEN_ONTOLOGIES_STORAGE_MODE` or `--storage-mode` on `serve` / `serve-http`;
+ precedence is CLI, then env, then config, then the default. Unknown values
+ warn and fall back to `memory` rather than failing.
+
+ The one-shot CLI subcommands read the same setting, so `open-ontologies load
+ foo.ttl` followed by `open-ontologies query ...` shares state when
+ persistence is on. Sandbox stores elsewhere in the codebase stay in-memory;
+ only the main graph is ever persistent.
+
+ Note that Oxigraph permits a single read-write handle per directory, so two
+ server processes pointed at the same `data_dir` will fail to open the second
+ store. Contributed by Ladislav Gazo (@lgazo).
+
### Fixed
- **SQL floating-point columns were mapped to `xsd:decimal`.** `real`,
`float4`, `float`, `float8`, `double` and `double precision` all resolved to
diff --git a/src/config.rs b/src/config.rs
index 06200284..b9e08f97 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -327,8 +327,29 @@ pub enum StorageMode {
/// (`memory`). Unknown values fall back to `memory` with a warning printed to
/// stderr — preserves the historical in-memory behaviour for typos.
pub fn resolve_storage_mode(cfg: &StorageConfig) -> StorageMode {
- let raw = std::env::var("OPEN_ONTOLOGIES_STORAGE_MODE")
- .ok()
+ let from_env = std::env::var("OPEN_ONTOLOGIES_STORAGE_MODE").ok();
+ resolve_storage_mode_from(from_env.as_deref(), cfg)
+}
+
+/// Resolve the storage mode from an explicitly supplied override.
+///
+/// This is the pure half of [`resolve_storage_mode`]: it takes the override
+/// string rather than reading the environment, so every branch can be
+/// exercised without mutating process-global state.
+///
+/// That matters more than it looks. `std::env::set_var` and `remove_var` are
+/// `unsafe` in edition 2024 because the environment is shared mutable state
+/// with no synchronisation, and cargo runs the test functions within a test
+/// binary on parallel threads. A test that mutates the variable while another
+/// thread is inside `std::env::var` is a data race, whether or not it happens
+/// to pass. Testing against this function instead of the environment removes
+/// the hazard rather than narrowing the window.
+pub fn resolve_storage_mode_from(
+ raw_override: Option<&str>,
+ cfg: &StorageConfig,
+) -> StorageMode {
+ let raw = raw_override
+ .map(str::to_owned)
.filter(|v| !v.trim().is_empty())
.unwrap_or_else(|| cfg.mode.clone());
match raw.trim().to_lowercase().as_str() {
diff --git a/tests/persistent_store_test.rs b/tests/persistent_store_test.rs
index 382303c4..15230e68 100644
--- a/tests/persistent_store_test.rs
+++ b/tests/persistent_store_test.rs
@@ -1,4 +1,4 @@
-use open_ontologies::config::{resolve_storage_mode, StorageConfig, StorageMode};
+use open_ontologies::config::{resolve_storage_mode_from, StorageConfig, StorageMode};
use open_ontologies::graph::GraphStore;
use tempfile::TempDir;
@@ -31,16 +31,20 @@ fn persistent_store_survives_drop_and_reopen() {
}
}
+// These exercise `resolve_storage_mode_from` rather than `resolve_storage_mode`
+// so that no test has to touch the process environment. `remove_var` is unsafe
+// under edition 2024 for a real reason: cargo runs these test functions on
+// parallel threads, so mutating the environment while another thread reads it
+// is a data race regardless of whether the assertion happens to hold.
+// Passing the override in explicitly tests the same logic with the hazard gone.
+
#[test]
fn storage_mode_resolves_default_to_memory() {
let cfg = StorageConfig::default();
- // Guard against a stray env var in the test runner shell.
- // SAFETY: tests run single-threaded for env mutation by default in cargo;
- // this is only touched here, so no cross-test interference.
- unsafe {
- std::env::remove_var("OPEN_ONTOLOGIES_STORAGE_MODE");
- }
- assert_eq!(resolve_storage_mode(&cfg), StorageMode::Memory);
+ assert_eq!(
+ resolve_storage_mode_from(None, &cfg),
+ StorageMode::Memory
+ );
}
#[test]
@@ -48,8 +52,62 @@ fn storage_mode_parses_persistent() {
let cfg = StorageConfig {
mode: "persistent".to_string(),
};
- unsafe {
- std::env::remove_var("OPEN_ONTOLOGIES_STORAGE_MODE");
+ assert_eq!(
+ resolve_storage_mode_from(None, &cfg),
+ StorageMode::Persistent
+ );
+}
+
+#[test]
+fn storage_mode_override_beats_config() {
+ let cfg = StorageConfig {
+ mode: "persistent".to_string(),
+ };
+ assert_eq!(
+ resolve_storage_mode_from(Some("memory"), &cfg),
+ StorageMode::Memory
+ );
+}
+
+#[test]
+fn storage_mode_blank_override_falls_through_to_config() {
+ let cfg = StorageConfig {
+ mode: "persistent".to_string(),
+ };
+ assert_eq!(
+ resolve_storage_mode_from(Some(" "), &cfg),
+ StorageMode::Persistent
+ );
+}
+
+#[test]
+fn storage_mode_accepts_aliases_case_insensitively() {
+ let cfg = StorageConfig::default();
+ for alias in ["persistent", "disk", "rocksdb", "RocksDB", " DISK "] {
+ assert_eq!(
+ resolve_storage_mode_from(Some(alias), &cfg),
+ StorageMode::Persistent,
+ "alias {alias:?} should select the persistent backend"
+ );
+ }
+ for alias in ["memory", "mem", "in-memory", "IN-MEMORY"] {
+ assert_eq!(
+ resolve_storage_mode_from(Some(alias), &cfg),
+ StorageMode::Memory,
+ "alias {alias:?} should select the in-memory backend"
+ );
}
- assert_eq!(resolve_storage_mode(&cfg), StorageMode::Persistent);
+}
+
+#[test]
+fn storage_mode_unknown_value_falls_back_to_memory() {
+ let cfg = StorageConfig {
+ mode: "persistent".to_string(),
+ };
+ // A typo must not silently promote an in-memory deployment to a
+ // persistent one, nor hard-fail: it warns and degrades to memory.
+ assert_eq!(
+ resolve_storage_mode_from(Some("persistant"), &cfg),
+ StorageMode::Memory
+ );
}