FIX: preview bust when no params

This commit is contained in:
Sam 2014-08-13 12:25:56 +10:00
parent b0179bd3d2
commit 1baa55fe45
2 changed files with 14 additions and 2 deletions

View file

@ -134,8 +134,10 @@ class BadgeGranter
def self.preview(sql, opts = {})
params = {user_ids: [], post_ids: [], backfill: true}
count_sql = "SELECT COUNT(*) count FROM (#{sql}) q"
grant_count = SqlBuilder.map_exec(OpenStruct, count_sql, params).first.count
# hack to allow for params, otherwise sanitizer will trigger sprintf
count_sql = "SELECT COUNT(*) count FROM (#{sql}) q WHERE :backfill = :backfill"
grant_count = SqlBuilder.map_exec(OpenStruct, count_sql, params).first.count.to_i
grants_sql =
if opts[:target_posts]
@ -144,11 +146,13 @@ class BadgeGranter
JOIN users u on u.id = q.user_id
LEFT JOIN badge_posts p on p.id = q.post_id
LEFT JOIN topics t on t.id = p.topic_id
WHERE :backfill = :backfill
LIMIT 10"
else
"SELECT u.id, u.username, q.granted_at
FROM(#{sql}) q
JOIN users u on u.id = q.user_id
WHERE :backfill = :backfill
LIMIT 10"
end

View file

@ -5,6 +5,14 @@ describe BadgeGranter do
let(:badge) { Fabricate(:badge) }
let(:user) { Fabricate(:user) }
describe 'preview' do
it 'can correctly preview' do
Fabricate(:user, email: 'sam@gmail.com')
result = BadgeGranter.preview('select id user_id, null post_id, created_at granted_at from users where email like \'%gmail.com\'')
result[:grant_count].should == 1
end
end
describe 'backfill' do
it 'has no broken badge queries' do