Skip to content

Commit a97115e

Browse files
authored
Merge pull request #1 from aidanharan/raise-connection-not-established-refactor
Refactor 'Raise ActiveRecord::ConnectionNotEstablished on calls to execute with a disconnected connection'
2 parents e4eb203 + 9f18dd9 commit a97115e

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ def execute_procedure(proc_name, *variables)
167167
log(sql, name) do
168168
case @connection_options[:mode]
169169
when :dblib
170-
raise ActiveRecord::ConnectionNotEstablished if @connection.nil?
171-
172-
result = @connection.execute(sql)
170+
result = ensure_established_connection! { @connection.execute(sql) }
173171
options = { as: :hash, cache_rows: true, timezone: ActiveRecord::Base.default_timezone || :utc }
174172
result.each(options) do |row|
175173
r = row.with_indifferent_access
@@ -359,9 +357,7 @@ def sp_executesql_sql(sql, types, params, name)
359357
def raw_connection_do(sql)
360358
case @connection_options[:mode]
361359
when :dblib
362-
raise ActiveRecord::ConnectionNotEstablished if @connection.nil?
363-
364-
result = @connection.execute(sql)
360+
result = ensure_established_connection! { @connection.execute(sql) }
365361

366362
# TinyTDS returns false instead of raising an exception if connection fails.
367363
# Getting around this by raising an exception ourselves while this PR
@@ -432,9 +428,7 @@ def _raw_select(sql, options = {})
432428
def raw_connection_run(sql)
433429
case @connection_options[:mode]
434430
when :dblib
435-
raise ActiveRecord::ConnectionNotEstablished if @connection.nil?
436-
437-
@connection.execute(sql)
431+
ensure_established_connection! { @connection.execute(sql) }
438432
end
439433
end
440434

@@ -468,6 +462,12 @@ def finish_statement_handle(handle)
468462
end
469463
handle
470464
end
465+
466+
def ensure_established_connection!
467+
raise ActiveRecord::ConnectionNotEstablished, 'SQL Server client is not connected' unless @connection
468+
469+
yield
470+
end
471471
end
472472
end
473473
end

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def initialize_type_map(m = type_map)
374374
end
375375

376376
def translate_exception(e, message:, sql:, binds:)
377-
return ActiveRecord::ConnectionNotEstablished.new("SQLServer client is not connected") if e.is_a?(ActiveRecord::ConnectionNotEstablished)
377+
return e if e.is_a?(ActiveRecord::ConnectionNotEstablished)
378378

379379
case message
380380
when /(cannot insert duplicate key .* with unique index) | (violation of unique key constraint)/i

test/cases/disconnected_test_sqlserver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def setup
1818
test "can't execute procuderes while disconnected" do
1919
@connection.execute_procedure :sp_tables, "sst_datatypes"
2020
@connection.disconnect!
21-
assert_raises(ActiveRecord::ConnectionNotEstablished) do
21+
assert_raises(ActiveRecord::ConnectionNotEstablished, 'SQL Server client is not connected') do
2222
@connection.execute_procedure :sp_tables, "sst_datatypes"
2323
end
2424
end
@@ -32,7 +32,7 @@ def setup
3232

3333
@connection.exec_query sql, "TEST", binds
3434
@connection.disconnect!
35-
assert_raises(ActiveRecord::ConnectionNotEstablished) do
35+
assert_raises(ActiveRecord::ConnectionNotEstablished, 'SQL Server client is not connected') do
3636
@connection.exec_query sql, "TEST", binds
3737
end
3838
end

0 commit comments

Comments
 (0)