fix: Transaction::run() never commits — closure signature change#103
Merged
Conversation
The previous implementation took the closure as `FnOnce(TransactionContext)`,
which consumed `ctx` and dropped it before `commit()` was ever called. The
underlying `sqlx::Transaction::Drop` then rolled back, so every successful
`run()` silently lost the writes.
Change the closure signature to `AsyncFnOnce(&mut TransactionContext) ->
Result<T, E>` so `run` retains ownership of `ctx`. On Ok, call
`ctx.commit().await` and propagate any commit error. On Err, drop `ctx` so
sqlx's Drop performs the rollback (existing behavior, now intentional).
`run_with_commit` keeps its by-value closure signature — it intentionally
hands ownership of `ctx` to the closure for explicit commit/rollback flow.
Bump versions to reflect the breaking signature change on a pre-1.0 crate:
- entity-core 0.4.0 -> 0.5.0
- entity-derive-impl 0.4.0 -> 0.5.0
- entity-derive 0.6.0 -> 0.7.0
Update internal docs and both transaction examples to the new
`async |ctx| { ... }` form. Add a compile-time regression guard in the
transaction.rs test module so the signature can never silently regress to
consuming `ctx` by value again. A real commit/rollback integration test
requires Postgres in CI and is tracked separately.
Closes #102
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced May 11, 2026
RAprogramm
added a commit
that referenced
this pull request
May 11, 2026
…ctions example (#121) README had drifted from the current crates: - Installation block still pinned `entity-derive = "0.4"`. Updated to `0.8` (latest published). - Added a "Feature flags" section enumerating every Cargo feature: `postgres`, `clickhouse`, `mongodb`, `streams`, `api`, `validate`, `tracing`. Each gets a one-line description so users can pick what they need without reading the source. - Added a "Transactions" subsection under Quick Reference with a current, copy-pasteable `.run(async |ctx| { ... })` example. The previous text mentioned transactions only as a feature-table bullet; the API changed in 0.7.0 (#103) and the README never demonstrated the fixed signature. Cross-links to `run_with_commit` for conditional-commit use cases. - Added a "Tracing" subsection showing the feature flag, expected dependencies, sample log output, and the zero-cost guarantee when the flag is off — the feature was added in 0.8.0 (#118 / #119) and was previously undocumented at the README level. - Added "Structured Logging" to the Features table. Closes #120
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 #102
Summary
entity_core::transaction::Transaction::run()previously took the closure asFnOnce(TransactionContext), consumedctx, and dropped it beforecommit()could be called — every successfulrun()silently rolled back viasqlx::Transaction::Drop.AsyncFnOnce(&mut TransactionContext) -> Result<T, E>sorunkeeps ownership and callsctx.commit().awaitexplicitly on Ok. On Err, droppingctxperforms the rollback as before (now intentional rather than accidental).run_with_commitis unchanged — it deliberately movesctxinto the closure so callers can decide when/whether to commit.Breaking change
This changes a public function's closure signature. Per
STABILITY.md, pre-1.0 minor bumps may include breaking changes. Versions bumped:Old broken versions (0.4.0 / 0.4.0 / 0.6.0) should be yanked from crates.io after this PR is released.
Migration
Why this slipped through 0.6.0
Codecov flagged
crates/entity-core/src/transaction.rsat 25% on PR #101. Neitherrunnorrun_with_commithad any test exercising actual commit/rollback semantics; onlyTransactionErrorvariants were covered. The bug was visible in the diff but the PR was merged without addressing the Codecov warning.Test plan
cargo check --all-features— entity-core, entity-derive, entity-derive-impl, examples/transactions, examples/full-appcargo clippy --all-targets --all-features -- -D warningscargo +nightly fmt --all -- --checkcargo test --all-features(existing unit tests still pass)transaction.rstest module — ensures the signature requires&mut TransactionContextand rejects by-valueTransactionContextpostgresservice to CI, tracked as follow-up