summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorblackst0ne <blackst0ne.ru@gmail.com>2018-06-14 11:20:11 +1100
committerblackst0ne <blackst0ne.ru@gmail.com>2018-06-14 11:20:11 +1100
commit53ea0f221ee37e8be7365fac98e936c0df2da85d (patch)
tree67ab82c335458cffcea791d956ff38ac71681e6d
parentadb069881ae0253c5bf3718aded02976f13fc859 (diff)
downloadgitlab-ce-blackst0ne-rails5-fix-snippets-finder.tar.gz
[Rails5] Fix snippets_finder arel queriesblackst0ne-rails5-fix-snippets-finder
There is a bug https://github.com/rails/arel/issues/531 in Rails 5.0 and 5.1 in Arel/ActiveRecord. This commit converts arel based queries to their raw counterparts to make the finder work in Rails 5.0. These changes should be reverted when on Rails 5.2 as since that version such queries work well again. See the bug link.
-rw-r--r--app/finders/snippets_finder.rb15
-rw-r--r--changelogs/unreleased/blackst0ne-rails5-fix-snippets-finder.yml5
2 files changed, 15 insertions, 5 deletions
diff --git a/app/finders/snippets_finder.rb b/app/finders/snippets_finder.rb
index d498a2d6d11..9d3772d7541 100644
--- a/app/finders/snippets_finder.rb
+++ b/app/finders/snippets_finder.rb
@@ -54,7 +54,10 @@ class SnippetsFinder < UnionFinder
end
def authorized_snippets
- Snippet.where(feature_available_projects.or(not_project_related))
+ # This query was intentionally converted to a raw one to get it work in Rails 5.0.
+ # In Rails 5.0 and 5.1 there's a bug: https://github.com/rails/arel/issues/531
+ # Please convert it back when on rails 5.2 as it works again as expected since 5.2.
+ Snippet.where("#{feature_available_projects} OR #{not_project_related}")
.public_or_visible_to_user(current_user)
end
@@ -86,18 +89,20 @@ class SnippetsFinder < UnionFinder
def feature_available_projects
# Don't return any project related snippets if the user cannot read cross project
- return table[:id].eq(nil) unless Ability.allowed?(current_user, :read_cross_project)
+ return table[:id].eq(nil).to_sql unless Ability.allowed?(current_user, :read_cross_project)
projects = projects_for_user do |part|
part.with_feature_available_for_user(:snippets, current_user)
end.select(:id)
- arel_query = Arel::Nodes::SqlLiteral.new(projects.to_sql)
- table[:project_id].in(arel_query)
+ # This query was intentionally converted to a raw one to get it work in Rails 5.0.
+ # In Rails 5.0 and 5.1 there's a bug: https://github.com/rails/arel/issues/531
+ # Please convert it back when on rails 5.2 as it works again as expected since 5.2.
+ "snippets.project_id IN (#{projects.to_sql})"
end
def not_project_related
- table[:project_id].eq(nil)
+ table[:project_id].eq(nil).to_sql
end
def table
diff --git a/changelogs/unreleased/blackst0ne-rails5-fix-snippets-finder.yml b/changelogs/unreleased/blackst0ne-rails5-fix-snippets-finder.yml
new file mode 100644
index 00000000000..597b85de26f
--- /dev/null
+++ b/changelogs/unreleased/blackst0ne-rails5-fix-snippets-finder.yml
@@ -0,0 +1,5 @@
+---
+title: "[Rails5] Fix snippets_finder arel queries"
+merge_request: 19796
+author: "@blackst0ne"
+type: fixed