<commit_before>module ActiveRecord::Turntable
  module ActiveRecordExt
    module QueryCache
      def self.prepended(klass)
        class << klass
          if Util.ar_version_equals_or_later?("5.1.0")
            prepend(ClassMethods::AR5_1)
          elsif Util.ar_version_equals_or_later?("5.0.1")
            prepend(ClassMethods::AR5_0_1)
          else
            prepend(ClassMethods::AR5_0)
          end
        end
      end

      module ClassMethods
        module AR5_1
          def run
            result = super

            pools = ActiveRecord::Base.turntable_connections.values
            pools.each do |pool|
              pool.enable_query_cache!
            end

            [*result, pools]
          end

          def complete(state)
            caching_pool, caching_was_enabled, turntable_pools = state
            super([caching_pool, caching_was_enabled])

            turntable_pools.each do |pool|
              pool.disable_query_cache! unless caching_was_enabled
            end
          end
        end

        module AR5_0_1
          def run
            result = super

            pools = ActiveRecord::Base.turntable_connections.values
            pools.each do |pool|
              pool.enable_query_cache!
            end

            [*result, pools]
          end

          def complete(state)
            caching_pool, caching_was_enabled, connection_id, turntable_pools = state
            super([caching_pool, caching_was_enabled, connection_id])

            turntable_pools.each do |pool|
              pool.disable_query_cache! unless caching_was_enabled
            end
          end
        end

        module AR5_0
          def run
            result = super

            pools = ActiveRecord::Base.turntable_connections.values
            pools.each do |k|
              k.connection.enable_query_cache!
            end

            result
          end

          def complete(state)
            enabled, _connection_id = state
            super

            klasses = ActiveRecord::Base.turntable_connections.values
            klasses.each do |k|
              k.connection.clear_query_cache
              k.connection.disable_query_cache! unless enabled
            end
          end
        end
      end

      def self.install_turntable_executor_hooks(executor = ActiveSupport::Executor)
        return if Util.ar_version_equals_or_later?("5.0.1")

        executor.to_complete do
          klasses = ActiveRecord::Base.turntable_connection_classes
          klasses.each do |k|<commit_msg>Make QueryCache extension to use Compatibility module on loading patches<commit_after>module ActiveRecord::Turntable
  module ActiveRecordExt
    module QueryCache
      def self.prepended(klass)
        class << klass
          prepend ClassMethods.compatible_module
        end
      end

      module ClassMethods
        extend Compatibility

        module V5_1
          def run
            result = super

            pools = ActiveRecord::Base.turntable_connections.values
            pools.each do |pool|
              pool.enable_query_cache!
            end

            [*result, pools]
          end

          def complete(state)
            caching_pool, caching_was_enabled, turntable_pools = state
            super([caching_pool, caching_was_enabled])

            turntable_pools.each do |pool|
              pool.disable_query_cache! unless caching_was_enabled
            end
          end
        end

        module V5_0_1
          def run
            result = super

            pools = ActiveRecord::Base.turntable_connections.values
            pools.each do |pool|
              pool.enable_query_cache!
            end

            [*result, pools]
          end

          def complete(state)
            caching_pool, caching_was_enabled, connection_id, turntable_pools = state
            super([caching_pool, caching_was_enabled, connection_id])

            turntable_pools.each do |pool|
              pool.disable_query_cache! unless caching_was_enabled
            end
          end
        end

        module V5_0
          def run
            result = super

            pools = ActiveRecord::Base.turntable_connections.values
            pools.each do |k|
              k.connection.enable_query_cache!
            end

            result
          end

          def complete(state)
            enabled, _connection_id = state
            super

            klasses = ActiveRecord::Base.turntable_connections.values
            klasses.each do |k|
              k.connection.clear_query_cache
              k.connection.disable_query_cache! unless enabled
            end
          end
        end
      end

      def self.install_turntable_executor_hooks(executor = ActiveSupport::Executor)
        return if Util.ar_version_equals_or_later?("5.0.1")

        executor.to_complete do
          klasses = ActiveRecord::Base.turntable_connection_classes
          klasses.each do |k|
{'fraction_non_alphanumeric': 0.055198715375351266, 'fraction_numerical': 0.0066238458450421514, 'mean_word_length': 1.643501326259947, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6539236', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1281, 'n_words': 279}