Skip to content

Commit

Permalink
Fix AdvisoryLockable using wrong advisory_lockable_column attribute…
Browse files Browse the repository at this point in the history
… when aborting `create_with_advisory_lock`
  • Loading branch information
bensheldon committed May 23, 2024
1 parent e632cf3 commit 508faac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/concerns/good_job/advisory_lockable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module AdvisoryLockable

after_create lambda {
advisory_lock || begin
errors.add(self.class.advisory_lockable_column, "Failed to acquire advisory lock: #{lockable_key}")
errors.add(self.class._advisory_lockable_column, "Failed to acquire advisory lock: #{lockable_key}")
raise ActiveRecord::RecordInvalid # do not reference the record because it can cause I18n missing translation error
end
}, if: :create_with_advisory_lock
Expand Down
10 changes: 10 additions & 0 deletions spec/app/models/concerns/good_job/advisory_lockable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@

execution.advisory_unlock
end

it 'aborts when the lock already exists' do
existing = model_class.create!(active_job_id: SecureRandom.uuid, create_with_advisory_lock: true)

expect do
rails_promise { model_class.create!(active_job_id: existing.active_job_id, create_with_advisory_lock: true) }.value!
end.to raise_error(ActiveRecord::RecordInvalid)
ensure
existing.advisory_unlock
end
end

it 'is lockable' do
Expand Down

0 comments on commit 508faac

Please sign in to comment.