diff options
Diffstat (limited to 'gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning')
5 files changed, 131 insertions, 0 deletions
diff --git a/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/associations/builder/association.rb b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/associations/builder/association.rb new file mode 100644 index 00000000000..3c92ba91c31 --- /dev/null +++ b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/associations/builder/association.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module ActiveRecord + module GitlabPatches + module Partitioning + module Associations + module Builder + module Association + extend ActiveSupport::Concern + + class_methods do + def valid_options(options) + super + [:partition_foreign_key] + end + end + end + end + end + end + end +end diff --git a/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/base.rb b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/base.rb new file mode 100644 index 00000000000..0c8a248b984 --- /dev/null +++ b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/base.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +if ::ActiveRecord::VERSION::STRING >= "7.1" + raise 'New version of active-record detected, please remove or update this patch' +end + +module ActiveRecord + module GitlabPatches + module Partitioning + module Base + extend ActiveSupport::Concern + + def _query_constraints_hash + constraints_hash = super + + return constraints_hash unless self.class.use_partition_id_filter? + + if self.class.query_constraints_list.nil? + { @primary_key => id_in_database } # rubocop:disable Gitlab/ModuleWithInstanceVariables + else + self.class.query_constraints_list.index_with do |column_name| + attribute_in_database(column_name) + end + end + end + + class_methods do + def use_partition_id_filter? + false + end + + def query_constraints(*columns_list) + raise ArgumentError, "You must specify at least one column to be used in querying" if columns_list.empty? + + @query_constraints_list = columns_list.map(&:to_s) + end + + def query_constraints_list # :nodoc: + @query_constraints_list ||= if base_class? || primary_key != base_class.primary_key + primary_key if primary_key.is_a?(Array) + else + base_class.query_constraints_list + end + end + end + end + end + end +end diff --git a/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/abstract_reflection.rb b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/abstract_reflection.rb new file mode 100644 index 00000000000..7532cd120a5 --- /dev/null +++ b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/abstract_reflection.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module ActiveRecord + module GitlabPatches + module Partitioning + module Reflection + module AbstractReflection + extend ActiveSupport::Concern + + def join_scope(table, foreign_table, foreign_klass) + klass_scope = super + return klass_scope unless respond_to?(:options) + + partition_foreign_key = options[:partition_foreign_key] + if partition_foreign_key && klass.use_partition_id_filter? + klass_scope.where!(table[:partition_id].eq(foreign_table[partition_foreign_key])) + end + + klass_scope + end + end + end + end + end +end diff --git a/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/association_reflection.rb b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/association_reflection.rb new file mode 100644 index 00000000000..299ceaab973 --- /dev/null +++ b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/association_reflection.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module ActiveRecord + module GitlabPatches + module Partitioning + module Reflection + module AssociationReflection + def check_eager_loadable! + return if scope && scope.arity == 1 && options.key?(:partition_foreign_key) + + super + end + end + end + end + end +end diff --git a/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/macro_reflection.rb b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/macro_reflection.rb new file mode 100644 index 00000000000..7ec7da44253 --- /dev/null +++ b/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/reflection/macro_reflection.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module ActiveRecord + module GitlabPatches + module Partitioning + module Reflection + module MacroReflection + def scope_for(relation, owner = nil) + if scope.arity == 1 && owner.nil? && options.key?(:partition_foreign_key) + relation + else + super + end + end + end + end + end + end +end |