Commit 6823e7de authored by Toon Claes's avatar Toon Claes

Work around a bug in Rails 5, where LIMIT causes trouble

The original code caused Rails to generate invalid SQL. The problem
lays in the `.arel` method in `ActiveRecord::Relation`. When there was
a `limit` on the relation, the `LIMIT` statement was taken over to
Arel, but the value wasn't.

```ruby
relation = Event.limit(2)
relation.to_sql
#=> "SELECT  `events`.* FROM `events` LIMIT 2"
relation.arel.to_sql
#=> "SELECT  `events`.* FROM `events` LIMIT ?"
```

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/51729
parent b4c78a58
...@@ -6,7 +6,15 @@ module Gitlab ...@@ -6,7 +6,15 @@ module Gitlab
class << self class << self
def self_join(relation) def self_join(relation)
t = relation.arel_table t = relation.arel_table
t2 = relation.arel.as('t2') t2 = if !Gitlab.rails5?
relation.arel.as('t2')
else
# Work around a bug in Rails 5, where LIMIT causes trouble
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/51729
r = relation.limit(nil).arel
r.take(relation.limit_value) if relation.limit_value
r.as('t2')
end
relation.unscoped.joins(t.join(t2).on(t[:id].eq(t2[:id])).join_sources.first) relation.unscoped.joins(t.join(t2).on(t[:id].eq(t2[:id])).join_sources.first)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment