Sha256: f7093c7c295777631978ecb6c5ab876d996882ecce12136d7f2b49b7a200a4cd

Contents?: true

Size: 1.13 KB

Versions: 13

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require "active_record/connection_adapters/sqlserver_adapter"

module SqlIgnoredCache
  extend ActiveSupport::Concern

  IGNORED_SQL = [
    /INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS|KEY_COLUMN_USAGE)/im,
    /sys.columns/i,
    /SELECT @@version/,
    /SELECT @@TRANCOUNT/,
    /(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION/,
    /SELECT CAST\(.* AS .*\) AS value/,
    /SELECT DATABASEPROPERTYEX/im
  ]

  # We don't want to coerce every ActiveRecord test that relies on `query_cache`
  # just because we do more queries than the other adapters.
  #
  # Removing internal queries from the cache will make AR tests pass without
  # compromising cache outside tests.
  def cache_sql(sql, name, binds)
    result = super
    
    @query_cache.delete_if do |cache_key, _v|
      # Query cache key generated by `sql` or `[sql, binds]`, so need to retrieve `sql` for both cases.
      cache_key_sql = Array(cache_key).first
      Regexp.union(IGNORED_SQL).match?(cache_key_sql)
    end

    result
  end
end

ActiveSupport.on_load(:active_record) do
  ActiveRecord::ConnectionAdapters::SQLServerAdapter.prepend(SqlIgnoredCache)
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
activerecord-sqlserver-adapter-7.1.9 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.8 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.7 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.6 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.5 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.4 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.3 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.2 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.1 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.0 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.0.rc2 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.0.rc1 test/support/core_ext/query_cache.rb
activerecord-sqlserver-adapter-7.1.0.beta1 test/support/core_ext/query_cache.rb