Skip to main content

relate_executor/
procedure_gate.rs

1//! The one gate-mapping module (ADR-0378, ADR-0741).
2//!
3//! The `db.ontology.*` procedure family is gated behind ADR-0371's
4//! `RELATE_PREVIEW` opt-in. Both consumers of that fact — the facade's
5//! plan gate (`relate::gate`) and the `SHOW PROCEDURES` catalog
6//! (`show_catalog`) — read this mapping, and the mapping itself derives the
7//! family from the canonical registry predicate
8//! ([`is_ontology_procedure`]), so the gate never re-spells the family.
9//!
10//! The preview-gated capability rows of `dbms.listCapabilities`
11//! (ADR-0741) live in the twin table [`GATED_CAPABILITIES`]: the row
12//! producer renders it, so a gated row cannot exist outside it, and the
13//! `relate` crate's consumer audit reads it back against the registry.
14
15use relate_core::PreviewFeature;
16use relate_ontology::MAX_SUPPORTED_SPEC_VERSION;
17
18use crate::procedure_registry::is_ontology_procedure;
19
20/// The preview feature gating procedure `name` (ADR-0378):
21/// `Some(PreviewFeature::Ontology)` when `name` resolves to one of the
22/// built-in `db.ontology.*` registry signatures, `None` for every other
23/// name. The lookup is by name against the built-in registry only and never
24/// consults the host procedure registry, so a user-registered procedure is
25/// not gated — a host `db.ontology.custom`, for example, resolves to no
26/// built-in signature and stays ungated.
27pub fn procedure_preview_feature(name: &str) -> Option<PreviewFeature> {
28    is_ontology_procedure(name).then_some(PreviewFeature::Ontology)
29}
30
31/// One preview-gated capability row of `dbms.listCapabilities`
32/// (ADR-0741): the feature whose opt-in lists it, and the row's three
33/// columns. A gated row is a content row, not a call gate — the
34/// procedure stays callable with the opt-in off and its always-on rows
35/// are unchanged; the gated row is simply absent.
36pub struct GatedCapability {
37    /// The preview feature whose opt-in lists the row.
38    pub feature: PreviewFeature,
39    /// The capability name — Neo4j's own entry name for the mirrored
40    /// row, so ontology tooling reads the same fact from either engine.
41    pub name: &'static str,
42    /// The capability description.
43    pub description: &'static str,
44    /// The capability value, a STRING constant.
45    pub value: &'static str,
46}
47
48/// The one table of preview-gated capability rows (ADR-0741): the row
49/// producer renders exactly these rows, each only under its feature's
50/// opt-in. Today one row — the supported ontology spec version, the
51/// pin `db.ontology.set` validates against, so the listing cannot
52/// drift from the gate.
53pub const GATED_CAPABILITIES: &[GatedCapability] = &[GatedCapability {
54    feature: PreviewFeature::Ontology,
55    name: "ontology.supported.version",
56    description: "The latest version of the Neo4j ontology graph spec supported by this DBMS.",
57    value: MAX_SUPPORTED_SPEC_VERSION,
58}];
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::procedure_registry::BUILTIN_PROCEDURE_SIGNATURES;
64
65    #[test]
66    fn the_ontology_family_is_the_only_gated_procedure_family() {
67        // The gated names, spelled literally: this is the independent fact
68        // the test pins, not a re-run of the predicate the mapping is
69        // defined from, so a mapping change that moved a name in or out of
70        // the family fails here.
71        let family = [
72            ("db.ontology.set", Some(PreviewFeature::Ontology)),
73            ("db.ontology.get", Some(PreviewFeature::Ontology)),
74            ("db.ontology.drop", Some(PreviewFeature::Ontology)),
75            ("db.ontology.apply", Some(PreviewFeature::Ontology)),
76        ];
77        for (name, expected) in family {
78            assert_eq!(procedure_preview_feature(name), expected, "{name}");
79        }
80        // A non-ontology name is ungated, and so is a host-registered
81        // `db.ontology.*` name: the mapping reads the built-in registry
82        // only.
83        assert_eq!(procedure_preview_feature("db.labels"), None);
84        assert_eq!(procedure_preview_feature("db.ontology.custom"), None);
85        // No other registry row is gated either: the gated set is exactly
86        // the four literal names above, so a registry row added or dropped
87        // without the matching mapping change fails here.
88        let gated: Vec<&str> = BUILTIN_PROCEDURE_SIGNATURES
89            .iter()
90            .filter(|signature| procedure_preview_feature(signature.name).is_some())
91            .map(|signature| signature.name)
92            .collect();
93        assert_eq!(
94            gated,
95            [
96                "db.ontology.apply",
97                "db.ontology.drop",
98                "db.ontology.get",
99                "db.ontology.set"
100            ]
101        );
102    }
103
104    #[test]
105    fn the_capability_table_is_the_one_mirrored_row() {
106        // The gated capability rows, spelled literally: the table is the
107        // row producer's one source, so the listing changes only through
108        // it. This pins the one mirrored entry (ADR-0741) — Neo4j's own
109        // name for the supported spec version, gated by the feature that
110        // gates the rest of the ontology surface. The value's pin
111        // ("1.1.0") is asserted where it is consumed, in the row
112        // producer's and the facade's tests.
113        assert_eq!(GATED_CAPABILITIES.len(), 1);
114        let row = &GATED_CAPABILITIES[0];
115        assert!(matches!(row.feature, PreviewFeature::Ontology));
116        assert_eq!(row.name, "ontology.supported.version");
117        assert!(!row.description.is_empty());
118    }
119}