diff options
author | Toon Claes <toon@gitlab.com> | 2018-09-24 15:13:11 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2018-09-25 12:49:57 +0300 |
commit | 6823e7defb45dfd86d5258b40d6f82482d1ef451 (patch) | |
tree | c6ce206ac1f2bb790774e602ff1cc14d63483df1 /lib/gitlab/database | |
parent | b4c78a58f5c5faef8a485903e5053f767221935c (diff) |
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
Diffstat (limited to 'lib/gitlab/database')
-rw-r--r-- | lib/gitlab/database/subquery.rb | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/gitlab/database/subquery.rb b/lib/gitlab/database/subquery.rb index 2a6f39c6a27..36e4559b554 100644 --- a/lib/gitlab/database/subquery.rb +++ b/lib/gitlab/database/subquery.rb @@ -6,7 +6,15 @@ module Gitlab class << self def self_join(relation) 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) end |