From 557c8b2d100213a62fcfd6d1d26aebfe045156cf Mon Sep 17 00:00:00 2001 From: Alex Watt Date: Tue, 27 Feb 2024 22:18:18 -0500 Subject: [PATCH] Database#transaction returns block result --- CHANGELOG.md | 1 + lib/sqlite3/database.rb | 4 ++-- test/test_database.rb | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fba081a9..f2ca5fd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ This release drops support for Ruby 2.7. [#453] @flavorjones - `Database#columns` returns a list of internal frozen strings. [#155, #474, #486] @tenderlove - Freeze results that come from the database. [#480] @tenderlove - The encoding of a Database is no longer cached. [#485] @tenderlove +- `Database#transaction` returns the result of the block when used with a block. @alexcwatt ### Removed diff --git a/lib/sqlite3/database.rb b/lib/sqlite3/database.rb index e72c0ef1..b7f78016 100644 --- a/lib/sqlite3/database.rb +++ b/lib/sqlite3/database.rb @@ -651,9 +651,9 @@ def transaction(mode = nil) ensure abort and rollback or commit end + else + true end - - true end # Commits the current transaction. If there is no current transaction, diff --git a/test/test_database.rb b/test/test_database.rb index 5c902eed..14a2b653 100644 --- a/test/test_database.rb +++ b/test/test_database.rb @@ -686,5 +686,14 @@ def test_default_transaction_mode ensure tf&.unlink end + + def test_transaction_returns_true_without_block + assert @db.transaction + end + + def test_transaction_returns_block_result + result = @db.transaction { :foo } + assert_equal :foo, result + end end end