Skip to content

Data history: emit property Delete events on twin/relationship deletion (opt-in) - #130

Merged
nikoraes merged 1 commit into
mainfrom
feat/emit-property-delete-events-129
Dec 2, 2026
Merged

nikoraes merged 1 commit into
mainfrom
feat/emit-property-delete-events-129

Conversation

@nikoraes

@nikoraes nikoraes commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Closes #129

Data History emitted only the lifecycle Delete when a twin/relationship was deleted, so the property series stopped at the last Update. Any reconstruction resolving node/edge values from the latest row per (Id, Key) kept answering with the pre-delete value, and a twin re-created with the same id inherited every property the new write did not set.

What changes

Opt-in flag Parameters:EmitPropertyEventsOnDelete (Events service, default false, env Parameters__EmitPropertyEventsOnDelete=true), threaded Program.cs → SharedEventConsumer → CloudEventFactory.CreateDataHistoryEvents, mirroring TrackLastUpdatedBy's plumbing. With it off the emitted stream is unchanged — the existing CreateDataHistoryEvents_HandlesTwinDeleteEventWithProperties (exactly one event) is untouched and green.

CloudEventFactory.CreatePropertyDeleteEvents emits one property Delete per non-$ top-level property of the deleted entity, in addition to the lifecycle row (a separate method: CreatePropertyEvents requires both OldValue and NewValue):

field value
action Delete
key top-level property name (object values kept whole, same as a remove /prop patch)
value the removed value from the replication row's image (OldValue)
id $dtId, or $sourceId for a relationship
modelId $metadata.$model (present for twins, absent on relationships)
relationshipId / relationshipTarget from the edge image (relationship deletes only)
timeStamp the delete's replication ServerClock — never UtcNow
sourceTimeStamp omitted
updatedBy only with TrackLastUpdatedBy: the twin/relationship-level $metadata.$lastUpdatedBy, not the per-property entry (#127)
subject <id> or <id>/relationships/<relationshipId>

$-keys never produce an event. A twin that carries no properties produces none. A missing/empty replication image produces none and logs a warning — never a null-valued Delete row, which would be indistinguishable from a real value change.

No sink, table or migration change: KustoEventSink.BuildPropertyEventMappings and PostgresEventSink already map action/Action.

Relationships keep the documented operating contract. Deleting a twin does not cascade here: AGE rejects a plain DELETE of a vertex that still has edges, so the caller deletes the relationships first, and each DeleteRelationshipAsync is its own event with its own property Delete rows. DETACH DELETE is deliberately not introduced in this PR (the issue is explicit about that) — the diff touches no client code.

Two findings worth your eye

  1. Rows of one transaction are not bit-identical in TimeStamp. The issue's §3 says the events within a transaction share one ServerClock. Measured on the relationship delete (13:04:32.078716Z vs its lifecycle row): they are clustered but not equal — the per-row ServerClock of a pgoutput message is not the transaction's commit stamp. The invariant the reconstruction relies on is ordering (Delete < Create across transactions), which holds; the integration test asserts a transaction window rather than equality, with the measurement in a comment.
  2. The ADX Action column is a pre-existing gap, not one this PR introduces. BuildPropertyEventMappings has always mapped Action ← $.action, while the schema table in docs/how-to-guides/data-history-kusto.mdx omitted it. I added the row plus the .alter-merge table AdtPropertyEvents add Action:string step for hand-created tables. I could not exercise ADX queued ingestion here, so I did not claim what it does with a mapped-but-missing column — the doc states the column is required and how to add it.

Tests

Unit (CloudEventFactoryTests, 8 new): flag off → exactly 1 lifecycle event; flag on → lifecycle + one per property with correct key/value/id/modelId/subject/timeStamp, no sourceTimeStamp; $-keys never emit; relationship variant; updatedBy from twin-level metadata while the per-property entry names the previous writer; twin without properties → no extras; null/empty image → nothing and no throw; delete→re-create ordering.

Integration (EventsIntegrationTests, 3 new, over live replication; the shared fixture runs the consumer with the flag on — flag-off byte-identical behaviour is pinned by the unit tests):

  • twin delete with 2 properties → lifecycle Delete + 2 property Delete rows sharing the delete timestamp, with model id and values;
  • the reported defect: create a+b → delete → re-create with a only → latest row per (Id, Key) is Delete for b and the re-create's Create for a, and that Create is strictly newer;
  • relationship-then-twin delete → edge lifecycle Delete + one property Delete per edge property, then the twin's lifecycle Delete + one per twin property, in that TimeStamp order, nothing emitted twice.

Verified on ghcr.io/konnektr-io/age-pgvector:16-1.6.0-standard-trixie (PG 16.15, AGE 1.6.0, cnpg driver): AgeDigitalTwins.Events.Test 117 passed / 0 failed, AgeDigitalTwins.Test 358 passed / 8 skipped / 0 failed. The PG18/AGE 1.8 and apache/AGE legs are CI's to cover.

Docs

data-history-kusto.mdx (opt-in section, Action in the schema, note on the reconstruction query), data-history-postgres.mdx (global key, delete-row semantics, note), configuration.mdx (Events service key), CLAUDE.md (event semantics, delete-relationships-first contract).

…on (opt-in)

Data History emitted only the lifecycle Delete row when an entity was deleted, so the
property series stopped at the last Update. Anything that rebuilds node/edge values from
the latest row per (Id, Key) kept resolving the pre-delete value: a twin re-created with
the same id inherited every property the new write did not set.

Behind Parameters:EmitPropertyEventsOnDelete (default false, ADT parity) the factory now
emits one property Delete row per non-$ top-level property of the deleted twin or
relationship, in addition to the lifecycle Delete. Rows are synthesized from the
replication row's property image (OldValue), carry the delete's TimeStamp and the removed
value, and use the twin/relationship-level $metadata.$lastUpdatedBy for updatedBy (the
per-property entry still names the previous writer, #127). A missing image emits nothing
and logs a warning rather than a null-valued Delete row. No sink, table or migration
change: both sinks already map action/Action.

Relationship deletes keep the documented delete-relationships-first contract: each one is
its own RelationshipDelete event and gets its own property Delete rows. No DETACH DELETE,
no implicit cascade, so a twin delete emits property deletes for the twin's own
properties only.

Tests: 8 unit cases (flag off unchanged, flag on shape, system keys, relationship variant,
updatedBy source, empty image, delete<recreate ordering) and 3 integration cases over live
replication (per-property deletes, latest-row-per-key termination after a partial
re-create, relationship-then-twin delete ordering).

Refs #129

Co-authored-by: Hermes Agent <hermes@nikoraes.local>
@nikoraes
nikoraes force-pushed the feat/emit-property-delete-events-129 branch from 4732721 to c5acd10 Compare December 2, 2026 13:07
@nikoraes
nikoraes merged commit e65928b into main Dec 2, 2026
5 checks passed
@nikoraes
nikoraes deleted the feat/emit-property-delete-events-129 branch December 2, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data history: emit property Delete events on twin/relationship deletion (opt-in)

1 participant