diff options
author | Toon Claes <toon@gitlab.com> | 2018-09-18 18:32:21 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2018-09-20 17:27:09 +0300 |
commit | ce830d3c60fbf445c67fb923f03678ad2333eba5 (patch) | |
tree | 68a6b4a82244e15eb9c81d697ae9ac2cacad3642 /lib/gitlab/database | |
parent | 8c2192943a5efc4d0a28c67b04bf9b979def66a1 (diff) |
Add Gitlab::Database::Subquery.self_join to delete_all with limit
`delete_all` doesn't support limit, so you'd need to subquery
that. And instead of subquerying with `where(id: query)`, it's better
to use an `INNER JOIN`. This method also works with MySQL, while
subquerying doesn't (without another layer of subquerying)
Reference:
https://stackoverflow.com/questions/17892762/mysql-this-version-of-mysql-doesnt-yet-support-limit-in-all-any-some-subqu/17892886#17892886
Diffstat (limited to 'lib/gitlab/database')
-rw-r--r-- | lib/gitlab/database/subquery.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/gitlab/database/subquery.rb b/lib/gitlab/database/subquery.rb new file mode 100644 index 00000000000..2a6f39c6a27 --- /dev/null +++ b/lib/gitlab/database/subquery.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Gitlab + module Database + module Subquery + class << self + def self_join(relation) + t = relation.arel_table + t2 = relation.arel.as('t2') + + relation.unscoped.joins(t.join(t2).on(t[:id].eq(t2[:id])).join_sources.first) + end + end + end + end +end |