Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Views related fixes for structure dump and load #400

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/active_record/tasks/sqlserver_database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ def structure_dump(filename)
]
table_args = connection.tables.map { |t| Shellwords.escape(t) }
command.concat(table_args)
view_args = connection.views.map { |v| Shellwords.escape(v) }
command.concat(view_args)
raise 'Error dumping database' unless Kernel.system(command.join(' '))
dump = File.read(filename)
dump.gsub!(/^USE .*$\nGO\n/, '') # Strip db USE statements
Expand All @@ -69,10 +67,24 @@ def structure_dump(filename)
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 }

# defncopy appears to truncate definition output in some circumstances
# Also create view needs to be the first operation in the batch.
File.open(filename, 'a') { |file|
connection.select_all("select definition, o.type from sys.objects as o join sys.sql_modules as m on m.object_id = o.object_id where o.type = 'V'").each do |row|
file.puts "\r\nGO\r\n#{row['definition']}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use this SQL vs our existing view_information method? Does one work better than the other?

end
file.puts "\r\nGO\r\n"
}
end

def structure_load(filename)
connection.execute File.read(filename)
structure = File.read(filename)
# Split by GO so that operations that must be in separate batches are in
# separate batches
structure.split(/^GO/).each { |s|
connection.execute s
}
end


Expand Down