Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise exception if not in transaction #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/with_advisory_lock/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class PostgreSQL < Base
# See http://www.postgresql.org/docs/9.1/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
def try_lock
pg_function = "pg_try_advisory#{transaction ? '_xact' : ''}_lock#{shared ? '_shared' : ''}"

raise "#{pg_function} requires transaction" if transaction && !ActiveRecord::Base.connection.transaction_open?

execute_successful?(pg_function)
end

Expand Down
10 changes: 10 additions & 0 deletions test/transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,15 @@ def pg_lock_count
end
assert_equal(0, pg_lock_count)
end

specify 'transaction level locks fail if not in transaction' do
exception = assert_raises do
Tag.with_advisory_lock 'test', transaction: true do
raise 'should not get here'
end
end

assert_match(/#{Regexp.escape('requires transaction')}/, exception.message)
end
end
end