fix(backend): scope node-level constraint validators to the changed field/path

Merged
AJ
4mo ago
+200-25

Why

IFC-2604 (#9896) scoped constraint validation to the kinds that have a data diff, but within a
changed kind the determiner still emitted every node-level constraint (uniqueness, hierarchy)
regardless of which field changed. A data change to a non-unique attribute still triggered
node.uniqueness_constraints.update for the whole kind.

Goal: emit a node-level constraint only when the specific attribute/relationship/path it guards
is actually part of the diff.

Non-goals: the node-scoped execution of the uniqueness and other constraint validators
(follow-ups IFC-2796 / IFC-2797, which assume this trigger gate exists); no change to the
schema-diff constraint source, which is already field-scoped.

Part of epic IFC-2706. Closes IFC-2795.

What changed

Behavioral changes — a data change to field X of kind K now emits a node-level constraint only
when X participates in that constraint's guarded path:

  • Uniqueness (node.uniqueness_constraints.update) fires only when a unique=True attribute or a
    uniqueness_constraints path element changed. This includes the peer case: a constraint like
    owner__name fires only when the related node's name changed, not when an unrelated peer
    attribute (e.g. height) changed.
  • Hierarchy (node.parent.update / node.children.update) fires only when the parent/children
    relationship changed on that kind — re-parenting a child no longer triggers the children check on
    the parent, and vice-versa.
  • Generic-level uniqueness still fires when an implementation's inherited field changes (a
    generic's constraint spans every implementing node).

Implementation notes:

  • All "only-when-required" trigger logic lives in the determiner (ConstraintValidatorDeterminer),
    per the epic's design. A new _node_property_triggered_by_diff gate dispatches to
    _diff_triggers_uniqueness (unique attributes + constraint-path fields, local and peer) and a
    parent/children relationship-diff check.
  • _field_in_diff handles generic -> implementation propagation: an inherited field keeps its name
    on the child, so a change on an implementation still triggers the generic's uniqueness gate.
  • The coarser _has_uniqueness_constraint_on_peer (which fired on any change to a peer kind) is
    replaced by the field-precise _diff_triggers_uniqueness, reusing get_attribute_path_from_string.
  • Unknown node-level properties default to emitting, so a newly-added node-level constraint is
    never silently missed.

What stayed the same:

  • Attribute and relationship constraints were already field-scoped — unchanged.
  • The schema-diff source (MergeSchemaAnalyzer.calculate_validations ->
    SchemaUpdateValidationResult.process_diff) was already scoped per changed property — no code
    change; a guard test locks that in.
  • No DB schema or migration changes. Validator execution, constraint payloads, and the API contract
    are unchanged — only which validators are triggered.

How to review

Suggested order:

  1. backend/infrahub/core/validators/determiner.py — the gate plus the two helpers
    (_diff_triggers_uniqueness, _field_in_diff) and the peer-selection change in
    _get_property_constraints_for_impacted_kinds.
  2. backend/tests/component/core/constraint_validators/test_determiner.py — narrowed and new
    exact-set assertions.
  3. backend/tests/unit/core/schema/test_schema_update_validation.py — the schema-diff guard (also
    the new home for the relocated schema-update-validation unit tests, moved out of the
    grab-bag test_models.py).

The risk to scrutinize is under-selection — narrowing which validators fire must not drop one a
change could actually violate. Extra scrutiny welcome on the peer/generic uniqueness cases in
_diff_triggers_uniqueness / _field_in_diff; the uniqueness-merge integration suite guards the
end-to-end behavior.

Impact & rollout

  • Backward compatibility: no API or schema changes; validation is narrowed only, never widened.
  • Performance: the motivating win — a data change now runs only the node-level validators whose
    guarded field actually changed, reducing constraint validation work on proposed-change checks,
    merges, and rebases.
  • Config/env changes: none.
  • Deployment notes: safe to deploy.

Checklist

  • Tests added/updated
  • Changelog entry added
  • External docs updated (n/a — internal validation behavior)
  • Internal .md docs updated (n/a — internal validation behavior)
  • I have reviewed AI generated content

Summary by cubic

Scopes node-level constraint validation (uniqueness, hierarchy) to only the specific changed field/path, reducing unnecessary checks and speeding up diff validation. Aligns with IFC-2795; adds precise path parsing, inheritance-aware peer checks, and logs unparseable paths.

  • Bug Fixes
    • Uniqueness fires only when a unique attribute or a uniqueness_constraints path segment changed (local relationship or peer like owner__name); paths parsed via schema.parse_schema_path; unparseable elements are skipped and warned.
    • Inheritance-aware: changes to inherited fields trigger generic-level uniqueness; a path that targets a generic peer fires when an implementation’s attribute changes.
    • Hierarchy fires only when parent or children changed on that kind; each endpoint emits only its changed side.
    • The determiner gates node-level properties per field/path; unknown properties default to emit so new constraints aren’t missed.
    • Schema-diff validation stays field-scoped; guard test added.