Skip to content

fix(aggregate_root): emit Created event from save() for streams entities#134

Merged
RAprogramm merged 1 commit into
mainfrom
133
May 11, 2026
Merged

fix(aggregate_root): emit Created event from save() for streams entities#134
RAprogramm merged 1 commit into
mainfrom
133

Conversation

@RAprogramm
Copy link
Copy Markdown
Owner

Closes #133

Summary

Missing companion to #125. `save_method` (aggregate-root INSERT) already used a transaction but never spliced `pg_notify` — so streams subscribers missed every new aggregate insert.

Now `save()` runs `pg_notify` against the same transaction handle (`&mut *tx`) used by the INSERT, before the commit. Postgres only broadcasts `Created` on commit, discards on rollback. When `streams` is off, `notify_created()` returns an empty TokenStream so non-streams aggregate roots keep their single round-trip.

Tests (3 new)

Test Asserts
`save_emits_pg_notify_when_streams_enabled` streams + aggregate_root produces body with `pg_notify` executed on `&mut *tx`
`save_omits_pg_notify_when_streams_disabled` aggregate_root alone does NOT emit `pg_notify` (regression guard)
`save_is_empty_for_non_aggregate_root` save_method returns empty without aggregate_root (control)

Version bump

Crate Old New
entity-derive-impl 0.6.3 0.6.4
entity-derive 0.8.4 0.8.5

`entity-core` is unchanged.

Test plan

  • `cargo test --all-features` — 551 lib + 9 streams + 45 core + 2 integration + trybuild pass.
  • `cargo clippy --all-targets --all-features -- -D warnings` — clean.
  • `cargo +nightly fmt --all -- --check` — clean.
  • CI + Codecov green before merge.

…entities

`crates/entity-derive-impl/src/entity/sql/postgres/save.rs::save_method`
already wrapped the INSERT in a transaction (#118 era), but for entities
with both `aggregate_root` and `streams` enabled it never spliced
`pg_notify`. Subscribers missed every new aggregate-root insert silently,
even though the row write itself was atomic.

Splice `self.notify_created()` into the existing transaction body, after
the INSERT but before the commit. The notify executes against
`&mut *tx`, the same handle the INSERT uses, so Postgres only broadcasts
`Created` on commit and discards it on rollback — the same guarantee
already in place for `create_method` (#125).

When `streams` is off, `notify_created()` returns an empty TokenStream,
so non-streams aggregate roots stay one round-trip with no regression.

Tests (3 new lib tests in `save::tests`):

- `save_emits_pg_notify_when_streams_enabled`: combined attribute set
  produces a save body that contains `pg_notify` and executes it on
  `&mut *tx`.
- `save_omits_pg_notify_when_streams_disabled`: aggregate_root without
  streams does NOT emit `pg_notify` (perf regression guard).
- `save_is_empty_for_non_aggregate_root`: untouched control case —
  `save_method` returns empty when aggregate_root is off.

Bump:

- entity-derive-impl: 0.6.3 -> 0.6.4
- entity-derive: 0.8.4 -> 0.8.5

`entity-core` is unchanged.

Closes #133
@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@RAprogramm RAprogramm merged commit ab05134 into main May 11, 2026
17 checks passed
@RAprogramm RAprogramm deleted the 133 branch May 11, 2026 10:57
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.

fix(aggregate_root): emit Created event from save() for streams-enabled entities

1 participant