Commit 42ef96b7 authored by Doug Stull's avatar Doug Stull Committed by Alex Ives

Enumerate columns to address db query cache issues on column change

parent 80bea93f
...@@ -83,7 +83,10 @@ class MembersFinder ...@@ -83,7 +83,10 @@ class MembersFinder
union = Gitlab::SQL::Union.new(union_members, remove_duplicates: false) # rubocop: disable Gitlab/Union union = Gitlab::SQL::Union.new(union_members, remove_duplicates: false) # rubocop: disable Gitlab/Union
sql = distinct_on(union) sql = distinct_on(union)
Member.includes(:user).from([Arel.sql("(#{sql}) AS #{Member.table_name}")]) # rubocop: disable CodeReuse/ActiveRecord # enumerate the columns here since we are enumerating them in the union and want to be immune to
# column caching issues when adding/removing columns
Member.select(*Member.column_names)
.includes(:user).from([Arel.sql("(#{sql}) AS #{Member.table_name}")]) # rubocop: disable CodeReuse/ActiveRecord
end end
def distinct_on(union) def distinct_on(union)
......
...@@ -28,6 +28,8 @@ class GroupMember < Member ...@@ -28,6 +28,8 @@ class GroupMember < Member
attr_accessor :last_owner, :last_blocked_owner attr_accessor :last_owner, :last_blocked_owner
self.enumerate_columns_in_select_statements = true
def self.access_level_roles def self.access_level_roles
Gitlab::Access.options_with_owner Gitlab::Access.options_with_owner
end end
......
...@@ -77,7 +77,7 @@ module JiraImport ...@@ -77,7 +77,7 @@ module JiraImport
end end
def project_member_ids def project_member_ids
@project_member_ids ||= MembersFinder.new(project, current_user).execute.select(:user_id) @project_member_ids ||= MembersFinder.new(project, current_user).execute.reselect(:user_id)
end end
end end
end end
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
# statement cache. If a different migration is then run and one of these columns is # statement cache. If a different migration is then run and one of these columns is
# removed in the meantime, the query is invalid. # removed in the meantime, the query is invalid.
ActiveRecord::Base.class_eval do
class_attribute :enumerate_columns_in_select_statements
end
module ActiveRecord module ActiveRecord
module QueryMethods module QueryMethods
private private
...@@ -16,6 +20,8 @@ module ActiveRecord ...@@ -16,6 +20,8 @@ module ActiveRecord
def build_select(arel) def build_select(arel)
if select_values.any? if select_values.any?
arel.project(*arel_columns(select_values.uniq)) arel.project(*arel_columns(select_values.uniq))
elsif klass.enumerate_columns_in_select_statements
arel.project(*klass.column_names.map { |field| table[field] })
else else
arel.project(@klass.arel_table[Arel.star]) arel.project(@klass.arel_table[Arel.star])
end end
......
...@@ -89,8 +89,8 @@ module EE ...@@ -89,8 +89,8 @@ module EE
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def find_user(board) def find_user(board)
user_ids = user_finder(board).execute.select(:user_id) user_ids = user_finder(board).execute.reselect(:user_id)
::User.where(id: user_ids).find_by(id: params['assignee_id']) ::User.id_in(user_ids).find_by(id: params['assignee_id'])
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -19,6 +19,7 @@ module EE ...@@ -19,6 +19,7 @@ module EE
include FromUnion include FromUnion
self.inheritance_column = :_type_disabled self.inheritance_column = :_type_disabled
self.enumerate_columns_in_select_statements = true
end end
class GroupGroupLink < ActiveRecord::Base class GroupGroupLink < ActiveRecord::Base
......
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