Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(storage): introduce record upgrade functions

Steven Enamakel240b5dce4d66
diff --git a/crates/openhuman-core/src/desktop/notifications/storage_schema.rs b/crates/openhuman-core/src/desktop/notifications/storage_schema.rs
index 06797ebb3a..e3008cdf33 100644
--- a/crates/openhuman-core/src/desktop/notifications/storage_schema.rs
+++ b/crates/openhuman-core/src/desktop/notifications/storage_schema.rs
@@ -14,10 +14,13 @@
//! see as field names.
use serde_json::Value;
-use tinystoragedrivers::schema::{Schema, ScopeClass, SourceRecord, Step, TargetRecord};
-use tinystoragedrivers::{CollectionSpec, IndexSpec, Key, Result};
+use anyhow::Result;
+use tinystoragedrivers::{CollectionSpec, IndexSpec, Key};
-use crate::storage::schema_support::{key_of, owner, time_ms, Body};
+use crate::storage::schema_support::{
+ key_of, map_legacy_collection, time_ms, Body, LegacySourceRecord as SourceRecord,
+ LegacyTargetRecord as TargetRecord,
+};
pub(super) const NOTIFICATIONS: &str = "notifications.integration";
pub(super) const DEDUP: &str = "notifications.dedup";
@@ -29,26 +32,14 @@ pub(super) const LEGACY_DEDUP: &str = "notification_dedup";
pub(super) const LEGACY_SETTINGS: &str = "notification_settings";
pub(super) const LEGACY_CORE: &str = "core_notifications";
-const SCHEMA: u32 = 2;
-
-/// What the notification store keeps and the steps from the v1 collections.
-#[must_use]
-pub fn storage_schema() -> Schema {
- let mut schema = Schema::new(owner("notifications"));
- for spec in specs() {
- schema = schema.collection_with(SCHEMA, spec, ScopeClass::Tenant);
- }
- schema
- .step(
- Step::new("integration_v2", NOTIFICATIONS, 1, SCHEMA, integration_v2)
- .source(LEGACY_NOTIFICATIONS),
- )
- .step(Step::new("dedup_v2", DEDUP, 1, SCHEMA, renamed(DEDUP)).source(LEGACY_DEDUP))
- .step(
- Step::new("settings_v2", SETTINGS, 1, SCHEMA, renamed(SETTINGS))
- .source(LEGACY_SETTINGS),
- )
- .step(Step::new("core_v2", CORE, 1, SCHEMA, core_v2).source(LEGACY_CORE))
+/// Converts legacy notification documents into the current owner shapes.
+pub(crate) fn upgrade_records(
+ records: Vec<crate::storage::import::Record>,
+) -> Result<Vec<crate::storage::import::Record>> {
+ let records = map_legacy_collection(records, LEGACY_NOTIFICATIONS, integration_v2)?;
+ let records = map_legacy_collection(records, LEGACY_DEDUP, renamed(DEDUP))?;
+ let records = map_legacy_collection(records, LEGACY_SETTINGS, renamed(SETTINGS))?;
+ map_legacy_collection(records, LEGACY_CORE, core_v2)
}
/// The four v2 collections with their indexes.
diff --git a/crates/openhuman-core/src/integrations/task_sources/storage_schema.rs b/crates/openhuman-core/src/integrations/task_sources/storage_schema.rs
index 33ce30bd51..10225f52f9 100644
--- a/crates/openhuman-core/src/integrations/task_sources/storage_schema.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/storage_schema.rs
@@ -11,26 +11,24 @@
//! must never see as field names.
use serde_json::Value;
-use tinystoragedrivers::schema::{Schema, ScopeClass, SourceRecord, Step, TargetRecord};
-use tinystoragedrivers::{CollectionSpec, IndexSpec, Key, Result};
+use anyhow::Result;
+use tinystoragedrivers::{CollectionSpec, IndexSpec, Key};
-use crate::storage::schema_support::{key_of, owner, time_ms, Body};
+use crate::storage::schema_support::{
+ key_of, map_legacy_collection, time_ms, Body, LegacySourceRecord as SourceRecord,
+ LegacyTargetRecord as TargetRecord,
+};
pub(super) const SOURCES: &str = "task_sources.sources";
pub(super) const INGESTED: &str = "task_sources.ingested";
pub(super) const LEGACY_SOURCES: &str = "task_sources";
pub(super) const LEGACY_INGESTED: &str = "ingested_tasks";
-const SCHEMA: u32 = 2;
-
-/// What the task-source store keeps and the steps from the v1 collections.
-#[must_use]
-pub fn storage_schema() -> Schema {
- let [sources, ingested] = specs();
- Schema::new(owner("task_sources"))
- .collection_with(SCHEMA, sources, ScopeClass::Tenant)
- .collection_with(SCHEMA, ingested, ScopeClass::Tenant)
- .step(Step::new("sources_v2", SOURCES, 1, SCHEMA, sources_v2).source(LEGACY_SOURCES))
- .step(Step::new("ingested_v2", INGESTED, 1, SCHEMA, ingested_v2).source(LEGACY_INGESTED))
+/// Converts legacy task-source documents into the current owner shapes.
+pub(crate) fn upgrade_records(
+ records: Vec<crate::storage::import::Record>,
+) -> Result<Vec<crate::storage::import::Record>> {
+ let records = map_legacy_collection(records, LEGACY_SOURCES, sources_v2)?;
+ map_legacy_collection(records, LEGACY_INGESTED, ingested_v2)
}
/// The two v2 collections with their indexes.
diff --git a/crates/openhuman-core/src/storage/schema_support.rs b/crates/openhuman-core/src/storage/schema_support.rs
index e350688429..e35b7ab8d0 100644
--- a/crates/openhuman-core/src/storage/schema_support.rs
+++ b/crates/openhuman-core/src/storage/schema_support.rs
@@ -50,11 +50,14 @@ impl LegacyTargetRecord {
/// Converts one legacy collection using an owner-specific pure transform.
/// Records from other collections pass through unchanged.
-pub(crate) fn map_legacy_collection(
+pub(crate) fn map_legacy_collection<F>(
records: Vec<Record>,
source_collection: &str,
- convert: fn(Vec<LegacySourceRecord>) -> anyhow::Result<Vec<LegacyTargetRecord>>,
-) -> anyhow::Result<Vec<Record>> {
+ convert: F,
+) -> anyhow::Result<Vec<Record>>
+where
+ F: FnOnce(Vec<LegacySourceRecord>) -> anyhow::Result<Vec<LegacyTargetRecord>>,
+{
let mut source = Vec::new();
let mut output = Vec::new();
for record in records {