feat: add create-only seed mode to seed_domain() for production use #98

Open
opened 2026-02-11 15:13:59 +00:00 by mik-tf · 0 comments
Owner

Context

PR lhumina_code/hero_osis#2 fixed duplicate seeding by adding stable SIDs to all mock TOML files. With stable SIDs, db.set() performs an upsert instead of creating new records on every restart.

However, the current seeding behavior always overwrites existing records. In production, this means user edits to seeded entities get reset on every server restart.

Proposed Change

Add a seed_mode parameter to seed_domain() in packages/osis/src/rpc/server.rs:

  • upsert (current behavior): always overwrite with seed data
  • create-only: check if entity exists (via exists(sid)) before calling Set. If it already exists, skip it.

The mode should be passed through from seed_from_config_dir() so that callers (like hero_osis) can control it.

Suggested implementation in seed_domain():

// Before the Set call:
if seed_mode == SeedMode::CreateOnly {
    // Check if entity already exists
    if let Ok(handler_lock) = self.state.get_handler(context_name, domain_name, &type_lower).await {
        let handler = handler_lock.read().await;
        let exists_req = RpcRequest {
            method: Method::Exists,
            job_id: format!("seed-check-{}", sid),
            data: sid.clone(),
        };
        let exists_resp = handler.handle_request(&exists_req);
        if exists_resp.success && exists_resp.data == "true" {
            tracing::debug!("  Skipping {} (already exists): {}", type_name, sid);
            continue;
        }
    }
}
## Context PR https://forge.ourworld.tf/lhumina_code/hero_osis/pulls/2 fixed duplicate seeding by adding stable SIDs to all mock TOML files. With stable SIDs, `db.set()` performs an upsert instead of creating new records on every restart. However, the current seeding behavior always **overwrites** existing records. In production, this means user edits to seeded entities get reset on every server restart. ## Proposed Change Add a `seed_mode` parameter to `seed_domain()` in `packages/osis/src/rpc/server.rs`: - **`upsert`** (current behavior): always overwrite with seed data - **`create-only`**: check if entity exists (via `exists(sid)`) before calling `Set`. If it already exists, skip it. The mode should be passed through from `seed_from_config_dir()` so that callers (like hero_osis) can control it. ### Suggested implementation in `seed_domain()`: ```rust // Before the Set call: if seed_mode == SeedMode::CreateOnly { // Check if entity already exists if let Ok(handler_lock) = self.state.get_handler(context_name, domain_name, &type_lower).await { let handler = handler_lock.read().await; let exists_req = RpcRequest { method: Method::Exists, job_id: format!("seed-check-{}", sid), data: sid.clone(), }; let exists_resp = handler.handle_request(&exists_req); if exists_resp.success && exists_resp.data == "true" { tracing::debug!(" Skipping {} (already exists): {}", type_name, sid); continue; } } } ``` ## Related - hero_osis CLI flag: tracked separately in hero_osis repo - Idempotent seeding PR: https://forge.ourworld.tf/lhumina_code/hero_osis/pulls/2
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lhumina_code/hero_lib#98
No description provided.