Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Last active November 18, 2024 16:59
Show Gist options
  • Save chrisbloom7/10330 to your computer and use it in GitHub Desktop.
Save chrisbloom7/10330 to your computer and use it in GitHub Desktop.
I honestly don't remember what I was attempting to accomplish with this, but it was important enough at the time to be my second Gist
task :write => :environment do
dir = RAILS_ROOT + '/db/backup'
FileUtils.mkdir_p(dir)
FileUtils.chdir(dir)
interesting_tables.each do |tbl|
sql ||= ActiveRecord::Base.connection()
if (records = sql.execute("SELECT * FROM " + tbl) rescue false)
puts "Writing #{tbl}..."
collection = Array.new; records.each_hash(){|rec| collection.push rec }
File.open("#{tbl}.yml", 'w+') { |f| YAML.dump collection, f }
else
puts "Skipping #{tbl}..."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment