Skip to content

Commit 24aa845

Browse files
committed
Database#transaction returns block result
1 parent 62dc6e7 commit 24aa845

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This release drops support for Ruby 2.7. [#453] @flavorjones
3333
- `Database#columns` returns a list of internal frozen strings. [#155, #474, #486] @tenderlove
3434
- Freeze results that come from the database. [#480] @tenderlove
3535
- The encoding of a Database is no longer cached. [#485] @tenderlove
36+
- `Database#transaction` returns the result of the block when used with a block. @alexcwatt
3637

3738

3839
### Removed

lib/sqlite3/database.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,16 +644,17 @@ def transaction(mode = nil)
644644
if block_given?
645645
abort = false
646646
begin
647-
yield self
647+
result = yield self
648648
rescue
649649
abort = true
650650
raise
651651
ensure
652652
abort and rollback or commit
653653
end
654+
result
655+
else
656+
true
654657
end
655-
656-
true
657658
end
658659

659660
# Commits the current transaction. If there is no current transaction,

test/test_database.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,5 +686,15 @@ def test_default_transaction_mode
686686
ensure
687687
tf&.unlink
688688
end
689+
690+
def test_transaction_returns_true_without_block
691+
transaction = @db.transaction
692+
assert_equal true, transaction
693+
end
694+
695+
def test_transaction_returns_block_result
696+
result = @db.transaction { :foo }
697+
assert_equal :foo, result
698+
end
689699
end
690700
end

0 commit comments

Comments
 (0)