Skip to content

Commit fdb7cb0

Browse files
committed
Fix SQL Azure conflicts with DBCC useroptions. Use new #user_options_xyz methods. [kazamachi]
1 parent fc20fdc commit fdb7cb0

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
* 3.1.2 *
33

4+
* Fix SQL Azure conflicts with DBCC useroptions. Use new #user_options_xyz methods. [kazamachi]
5+
46
* Fix identity inserts for tables with natural PKs. [Gian Carlo Pace]
57

68
* Create a #configure_connection method that can be overridden. Think "SET TEXTSIZE...".

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def use_database(database=nil)
120120
end
121121

122122
def user_options
123+
return {} if sqlserver_azure?
123124
info_schema_query do
124125
select_rows("dbcc useroptions").inject(HashWithIndifferentAccess.new) do |values,row|
125126
set_option = row[0].gsub(/\s+/,'_')
@@ -129,10 +130,45 @@ def user_options
129130
end
130131
end
131132
end
133+
134+
def user_options_dateformat
135+
if sqlserver_azure?
136+
info_schema_query { select_value "SELECT [dateformat] FROM [sys].[syslanguages] WHERE [langid] = @@LANGID" }
137+
else
138+
user_options['dateformat']
139+
end
140+
end
141+
142+
def user_options_isolation_level
143+
if sqlserver_azure?
144+
info_schema_query do
145+
sql = %|SELECT CASE [transaction_isolation_level]
146+
WHEN 0 THEN NULL
147+
WHEN 1 THEN 'READ UNCOMITTED'
148+
WHEN 2 THEN 'READ COMITTED'
149+
WHEN 3 THEN 'REPEATABLE'
150+
WHEN 4 THEN 'SERIALIZABLE'
151+
WHEN 5 THEN 'SNAPSHOT' END AS [isolation_level]
152+
FROM [sys].[dm_exec_sessions]
153+
WHERE [session_id] = @@SPID|.squish
154+
select_value(sql)
155+
end
156+
else
157+
user_options['isolation_level']
158+
end
159+
end
160+
161+
def user_options_language
162+
if sqlserver_azure?
163+
info_schema_query { select_value "SELECT @@LANGUAGE AS [language]" }
164+
else
165+
user_options['language']
166+
end
167+
end
132168

133169
def run_with_isolation_level(isolation_level)
134170
raise ArgumentError, "Invalid isolation level, #{isolation_level}. Supported levels include #{valid_isolation_levels.to_sentence}." if !valid_isolation_levels.include?(isolation_level.upcase)
135-
initial_isolation_level = user_options[:isolation_level] || "READ COMMITTED"
171+
initial_isolation_level = user_options_isolation_level || "READ COMMITTED"
136172
do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}"
137173
begin
138174
yield

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def configure_application_name
446446
end
447447

448448
def initialize_dateformatter
449-
@database_dateformat = user_options['dateformat']
449+
@database_dateformat = user_options_dateformat
450450
a, b, c = @database_dateformat.each_char.to_a
451451
[a,b,c].each { |f| f.upcase! if f == 'y' }
452452
dateformat = "%#{a}-%#{b}-%#{c}"

test/cases/adapter_test_sqlserver.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def setup
140140
context 'with different language' do
141141

142142
setup do
143-
@default_language = @connection.user_options['language']
143+
@default_language = @connection.user_options_language
144144
end
145145

146146
teardown do
@@ -413,14 +413,14 @@ def setup
413413
@t2 = tasks(:another_task)
414414
assert @t1, 'Tasks :first_task should be in AR fixtures'
415415
assert @t2, 'Tasks :another_task should be in AR fixtures'
416-
good_isolation_level = @connection.user_options[:isolation_level].blank? || @connection.user_options[:isolation_level] =~ /read committed/i
417-
assert good_isolation_level, "User isolation level is not at a happy starting place: #{@connection.user_options[:isolation_level].inspect}"
416+
good_isolation_level = @connection.user_options_isolation_level.blank? || @connection.user_options_isolation_level =~ /read committed/i
417+
assert good_isolation_level, "User isolation level is not at a happy starting place: #{@connection.user_options_isolation_level.inspect}"
418418
end
419419

420420
should 'allow #run_with_isolation_level to not take a block to set it' do
421421
begin
422422
@connection.run_with_isolation_level 'READ UNCOMMITTED'
423-
assert_match %r|read uncommitted|i, @connection.user_options[:isolation_level]
423+
assert_match %r|read uncommitted|i, @connection.user_options_isolation_level
424424
ensure
425425
@connection.run_with_isolation_level 'READ COMMITTED'
426426
end

0 commit comments

Comments
 (0)