Skip to content

Commit 37e1925

Browse files
committed
Add back cache for internal queries
1 parent 78ac7a1 commit 37e1925

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

+8-10
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,13 @@ def column_definitions(table_name)
415415
ci[:default_function] = begin
416416
default = ci[:default_value]
417417
if default.nil? && view_exists
418-
default = uncached do
419-
select_value %{
420-
SELECT c.COLUMN_DEFAULT
421-
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
422-
WHERE
423-
c.TABLE_NAME = '#{view_tblnm}'
424-
AND c.COLUMN_NAME = '#{views_real_column_name(table_name, ci[:name])}'
425-
}.squish, 'SCHEMA'
426-
end
418+
default = select_value %{
419+
SELECT c.COLUMN_DEFAULT
420+
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
421+
WHERE
422+
c.TABLE_NAME = '#{view_tblnm}'
423+
AND c.COLUMN_NAME = '#{views_real_column_name(table_name, ci[:name])}'
424+
}.squish, 'SCHEMA'
427425
end
428426
case default
429427
when nil
@@ -442,7 +440,7 @@ def column_definitions(table_name)
442440
else ci[:type]
443441
end
444442
value = default.match(/\A\((.*)\)\Z/m)[1]
445-
value = uncached { select_value("SELECT CAST(#{value} AS #{type}) AS value3", 'SCHEMA') }
443+
value = select_value("SELECT CAST(#{value} AS #{type}) AS value", 'SCHEMA')
446444
[value, nil]
447445
end
448446
end

test/cases/helper_sqlserver.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'bundler/setup'
33
Bundler.require :default, :development
44
require 'pry'
5+
require 'support/core_ext/query_cache'
56
require 'support/minitest_sqlserver'
67
require 'support/test_in_memory_oltp'
78
require 'cases/helper'

test/support/core_ext/query_cache.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'active_record/connection_adapters/sqlserver_adapter'
2+
3+
module SqlIgnoredCache
4+
extend ActiveSupport::Concern
5+
6+
IGNORED_SQL = [
7+
/INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS|KEY_COLUMN_USAGE)/im,
8+
/SELECT @@version/,
9+
/SELECT @@TRANCOUNT/,
10+
/(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION/,
11+
/SELECT CAST\(.* AS .*\) AS value/,
12+
/SELECT DATABASEPROPERTYEX/im
13+
]
14+
15+
# We don't want to coerce every ActiveRecord test that relies on `query_cache`
16+
# just because we do more queries than the other adapters.
17+
#
18+
# Removing internal queries from the cache will make AR tests pass without
19+
# compromising cache outside tests.
20+
def cache_sql(sql, name, binds)
21+
result = super
22+
@query_cache.delete_if { |k, v| k =~ Regexp.union(IGNORED_SQL) }
23+
result
24+
end
25+
end
26+
27+
ActiveSupport.on_load(:active_record) do
28+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.prepend(SqlIgnoredCache)
29+
end

0 commit comments

Comments
 (0)