Skip to content

Commit e15bdfd

Browse files
committed
raise exception if not in transaction
better message add test fix
1 parent 00f1cfe commit e15bdfd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/with_advisory_lock/postgresql.rb

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class PostgreSQL < Base
55
# See http://www.postgresql.org/docs/9.1/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
66
def try_lock
77
pg_function = "pg_try_advisory#{transaction ? '_xact' : ''}_lock#{shared ? '_shared' : ''}"
8+
9+
raise "#{pg_function} requires transaction" if transaction && !ActiveRecord::Base.connection.transaction_open?
10+
811
execute_successful?(pg_function)
912
end
1013

test/transaction_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,15 @@ def pg_lock_count
6969
end
7070
assert_equal(0, pg_lock_count)
7171
end
72+
73+
specify 'transaction level locks fail if not in transaction' do
74+
exception = assert_raises do
75+
Tag.with_advisory_lock 'test', transaction: true do
76+
raise 'should not get here'
77+
end
78+
end
79+
80+
assert_match(/#{Regexp.escape('requires transaction')}/, exception.message)
81+
end
7282
end
7383
end

0 commit comments

Comments
 (0)