diff --git a/app/models/learning_outcome_group.rb b/app/models/learning_outcome_group.rb index 64f4c5edd99..b829884a8a9 100644 --- a/app/models/learning_outcome_group.rb +++ b/app/models/learning_outcome_group.rb @@ -34,8 +34,6 @@ class LearningOutcomeGroup < ActiveRecord::Base belongs_to :context, polymorphic: [:account, :course] before_save :infer_defaults - after_create :clear_descendants_cache - after_update :clear_descendants_cache, if: -> { clear_descendants_cache? } resolves_root_account through: -> (group) { group.context_id ? group.context.resolved_root_account_id : 0 } validates :vendor_guid, length: { maximum: maximum_string_length, allow_nil: true } validates_length_of :description, :maximum => maximum_text_length, :allow_nil => true, :allow_blank => true @@ -288,14 +286,6 @@ class LearningOutcomeGroup < ActiveRecord::Base ancestor_ids.member?(id) end - def clear_descendants_cache - Outcomes::LearningOutcomeGroupChildren.new(context).clear_descendants_cache - end - - def clear_descendants_cache? - (previous_changes.keys & %w[learning_outcome_group_id workflow_state]).any? - end - private_class_method def self.title_order_by_clause(table = nil) col = table ? "#{table}.title" : "title" best_unicode_collation_key(col) diff --git a/lib/outcomes/learning_outcome_group_children.rb b/lib/outcomes/learning_outcome_group_children.rb index 7df1eb6b0ce..bf25467a7ec 100644 --- a/lib/outcomes/learning_outcome_group_children.rb +++ b/lib/outcomes/learning_outcome_group_children.rb @@ -73,7 +73,7 @@ module Outcomes end def suboutcomes_by_group_id(learning_outcome_group_id, args={}) - learning_outcome_groups_ids = children_ids(learning_outcome_group_id) << learning_outcome_group_id + learning_outcome_groups_ids = children_ids_with_self(learning_outcome_group_id) relation = ContentTag.active.learning_outcome_links .where(associated_asset_id: learning_outcome_groups_ids) .joins(:learning_outcome_content) @@ -91,13 +91,6 @@ module Outcomes end end - def clear_descendants_cache - return unless improved_outcomes_management? - - Rails.cache.delete(descendants_cache_key) - clear_total_outcomes_cache - end - def clear_total_outcomes_cache Rails.cache.delete(context_timestamp_cache_key) if improved_outcomes_management? end @@ -112,7 +105,7 @@ module Outcomes private def total_outcomes_for(learning_outcome_group_id, args={}) - learning_outcome_groups_ids = children_ids(learning_outcome_group_id) << learning_outcome_group_id + learning_outcome_groups_ids = children_ids_with_self(learning_outcome_group_id) relation = ContentTag.active.learning_outcome_links .where(associated_asset_id: learning_outcome_groups_ids) @@ -162,34 +155,21 @@ module Outcomes ) end - def children_ids(learning_outcome_group_id) - parent = data.find { |d| d['parent_id'] == learning_outcome_group_id } - parent&.dig('descendant_ids')&.tr('{}', '')&.split(',') || [] - end - - def data - if improved_outcomes_management? - Rails.cache.fetch(descendants_cache_key) do - LearningOutcomeGroup.connection.execute(learning_outcome_group_descendants_query).as_json - end - else - LearningOutcomeGroup.connection.execute(learning_outcome_group_descendants_query).as_json - end - end - - def learning_outcome_group_descendants_query - <<-SQL.squish + def children_ids_with_self(learning_outcome_group_id) + sql = <<-SQL.squish WITH RECURSIVE levels AS ( - SELECT id, learning_outcome_group_id AS parent_id - FROM (#{LearningOutcomeGroup.active.where(context: context).to_sql}) AS data + SELECT id, id AS parent_id + FROM (#{LearningOutcomeGroup.active.where(id: learning_outcome_group_id).to_sql}) AS data UNION ALL SELECT child.id AS id, parent.parent_id AS parent_id FROM #{LearningOutcomeGroup.quoted_table_name} child INNER JOIN levels parent ON parent.id = child.learning_outcome_group_id WHERE child.workflow_state <> 'deleted' ) - SELECT parent_id, array_agg(id) AS descendant_ids FROM levels WHERE parent_id IS NOT NULL GROUP BY parent_id + SELECT id FROM levels SQL + + LearningOutcomeGroup.connection.execute(sql).as_json.map {|r| r["id"]} end def context_timestamp_cache @@ -198,10 +178,6 @@ module Outcomes end end - def descendants_cache_key - ['learning_outcome_group_descendants', context_asset_string].cache_key - end - def total_outcomes_cache_key(learning_outcome_group_id = nil) ['learning_outcome_group_total_outcomes', context_asset_string, diff --git a/spec/lib/outcomes/learning_outcome_group_children_spec.rb b/spec/lib/outcomes/learning_outcome_group_children_spec.rb index b3f37386c8b..34e729e00d0 100644 --- a/spec/lib/outcomes/learning_outcome_group_children_spec.rb +++ b/spec/lib/outcomes/learning_outcome_group_children_spec.rb @@ -417,21 +417,6 @@ describe Outcomes::LearningOutcomeGroupChildren do end end - describe '#clear_descendants_cache' do - it 'clears the cache' do - enable_cache do - expect(LearningOutcomeGroup.connection).to receive(:execute).and_call_original.twice - expect(ContentTag).to receive(:active).and_call_original.exactly(4).times - expect(subject.total_outcomes(g0.id)).to eq 12 - expect(subject.total_outcomes(g1.id)).to eq 9 - subject.clear_descendants_cache - instance = described_class.new(context) - expect(instance.total_outcomes(g0.id)).to eq 12 - expect(instance.total_outcomes(g1.id)).to eq 9 - end - end - end - describe '#clear_total_outcomes_cache' do it 'clears the cache' do enable_cache do