Skip to content

Commit

Permalink
Fix Lockable's SELECT 1 AS one queries missing LIMIT 1 (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon authored Apr 30, 2023
1 parent 8c23b5a commit 311bcd8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/good_job/lockable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def advisory_locked?(key: lockable_key)
AND pg_locks.objsubid = 1
AND pg_locks.classid = ('x' || substr(md5($1::text), 1, 16))::bit(32)::int
AND pg_locks.objid = (('x' || substr(md5($2::text), 1, 16))::bit(64) << 32)::bit(32)::int
LIMIT 1
SQL
binds = [
ActiveRecord::Relation::QueryAttribute.new('key', key, ActiveRecord::Type::String.new),
Expand Down Expand Up @@ -351,6 +352,7 @@ def owns_advisory_lock?(key: lockable_key)
AND pg_locks.classid = ('x' || substr(md5($1::text), 1, 16))::bit(32)::int
AND pg_locks.objid = (('x' || substr(md5($2::text), 1, 16))::bit(64) << 32)::bit(32)::int
AND pg_locks.pid = pg_backend_pid()
LIMIT 1
SQL
binds = [
ActiveRecord::Relation::QueryAttribute.new('key', key, ActiveRecord::Type::String.new),
Expand Down
7 changes: 6 additions & 1 deletion spec/support/reset_good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ def unlock
query = <<~SQL.squish
SELECT pg_advisory_unlock(($1::bigint << 32) + $2::bigint) AS unlocked
SQL
self.class.connection.exec_query(GoodJob::Execution.pg_or_jdbc_query(query), 'PgLock Advisory Unlock', [[nil, classid], [nil, objid]]).first['unlocked']

binds = [
ActiveRecord::Relation::QueryAttribute.new('classid', classid, ActiveRecord::Type::String.new),
ActiveRecord::Relation::QueryAttribute.new('objid', objid, ActiveRecord::Type::String.new),
]
self.class.connection.exec_query(GoodJob::Execution.pg_or_jdbc_query(query), 'PgLock Advisory Unlock', binds).first['unlocked']
end

def unlock!
Expand Down

0 comments on commit 311bcd8

Please sign in to comment.