Skip to content

Commit 038d036

Browse files
committed
Patch SQLServerDatabaseTasks#structure_{dump,load} to utilize tsql-ttds
Relates to: rails-sqlserver#957
1 parent 1e9409d commit 038d036

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/active_record/tasks/sqlserver_database_tasks.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,27 @@ def structure_dump(filename, extra_flags)
7272

7373
dump = File.read(filename)
7474
dump.gsub!(/^USE .*$\nGO\n/, "") # Strip db USE statements
75-
dump.gsub!(/^GO\n/, "") # Strip db GO statements
75+
# dump.gsub!(/^GO\n/, "") # Strip db GO statements
7676
dump.gsub!(/nvarchar\(8000\)/, "nvarchar(4000)") # Fix nvarchar(8000) column defs
7777
dump.gsub!(/nvarchar\(-1\)/, "nvarchar(max)") # Fix nvarchar(-1) column defs
7878
dump.gsub!(/text\(\d+\)/, "text") # Fix text(16) column defs
7979
File.open(filename, "w") { |file| file.puts dump }
8080
end
8181

8282
def structure_load(filename, extra_flags)
83-
connection.execute File.read(filename)
83+
# connection.execute File.read(filename)
84+
server_arg = "-S #{Shellwords.escape(configuration_hash[:host])}"
85+
server_arg += ":#{Shellwords.escape(configuration_hash[:port])}" if configuration_hash[:port]
86+
command = [
87+
"tsql-ttds",
88+
server_arg,
89+
"-D #{Shellwords.escape(configuration_hash[:database])}",
90+
"-U #{Shellwords.escape(configuration_hash[:username])}",
91+
"-P #{Shellwords.escape(configuration_hash[:password])}",
92+
]
93+
94+
stdout_str, stderr_str, status = Open3.capture3(command.join(" "), stdin_data: File.read(filename))
95+
raise "Error loading database: #{stderr_str}" unless status.exitstatus == 0
8496
end
8597

8698
private

0 commit comments

Comments
 (0)