Commit 54fa87a4 authored by Igor Drozdov's avatar Igor Drozdov

Fix scoped relation inheritance

Class level methods no longer inherit scoping in Rails 6.1

Let's fix the warnings now to have the correct behavior
when we migrate to 6.1
parent ece2b378
...@@ -908,7 +908,7 @@ module Ci ...@@ -908,7 +908,7 @@ module Ci
def same_family_pipeline_ids def same_family_pipeline_ids
::Gitlab::Ci::PipelineObjectHierarchy.new( ::Gitlab::Ci::PipelineObjectHierarchy.new(
self.class.where(id: root_ancestor), options: { same_project: true } self.class.default_scoped.where(id: root_ancestor), options: { same_project: true }
).base_and_descendants.select(:id) ).base_and_descendants.select(:id)
end end
......
...@@ -214,9 +214,9 @@ module AtomicInternalId ...@@ -214,9 +214,9 @@ module AtomicInternalId
def self.project_init(klass, column_name = :iid) def self.project_init(klass, column_name = :iid)
->(instance, scope) do ->(instance, scope) do
if instance if instance
klass.where(project_id: instance.project_id).maximum(column_name) klass.default_scoped.where(project_id: instance.project_id).maximum(column_name)
elsif scope.present? elsif scope.present?
klass.where(**scope).maximum(column_name) klass.default_scoped.where(**scope).maximum(column_name)
end end
end end
end end
......
...@@ -140,7 +140,8 @@ class Member < ApplicationRecord ...@@ -140,7 +140,8 @@ class Member < ApplicationRecord
scope :distinct_on_user_with_max_access_level, -> do scope :distinct_on_user_with_max_access_level, -> do
distinct_members = select('DISTINCT ON (user_id, invite_email) *') distinct_members = select('DISTINCT ON (user_id, invite_email) *')
.order('user_id, invite_email, access_level DESC, expires_at DESC, created_at ASC') .order('user_id, invite_email, access_level DESC, expires_at DESC, created_at ASC')
Member.from(distinct_members, :members)
from(distinct_members, :members)
end end
scope :order_name_asc, -> { left_join_users.reorder(Gitlab::Database.nulls_last_order('users.name', 'ASC')) } scope :order_name_asc, -> { left_join_users.reorder(Gitlab::Database.nulls_last_order('users.name', 'ASC')) }
......
...@@ -2528,7 +2528,7 @@ class Project < ApplicationRecord ...@@ -2528,7 +2528,7 @@ class Project < ApplicationRecord
namespace.root_ancestor.all_projects namespace.root_ancestor.all_projects
.joins(:packages) .joins(:packages)
.where.not(id: id) .where.not(id: id)
.merge(Packages::Package.with_name(package_name)) .merge(Packages::Package.default_scoped.with_name(package_name))
.exists? .exists?
end end
......
...@@ -29,7 +29,7 @@ class BackfillVersionAuthorAndCreatedAt < ActiveRecord::Migration[5.2] ...@@ -29,7 +29,7 @@ class BackfillVersionAuthorAndCreatedAt < ActiveRecord::Migration[5.2]
issues = Issue.arel_table issues = Issue.arel_table
projects = Project.arel_table projects = Project.arel_table
Version.select(versions[:issue_id]).where( select(versions[:issue_id]).where(
versions[:author_id].eq(nil).or( versions[:author_id].eq(nil).or(
versions[:created_at].eq(nil) versions[:created_at].eq(nil)
).and( ).and(
......
...@@ -22,8 +22,7 @@ module EE ...@@ -22,8 +22,7 @@ module EE
.join(later_deployments, Arel::Nodes::OuterJoin) .join(later_deployments, Arel::Nodes::OuterJoin)
.on(join_conditions) .on(join_conditions)
model joins(:successful_deployments)
.joins(:successful_deployments)
.joins(join.join_sources) .joins(join.join_sources)
.where(later_deployments[:id].eq(nil)) .where(later_deployments[:id].eq(nil))
.where(deployments[:cluster_id].eq(cluster.id)) .where(deployments[:cluster_id].eq(cluster.id))
......
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,7 @@ module Gitlab
class ObjectHierarchy class ObjectHierarchy
DEPTH_COLUMN = :depth DEPTH_COLUMN = :depth
attr_reader :ancestors_base, :descendants_base, :model, :options attr_reader :ancestors_base, :descendants_base, :model, :options, :unscoped_model
# ancestors_base - An instance of ActiveRecord::Relation for which to # ancestors_base - An instance of ActiveRecord::Relation for which to
# get parent objects. # get parent objects.
...@@ -19,6 +19,7 @@ module Gitlab ...@@ -19,6 +19,7 @@ module Gitlab
@ancestors_base = ancestors_base @ancestors_base = ancestors_base
@descendants_base = descendants_base @descendants_base = descendants_base
@model = ancestors_base.model @model = ancestors_base.model
@unscoped_model = @model.unscoped
@options = options @options = options
end end
...@@ -70,23 +71,23 @@ module Gitlab ...@@ -70,23 +71,23 @@ module Gitlab
# if hierarchy_order is given, the calculated `depth` should be present in SELECT # if hierarchy_order is given, the calculated `depth` should be present in SELECT
if expose_depth if expose_depth
recursive_query = base_and_ancestors_cte(upto, hierarchy_order).apply_to(model.all).distinct recursive_query = base_and_ancestors_cte(upto, hierarchy_order).apply_to(unscoped_model.all).distinct
read_only(model.from(Arel::Nodes::As.new(recursive_query.arel, objects_table)).order(depth: hierarchy_order)) read_only(unscoped_model.from(Arel::Nodes::As.new(recursive_query.arel, objects_table)).order(depth: hierarchy_order))
else else
recursive_query = base_and_ancestors_cte(upto).apply_to(model.all) recursive_query = base_and_ancestors_cte(upto).apply_to(unscoped_model.all)
if skip_ordering? if skip_ordering?
recursive_query = recursive_query.distinct recursive_query = recursive_query.distinct
else else
recursive_query = recursive_query.reselect(*recursive_query.arel.projections, 'ROW_NUMBER() OVER () as depth').distinct recursive_query = recursive_query.reselect(*recursive_query.arel.projections, 'ROW_NUMBER() OVER () as depth').distinct
recursive_query = model.from(Arel::Nodes::As.new(recursive_query.arel, objects_table)) recursive_query = unscoped_model.from(Arel::Nodes::As.new(recursive_query.arel, objects_table))
recursive_query = remove_depth_and_maintain_order(recursive_query, hierarchy_order: hierarchy_order) recursive_query = remove_depth_and_maintain_order(recursive_query, hierarchy_order: hierarchy_order)
end end
read_only(recursive_query) read_only(recursive_query)
end end
else else
recursive_query = base_and_ancestors_cte(upto, hierarchy_order).apply_to(model.all) recursive_query = base_and_ancestors_cte(upto, hierarchy_order).apply_to(unscoped_model.all)
recursive_query = recursive_query.order(depth: hierarchy_order) if hierarchy_order recursive_query = recursive_query.order(depth: hierarchy_order) if hierarchy_order
read_only(recursive_query) read_only(recursive_query)
end end
...@@ -103,23 +104,23 @@ module Gitlab ...@@ -103,23 +104,23 @@ module Gitlab
if use_distinct? if use_distinct?
# Always calculate `depth`, remove it later if with_depth is false # Always calculate `depth`, remove it later if with_depth is false
if with_depth if with_depth
base_cte = base_and_descendants_cte(with_depth: true).apply_to(model.all).distinct base_cte = base_and_descendants_cte(with_depth: true).apply_to(unscoped_model.all).distinct
read_only(model.from(Arel::Nodes::As.new(base_cte.arel, objects_table)).order(depth: :asc)) read_only(unscoped_model.from(Arel::Nodes::As.new(base_cte.arel, objects_table)).order(depth: :asc))
else else
base_cte = base_and_descendants_cte.apply_to(model.all) base_cte = base_and_descendants_cte.apply_to(unscoped_model.all)
if skip_ordering? if skip_ordering?
base_cte = base_cte.distinct base_cte = base_cte.distinct
else else
base_cte = base_cte.reselect(*base_cte.arel.projections, 'ROW_NUMBER() OVER () as depth').distinct base_cte = base_cte.reselect(*base_cte.arel.projections, 'ROW_NUMBER() OVER () as depth').distinct
base_cte = model.from(Arel::Nodes::As.new(base_cte.arel, objects_table)) base_cte = unscoped_model.from(Arel::Nodes::As.new(base_cte.arel, objects_table))
base_cte = remove_depth_and_maintain_order(base_cte, hierarchy_order: :asc) base_cte = remove_depth_and_maintain_order(base_cte, hierarchy_order: :asc)
end end
read_only(base_cte) read_only(base_cte)
end end
else else
read_only(base_and_descendants_cte(with_depth: with_depth).apply_to(model.all)) read_only(base_and_descendants_cte(with_depth: with_depth).apply_to(unscoped_model.all))
end end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -154,16 +155,15 @@ module Gitlab ...@@ -154,16 +155,15 @@ module Gitlab
ancestors_table = ancestors.alias_to(objects_table) ancestors_table = ancestors.alias_to(objects_table)
descendants_table = descendants.alias_to(objects_table) descendants_table = descendants.alias_to(objects_table)
ancestors_scope = model.unscoped.from(ancestors_table) ancestors_scope = unscoped_model.from(ancestors_table)
descendants_scope = model.unscoped.from(descendants_table) descendants_scope = unscoped_model.from(descendants_table)
if use_distinct? if use_distinct?
ancestors_scope = ancestors_scope.distinct ancestors_scope = ancestors_scope.distinct
descendants_scope = descendants_scope.distinct descendants_scope = descendants_scope.distinct
end end
relation = model relation = unscoped_model
.unscoped
.with .with
.recursive(ancestors.to_arel, descendants.to_arel) .recursive(ancestors.to_arel, descendants.to_arel)
.from_union([ .from_union([
...@@ -215,7 +215,7 @@ module Gitlab ...@@ -215,7 +215,7 @@ module Gitlab
cte << base_query cte << base_query
# Recursively get all the ancestors of the base set. # Recursively get all the ancestors of the base set.
parent_query = model parent_query = unscoped_model
.from(from_tables(cte)) .from(from_tables(cte))
.where(ancestor_conditions(cte)) .where(ancestor_conditions(cte))
.except(:order) .except(:order)
...@@ -248,7 +248,7 @@ module Gitlab ...@@ -248,7 +248,7 @@ module Gitlab
cte << base_query cte << base_query
# Recursively get all the descendants of the base set. # Recursively get all the descendants of the base set.
descendants_query = model descendants_query = unscoped_model
.from(from_tables(cte)) .from(from_tables(cte))
.where(descendant_conditions(cte)) .where(descendant_conditions(cte))
.except(:order) .except(:order)
......
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