core_api/schema.rs
1//! Declarative schema-as-code for mushroomdb.
2//!
3//! [`Schema`] is a serde-JSON-round-trippable description of the fulltext
4//! indexes, materialized views, and linking rules that should exist in a
5//! database. [`GraphDb::apply_schema`] applies the schema idempotently:
6//! items already matching the live database are left untouched (no WAL write),
7//! items that differ are replaced (delete + create), and items absent from the
8//! schema are left in place (no pruning — destructive removal is out of scope
9//! for this plan).
10//!
11//! # Application order
12//!
13//! 1. **Fulltext** indexes are applied first: rules may later benefit from
14//! freshly-enabled fulltext state during backfill, even though the current
15//! rule predicates do not require it.
16//! 2. **Views** are applied second: they are cheaper to backfill than rules
17//! and logically independent of rules.
18//! 3. **Rules** are applied last. Creating a rule triggers a full backfill
19//! of derived edges, which can be expensive for large graphs — document the
20//! cost at the call site.
21//!
22//! # Update semantics
23//!
24//! When a schema item (rule or view) exists in the database but its definition
25//! differs from the one in the schema, it is replaced via `delete_X` +
26//! `create_X`. For rules, the `create_rule` call triggers a full backfill of
27//! derived edges — this can be expensive for large graphs. The re-backfill
28//! cost is inherent to any definition change (the old edge set may not be
29//! valid under the new predicate), so there is no cheaper path in v1.
30//!
31//! # No pruning
32//!
33//! Items that live in the database but are absent from the schema are left
34//! untouched. Destructive removal ("prune items not in the schema") waits for
35//! explicit demand — YAGNI for this plan.
36
37use crate::roles::RoleDef;
38use crate::{GraphDb, Result, RuleDef, ViewDef};
39use core_storage::{fs::Fs, GraphError};
40use serde::{Deserialize, Serialize};
41
42// ---------------------------------------------------------------------------
43// Public types
44// ---------------------------------------------------------------------------
45
46/// A declarative description of the schema that should exist in a database.
47///
48/// All lists default to empty when absent from JSON, so a partial schema
49/// that names only rules (for example) is valid.
50#[derive(Serialize, Deserialize, Clone, Default, Debug)]
51pub struct Schema {
52 /// Fulltext index declarations as `(label, field)` pairs.
53 #[serde(default)]
54 pub fulltext: Vec<(String, String)>,
55 /// Equality-index declarations as `(label, field)` pairs. Each enables an
56 /// exact-match index so `WHERE n.field = value` becomes an indexed lookup.
57 #[serde(default)]
58 pub indexes: Vec<(String, String)>,
59 /// Linking rule definitions.
60 #[serde(default)]
61 pub rules: Vec<RuleDef>,
62 /// Materialized view definitions.
63 #[serde(default)]
64 pub views: Vec<ViewDef>,
65 /// RBAC role definitions. Persisted as `roles.json` sidecar on apply.
66 #[serde(default)]
67 pub roles: Vec<RoleDef>,
68}
69
70/// The outcome of a single [`GraphDb::apply_schema`] call.
71///
72/// Entry names are namespaced: `"rule:NAME"`, `"view:NAME"`,
73/// `"fulltext:LABEL.FIELD"`.
74#[derive(Debug, PartialEq)]
75pub struct SchemaDiff {
76 /// Items that did not exist and were created.
77 pub created: Vec<String>,
78 /// Items that existed but whose definition differed; replaced via
79 /// delete + create.
80 pub updated: Vec<String>,
81 /// Items that already matched the live database; no WAL writes made.
82 pub unchanged: Vec<String>,
83}
84
85// ---------------------------------------------------------------------------
86// apply_schema implementation
87// ---------------------------------------------------------------------------
88
89impl<F: Fs> GraphDb<F> {
90 /// Apply `schema` to the database idempotently.
91 ///
92 /// Returns a [`SchemaDiff`] describing what was created, updated, or left
93 /// unchanged. The diff is in application order: fulltext, then views,
94 /// then rules.
95 ///
96 /// Items absent from `schema` but present in the database are left
97 /// untouched (no pruning).
98 ///
99 /// # Atomicity of validation
100 ///
101 /// All rules and views that would be created or updated are validated
102 /// **before** any mutation is made. If any definition is invalid, the
103 /// function returns `Err` without touching the database. This prevents
104 /// the partial-application hazard where the old item is already deleted
105 /// before the invalid replacement fails.
106 ///
107 /// # Update cost
108 ///
109 /// Updating a rule (definition differs) triggers `delete_rule` +
110 /// `create_rule`. The `create_rule` call runs a full backfill of derived
111 /// edges. This can be expensive for large graphs; prefer stable rule
112 /// definitions in production.
113 pub fn apply_schema(&mut self, schema: &Schema) -> Result<SchemaDiff> {
114 // Pre-validation pass: validate every rule, view, and role that would be
115 // created or updated, before touching the database. Unchanged items
116 // are already valid (they passed validation when first created).
117 let live_views = self.views();
118 let live_rules = self.rules();
119
120 // Reject duplicate names within the submitted schema before any mutation.
121 {
122 let mut seen_rules = std::collections::HashSet::new();
123 for rule_def in &schema.rules {
124 if !seen_rules.insert(rule_def.name.as_str()) {
125 return Err(GraphError::RuleInvalid {
126 detail: format!("duplicate rule name in schema: {}", rule_def.name),
127 });
128 }
129 }
130 let mut seen_views = std::collections::HashSet::new();
131 for view_def in &schema.views {
132 if !seen_views.insert(view_def.name.as_str()) {
133 return Err(GraphError::RuleInvalid {
134 detail: format!("duplicate view name in schema: {}", view_def.name),
135 });
136 }
137 }
138 }
139
140 for view_def in &schema.views {
141 let would_mutate = live_views
142 .iter()
143 .find(|v| v.name == view_def.name)
144 .is_none_or(|live| live != view_def);
145 if would_mutate {
146 view_def
147 .validate()
148 .map_err(|e| GraphError::RuleInvalid { detail: e })?;
149 }
150 }
151
152 for rule_def in &schema.rules {
153 let would_mutate = live_rules
154 .iter()
155 .find(|r| r.name == rule_def.name)
156 .is_none_or(|live| live != rule_def);
157 if would_mutate {
158 rule_def
159 .validate()
160 .map_err(|e| GraphError::RuleInvalid { detail: e })?;
161 }
162 }
163
164 // Validate roles: non-empty names, unique names within the schema,
165 // and write-scope subset rule (§7.1 ruling: create/update/delete_labels
166 // must each be a subset of the role's read labels).
167 {
168 let mut seen = std::collections::HashSet::new();
169 for role_def in &schema.roles {
170 if role_def.name.is_empty() {
171 return Err(GraphError::RuleInvalid {
172 detail: "role name must not be empty".into(),
173 });
174 }
175 if !seen.insert(role_def.name.as_str()) {
176 return Err(GraphError::RuleInvalid {
177 detail: format!("duplicate role name: {}", role_def.name),
178 });
179 }
180 if let Some(write) = &role_def.write {
181 let read_labels: std::collections::HashSet<&str> =
182 role_def.labels.iter().map(String::as_str).collect();
183 // Check create_labels, update_labels, delete_labels — each must
184 // be a subset of the role's read labels (§7.1 subset ruling).
185 // create_edge_types and delete_edge_types are exempt (no read-scope
186 // analog for edge types).
187 for (field, labels) in [
188 ("create_labels", &write.create_labels),
189 ("update_labels", &write.update_labels),
190 ("delete_labels", &write.delete_labels),
191 ] {
192 for label in labels {
193 if !read_labels.contains(label.as_str()) {
194 return Err(GraphError::RuleInvalid {
195 detail: format!(
196 "role '{}': write scope {field} contains label '{}' \
197 that is not in the role's read labels (subset rule)",
198 role_def.name, label
199 ),
200 });
201 }
202 }
203 }
204 }
205 }
206 }
207
208 // Mutation pass — all definitions are known-valid from here.
209 let mut created = Vec::new();
210 let mut updated = Vec::new();
211 let mut unchanged = Vec::new();
212
213 // 1. Fulltext indexes.
214 for (label, field) in &schema.fulltext {
215 let key = format!("fulltext:{label}.{field}");
216 if self.is_fulltext_enabled(label, field) {
217 unchanged.push(key);
218 } else {
219 self.enable_fulltext(label, field)?;
220 created.push(key);
221 }
222 }
223
224 // 1b. Equality (property) indexes.
225 for (label, field) in &schema.indexes {
226 let key = format!("index:{label}.{field}");
227 if self.is_index_enabled(label, field) {
228 unchanged.push(key);
229 } else {
230 self.enable_index(label, field)?;
231 created.push(key);
232 }
233 }
234
235 // 2. Views.
236 for view_def in &schema.views {
237 let key = format!("view:{}", view_def.name);
238 if let Some(live) = live_views.iter().find(|v| v.name == view_def.name) {
239 if live == view_def {
240 unchanged.push(key);
241 } else {
242 // Delete + create to pick up the new definition.
243 self.delete_view(&view_def.name)?;
244 self.create_view(view_def.clone())?;
245 updated.push(key);
246 }
247 } else {
248 self.create_view(view_def.clone())?;
249 created.push(key);
250 }
251 }
252
253 // 3. Rules — creating a rule triggers a full backfill (see module doc).
254 for rule_def in &schema.rules {
255 let key = format!("rule:{}", rule_def.name);
256 if let Some(live) = live_rules.iter().find(|r| r.name == rule_def.name) {
257 if live == rule_def {
258 unchanged.push(key);
259 } else {
260 // Delete + create; create_rule backfills all derived edges.
261 self.delete_rule(&rule_def.name)?;
262 self.create_rule(rule_def.clone())?;
263 updated.push(key);
264 }
265 } else {
266 self.create_rule(rule_def.clone())?;
267 created.push(key);
268 }
269 }
270
271 // 4. Roles — sidecar only (no WAL records). Written atomically when
272 // any role is new or changed; unchanged roles leave the file untouched
273 // (byte-identical idempotency guarantee).
274 {
275 let live_roles = self.roles();
276 let mut new_roles: Vec<RoleDef> = live_roles.clone();
277 let mut roles_changed = false;
278
279 for role_def in &schema.roles {
280 let key = format!("role:{}", role_def.name);
281 if let Some(live) = live_roles.iter().find(|r| r.name == role_def.name) {
282 if live == role_def {
283 unchanged.push(key);
284 } else {
285 // Update the entry in new_roles.
286 if let Some(slot) = new_roles.iter_mut().find(|r| r.name == role_def.name) {
287 *slot = role_def.clone();
288 }
289 roles_changed = true;
290 updated.push(key);
291 }
292 } else {
293 new_roles.push(role_def.clone());
294 roles_changed = true;
295 created.push(key);
296 }
297 }
298
299 if roles_changed {
300 self.commit_roles(new_roles)?;
301 }
302 }
303
304 Ok(SchemaDiff {
305 created,
306 updated,
307 unchanged,
308 })
309 }
310}