fix(aggregate_root): emit Created event from save() for streams entities#134
Merged
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
Version bump
`entity-core` is unchanged.
Test plan