diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /lib/gitlab/sql | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'lib/gitlab/sql')
-rw-r--r-- | lib/gitlab/sql/glob.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/sql/set_operator.rb | 31 |
2 files changed, 31 insertions, 2 deletions
diff --git a/lib/gitlab/sql/glob.rb b/lib/gitlab/sql/glob.rb index f3421bd95d2..adc6ccbd1b9 100644 --- a/lib/gitlab/sql/glob.rb +++ b/lib/gitlab/sql/glob.rb @@ -17,7 +17,7 @@ module Gitlab end def q(string) - ActiveRecord::Base.connection.quote(string) + ApplicationRecord.connection.quote(string) end end end diff --git a/lib/gitlab/sql/set_operator.rb b/lib/gitlab/sql/set_operator.rb index 59a808eafa9..18275da3ef0 100644 --- a/lib/gitlab/sql/set_operator.rb +++ b/lib/gitlab/sql/set_operator.rb @@ -19,6 +19,7 @@ module Gitlab # Project.where("id IN (#{sql})") class SetOperator def initialize(relations, remove_duplicates: true, remove_order: true) + verify_select_values!(relations) if Rails.env.test? || Rails.env.development? @relations = relations @remove_duplicates = remove_duplicates @remove_order = remove_order @@ -33,7 +34,7 @@ module Gitlab # aren't incremented properly when joining relations together this way. # By using "unprepared_statements" we remove the usage of placeholders # (thus fixing this problem), at a slight performance cost. - fragments = ActiveRecord::Base.connection.unprepared_statement do + fragments = ApplicationRecord.connection.unprepared_statement do relations.map do |rel| remove_order ? rel.reorder(nil).to_sql : rel.to_sql end.reject(&:blank?) @@ -54,6 +55,34 @@ module Gitlab private attr_reader :relations, :remove_duplicates, :remove_order + + def verify_select_values!(relations) + all_select_values = relations.map do |relation| + if relation.respond_to?(:select_values) + relation.select_values + else + relation.projections # Handle Arel based subqueries + end + end + + unless all_select_values.map(&:size).uniq.one? + relation_select_sizes = all_select_values.map.with_index do |select_values, index| + if select_values.empty? + "Relation ##{index}: *, all columns" + else + "Relation ##{index}: #{select_values.size} select values" + end + end + + exception_text = <<~EOF + Relations with uneven select values were passed. The UNION query could break when the underlying table changes (add or remove columns). + + #{relation_select_sizes.join("\n")} + EOF + + raise(exception_text) + end + end end end end |