FIX: Select correct topic draft for user (#15234)

The old query could return multiple rows.
This commit is contained in:
Bianca Nenciu 2021-12-08 15:23:44 +02:00 committed by GitHub
parent 049bc33838
commit b1c11d5787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 1 deletions

View File

@ -211,7 +211,7 @@ class UserStat < ActiveRecord::Base
UPDATE user_stats
SET draft_count = (SELECT COUNT(*) FROM drafts WHERE user_id = :user_id)
WHERE user_id = :user_id
RETURNING draft_count, (SELECT 1 FROM drafts WHERE draft_key = :new_topic)
RETURNING draft_count, (SELECT 1 FROM drafts WHERE user_id = :user_id AND draft_key = :new_topic)
SQL
MessageBus.publish(

View File

@ -179,6 +179,8 @@ describe Draft do
end
it 'updates draft count when a draft is created or destroyed' do
Draft.set(Fabricate(:user), Draft::NEW_TOPIC, 0, "data")
messages = MessageBus.track_publish("/user") do
Draft.set(user, Draft::NEW_TOPIC, 0, "data")
end