Skip to content

Instantly share code, notes, and snippets.

@timander
Created February 22, 2010 22:42
Show Gist options
  • Save timander/311605 to your computer and use it in GitHub Desktop.
Save timander/311605 to your computer and use it in GitHub Desktop.
utilities to easily split/combine sql files
#!/bin/ruby
def combine_sql_files(final_sql)
Dir.glob("*.sql").sort.each do |file|
puts file
open(final_sql, 'a' ) {|f|
f.puts "\n-- #{file}\nGO\n"
}
`cat "#{file}" >> #{final_sql}`
end
`unix2dos #{final_sql}`
end
combine_sql_files('my_big_file.sql')
original_file = ARGV[0]
original_sql_file = File.readlines(original_file)
prefix = original_file.gsub(/\..*$/, "")
new_file_lines = []
count = 0
original_sql_file.each do |line|
new_file_lines << line
if line =~ /GO$/
new_sql_file = File.new(sprintf("#{prefix}_%05d.sql", count), 'w')
new_sql_file.write(new_file_lines.join(""))
new_file_lines = []
count += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment