Skip to content

Commit

Permalink
Patch SQLServerDatabaseTasks#structure_{dump,load} to utilize tsql-ttds
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahgeorge committed Jul 9, 2024
1 parent 1e9409d commit 038d036
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/active_record/tasks/sqlserver_database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,27 @@ def structure_dump(filename, extra_flags)

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

def structure_load(filename, extra_flags)
connection.execute File.read(filename)
# connection.execute File.read(filename)
server_arg = "-S #{Shellwords.escape(configuration_hash[:host])}"
server_arg += ":#{Shellwords.escape(configuration_hash[:port])}" if configuration_hash[:port]
command = [
"tsql-ttds",
server_arg,
"-D #{Shellwords.escape(configuration_hash[:database])}",
"-U #{Shellwords.escape(configuration_hash[:username])}",
"-P #{Shellwords.escape(configuration_hash[:password])}",
]

stdout_str, stderr_str, status = Open3.capture3(command.join(" "), stdin_data: File.read(filename))
raise "Error loading database: #{stderr_str}" unless status.exitstatus == 0
end

private
Expand Down

0 comments on commit 038d036

Please sign in to comment.