diff --git a/lib/with_advisory_lock/postgresql.rb b/lib/with_advisory_lock/postgresql.rb index 0740a62..386f5f6 100644 --- a/lib/with_advisory_lock/postgresql.rb +++ b/lib/with_advisory_lock/postgresql.rb @@ -10,7 +10,8 @@ def release_lock end def execute_successful?(pg_function) - sql = "SELECT #{pg_function}(#{lock_keys.join(',')}) AS #{unique_column_name}" + comment = lock_name.gsub(/(\/\*)|(\*\/)/, "--") + sql = "SELECT #{pg_function}(#{lock_keys.join(',')}) AS #{unique_column_name} /* #{comment} */" result = connection.select_value(sql) # MRI returns 't', jruby returns true. YAY! (result == 't' || result == true) diff --git a/test/lock_test.rb b/test/lock_test.rb index 9f42795..9c8a4d0 100644 --- a/test/lock_test.rb +++ b/test/lock_test.rb @@ -14,6 +14,14 @@ Tag.current_advisory_lock.must_match /#{lock_name}/ end end + + it 'can obtain a lock with a name that attempts to disrupt a SQL comment' do + dangerous_lock_name = 'test */ lock /*' + Tag.with_advisory_lock(dangerous_lock_name) do + Tag.current_advisory_lock.must_match(/#{Regexp.escape(dangerous_lock_name)}/) + end + + end end describe '.advisory_lock_exists?' do