Add scoped read and write transaction APIs - #22
Open
daniel-vacic wants to merge 1 commit into
Open
Conversation
daniel-vacic
marked this pull request as ready for review
July 29, 2026 18:24
simolus3
requested changes
Jul 30, 2026
Collaborator
There was a problem hiding this comment.
Thank you for the contribution! However, I don't think we should have these helpers in the Rust SDK (even though they make sense for our other SDKs).
This SDK is intentionally more low-level, and encourages the use of rusqlite for higher-level connection management. rusqlite happens to have excellent APIs for this:
- The Transaction struct is a RAII guard that calls
BEGINwhen constructed andROLLBACKwhen dropped in Rust (unless explicitly committed). The only reason we have an internalTransactionGuardis that we wantrusqliteto be optional in our implementation, but it's not a public API. - For statements, I also think something like Statement which finalizes on drop is a more appropriate API than a callback. It has a lifetime bound to the connection so it can't out-live a leased connection.
So rusqlite already provides the tools for statements to work here, and I don't think the powersync crate should expose high-level SQLite connection APIs. With the rusqlite feature enabled (it is by default), ConnectionLease dereferences to rusqlite::Connection and you can use these transaction / statement helpers.
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.
What changed
PowerSyncDatabase::read_transactionandwrite_transactionso it cannot suspend while SQLite owns a transaction or WAL snapshot
PowerSyncTransaction::with_statement, whose higher-ranked callbackprevents a prepared statement or row borrow from escaping the scope
BEGIN IMMEDIATEfor public write transactionsLifecycle guarantees
Droprolls backBEGIN/COMMIT/ROLLBACK/savepoint control is rejected inside thescoped callback
This is the scoped API discussed in #19. It complements pool-boundary cleanup in
#21: callers get a structurally safe API, while the pool boundary remains the
last line of defense for raw leases.
Validation
rustup run 1.96.0 cargo test -p powersyncrustup run 1.96.0 cargo clippy -p powersync --all-targets -- -D warningsrustup run 1.96.0 cargo fmt --all -- --checkThe suite includes real file-backed WAL checks for success, error, panic, failed
commit, rejected transaction control and read-snapshot release. A compile-fail
doctest proves that a statement cannot escape
with_statement.