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

Fix AdvisoryLockable using wrong advisory_lockable_column attribute when aborting create_with_advisory_lock #1360

Merged
merged 1 commit into from
May 23, 2024
Merged
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
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