DEV: Compatibility with Discourse on Rails 6.1 (#41)

Related to https://github.com/discourse/discourse/pull/12688.

The tags and categories count queries are failing on Rails 6.1 with `ERROR:  column "topics.bumped_at" must appear in the GROUP BY clause or be used in an aggregate function`. The reason these queries didn't fail prior to 6.1 was because Active Record would automatically drop the `ORDER BY` clause from the queries when we called `count` on them. This behavior has been [changed](5812feffe3) in 6.1 so now Active Record only drops the `ORDER BY` clause if there is no `GROUP BY` clause. Our queries do have `GROUP BY` clauses, so we need to drop the `ORDER BY` clauses ourselves before we call `count` to make them work on 6.1.
This commit is contained in:
Osama Sayegh 2021-04-15 19:17:26 +03:00 committed by GitHub
parent 2ab4e13892
commit 312cafe9d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -95,9 +95,9 @@ module Docs
INNER JOIN topic_tags ttx ON ttx.topic_id = topics.id
INNER JOIN tags t2 ON t2.id = ttx.tag_id
SQL
tags = count_query.group('t2.name').count
tags = count_query.group('t2.name').reorder('').count
tags = create_tags_object(tags)
categories = results.where('topics.category_id IS NOT NULL').group('topics.category_id').count
categories = results.where('topics.category_id IS NOT NULL').group('topics.category_id').reorder('').count
categories = create_categories_object(categories)
results_length = results.size