Skip to content

Commit 6b2689d

Browse files
committed
Eliminate missed lease_connection calls
1 parent 204bc98 commit 6b2689d

File tree

2 files changed

+60
-49
lines changed

2 files changed

+60
-49
lines changed

lib/active_record/tasks/sqlserver_database_tasks.rb

+38-32
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Tasks
1010
class SQLServerDatabaseTasks
1111
DEFAULT_COLLATION = "SQL_Latin1_General_CP1_CI_AS"
1212

13-
delegate :lease_connection, :establish_connection, to: ActiveRecord::Base
13+
delegate :establish_connection, to: ActiveRecord::Base
1414

1515
def self.using_database_configurations?
1616
true
@@ -23,8 +23,10 @@ def initialize(configuration)
2323

2424
def create(master_established = false)
2525
establish_master_connection unless master_established
26-
lease_connection.create_database configuration.database, configuration_hash.merge(collation: default_collation)
27-
establish_connection configuration
26+
with_connection do |connection|
27+
connection.create_database(configuration.database, configuration_hash.merge(collation: default_collation))
28+
end
29+
establish_connection(configuration)
2830
rescue ActiveRecord::StatementInvalid => e
2931
if /database .* already exists/i === e.message
3032
raise DatabaseAlreadyExists
@@ -35,15 +37,15 @@ def create(master_established = false)
3537

3638
def drop
3739
establish_master_connection
38-
lease_connection.drop_database configuration.database
40+
with_connection { |connection| connection.drop_database(configuration.database) }
3941
end
4042

4143
def charset
42-
lease_connection.charset
44+
with_connection { |connection| connection.charset }
4345
end
4446

4547
def collation
46-
lease_connection.collation
48+
with_connection { |connection| connection.collation }
4749
end
4850

4951
def purge
@@ -56,34 +58,38 @@ def clear_active_connections!
5658
ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
5759
end
5860

59-
def structure_dump(filename, extra_flags)
60-
server_arg = "-S #{Shellwords.escape(configuration_hash[:host])}"
61-
server_arg += ":#{Shellwords.escape(configuration_hash[:port])}" if configuration_hash[:port]
62-
command = [
63-
"defncopy-ttds",
64-
server_arg,
65-
"-D #{Shellwords.escape(configuration_hash[:database])}",
66-
"-U #{Shellwords.escape(configuration_hash[:username])}",
67-
"-P #{Shellwords.escape(configuration_hash[:password])}",
68-
"-o #{Shellwords.escape(filename)}",
69-
]
70-
table_args = lease_connection.tables.map { |t| Shellwords.escape(t) }
71-
command.concat(table_args)
72-
view_args = lease_connection.views.map { |v| Shellwords.escape(v) }
73-
command.concat(view_args)
74-
raise "Error dumping database" unless Kernel.system(command.join(" "))
75-
76-
dump = File.read(filename)
77-
dump.gsub!(/^USE .*$\nGO\n/, "") # Strip db USE statements
78-
dump.gsub!(/^GO\n/, "") # Strip db GO statements
79-
dump.gsub!(/nvarchar\(8000\)/, "nvarchar(4000)") # Fix nvarchar(8000) column defs
80-
dump.gsub!(/nvarchar\(-1\)/, "nvarchar(max)") # Fix nvarchar(-1) column defs
81-
dump.gsub!(/text\(\d+\)/, "text") # Fix text(16) column defs
82-
File.open(filename, "w") { |file| file.puts dump }
61+
def structure_dump(filename, _extra_flags)
62+
with_connection do |connection|
63+
server_arg = "-S #{Shellwords.escape(configuration_hash[:host])}"
64+
server_arg += ":#{Shellwords.escape(configuration_hash[:port])}" if configuration_hash[:port]
65+
command = [
66+
"defncopy-ttds",
67+
server_arg,
68+
"-D #{Shellwords.escape(configuration_hash[:database])}",
69+
"-U #{Shellwords.escape(configuration_hash[:username])}",
70+
"-P #{Shellwords.escape(configuration_hash[:password])}",
71+
"-o #{Shellwords.escape(filename)}",
72+
]
73+
table_args = connection.tables.map { |t| Shellwords.escape(t) }
74+
command.concat(table_args)
75+
view_args = connection.views.map { |v| Shellwords.escape(v) }
76+
command.concat(view_args)
77+
raise "Error dumping database" unless Kernel.system(command.join(" "))
78+
79+
dump = File.read(filename)
80+
dump.gsub!(/^USE .*$\nGO\n/, "") # Strip db USE statements
81+
dump.gsub!(/^GO\n/, "") # Strip db GO statements
82+
dump.gsub!(/nvarchar\(8000\)/, "nvarchar(4000)") # Fix nvarchar(8000) column defs
83+
dump.gsub!(/nvarchar\(-1\)/, "nvarchar(max)") # Fix nvarchar(-1) column defs
84+
dump.gsub!(/text\(\d+\)/, "text") # Fix text(16) column defs
85+
File.open(filename, "w") { |file| file.puts dump }
86+
end
8387
end
8488

85-
def structure_load(filename, extra_flags)
86-
lease_connection.execute File.read(filename)
89+
def structure_load(filename, _extra_flags)
90+
with_connection do |connection|
91+
connection.execute File.read(filename)
92+
end
8793
end
8894

8995
private

lib/arel/visitors/sqlserver.rb

+22-17
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,24 @@ def visit_Arel_Table(o, collector)
127127
# Apparently, o.engine.connection can actually be a different adapter
128128
# than sqlserver. Can be removed if fixed in ActiveRecord. See:
129129
# github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/450
130-
table_name =
131-
begin
132-
if o.class.engine.lease_connection.respond_to?(:sqlserver?) && o.class.engine.lease_connection.database_prefix_remote_server?
133-
remote_server_table_name(o)
134-
else
130+
131+
o.class.engine.with_connection do |connection|
132+
table_name =
133+
begin
134+
if connection.respond_to?(:sqlserver?) && connection.database_prefix_remote_server?
135+
remote_server_table_name(o)
136+
else
137+
quote_table_name(o.name)
138+
end
139+
rescue Exception
135140
quote_table_name(o.name)
136141
end
137-
rescue Exception
138-
quote_table_name(o.name)
139-
end
140142

141-
if o.table_alias
142-
collector << "#{table_name} #{quote_table_name o.table_alias}"
143-
else
144-
collector << table_name
143+
if o.table_alias
144+
collector << "#{table_name} #{quote_table_name o.table_alias}"
145+
else
146+
collector << table_name
147+
end
145148
end
146149
end
147150

@@ -315,14 +318,16 @@ def primary_Key_From_Table(t)
315318
end
316319

317320
def remote_server_table_name(o)
318-
ActiveRecord::ConnectionAdapters::SQLServer::Utils.extract_identifiers(
319-
"#{o.class.engine.lease_connection.database_prefix}#{o.name}"
320-
).quoted
321+
o.class.engine.with_connection do |connection|
322+
ActiveRecord::ConnectionAdapters::SQLServer::Utils.extract_identifiers(
323+
"#{connection.database_prefix}#{o.name}"
324+
).quoted
325+
end
321326
end
322327

323-
# Need to remove ordering from subqueries unless TOP/OFFSET also used. Otherwise, SQLServer
328+
# Need to remove ordering from sub-queries unless TOP/OFFSET also used. Otherwise, SQLServer
324329
# returns error "The ORDER BY clause is invalid in views, inline functions, derived tables,
325-
# subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified."
330+
# sub-queries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified."
326331
def remove_invalid_ordering_from_select_statement(node)
327332
return unless Arel::Nodes::SelectStatement === node
328333

0 commit comments

Comments
 (0)