Skip to content

Commit

Permalink
fix distinct on
Browse files Browse the repository at this point in the history
  • Loading branch information
hooopo committed Nov 20, 2019
1 parent 0dce57a commit 2b39508
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 8 additions & 3 deletions app/models/category_tag_stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ def self.topic_moved(topic, from_category_id, to_category_id)
SET topic_count = topic_count + 1
WHERE tag_id in (:tag_ids)
AND category_id = :category_id
RETURNING tag_id
SQL

tag_ids = topic.tags.map(&:id)
updated_tag_ids = DB.query_single(sql, tag_ids: tag_ids, category_id: to_category_id)


DB.exec(sql, tag_ids: tag_ids, category_id: to_category_id)
updated_tag_ids = DB.query_single(<<~SQL, tag_ids: tag_ids, category_id: to_category_id)
SELECT tag_id
FROM #{self.table_name}
WHERE tag_id in (:tag_ids)
AND category_id = :category_id
SQL
(tag_ids - updated_tag_ids).each do |tag_id|
CategoryTagStat.create!(tag_id: tag_id, category_id: to_category_id, topic_count: 1)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/discourse_tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ def self.filter_allowed_tags(query, guardian, opts = {})
else
# It's possible that the selected tags violate some one-tag-per-group restrictions,
# so filter them out by picking one from each group.
limit_tag_ids = TagGroupMembership.select('distinct on (tag_group_id) tag_id')
limit_tag_ids = TagGroupMembership.select('tag_group_id,tag_id')
.where(tag_id: selected_tag_ids)
.where(tag_group_id: exclude_group_ids)
.group_by {|x| x.tag_group_id }.values.map{|x| x[0]}
.map(&:tag_id)
sql = "(tags.id NOT IN (#{TAG_GROUP_TAG_IDS_SQL} WHERE (tg.parent_tag_id NOT IN (?) OR tg.id in (?))) OR tags.id IN (?))"
query = query.where(sql, selected_tag_ids, exclude_group_ids, limit_tag_ids)
Expand All @@ -200,9 +201,10 @@ def self.filter_allowed_tags(query, guardian, opts = {})
exclude_group_ids = one_per_topic_group_ids(selected_tag_ids)

unless exclude_group_ids.empty?
limit_tag_ids = TagGroupMembership.select('distinct on (tag_group_id) tag_id')
limit_tag_ids = TagGroupMembership.select('tag_group_id,tag_id')
.where(tag_id: selected_tag_ids)
.where(tag_group_id: exclude_group_ids)
.group_by {|x| x.tag_group_id }.values.map{|x| x[0]}
.map(&:tag_id)
sql = "(tags.id NOT IN (#{TAG_GROUP_TAG_IDS_SQL} WHERE (tg.id in (?))) OR tags.id IN (?))"
query = query.where(sql, exclude_group_ids, limit_tag_ids)
Expand Down

0 comments on commit 2b39508

Please sign in to comment.