Skip to content

Commit

Permalink
Added some generic YAML backup/restore code for my convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
konklone committed Feb 15, 2010
1 parent a621677 commit 6ced31c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
/config/database.yml
/*.sqlite3
/*.sqlite3
/data
44 changes: 43 additions & 1 deletion Rakefile
@@ -1,10 +1,52 @@
desc 'Migrate the database'
task :migrate => :environment do
ActiveRecord::Base.logger = Logger.new STDOUT
ActiveRecord::Migrator.migrate 'migrations', (ENV['VERSION'] ? ENV['VERSION'].to_i : nil)
ActiveRecord::Migrator.migrate 'migrations', (ENV['version'] ? ENV['version'].to_i : nil)
end

desc 'Loads environment'
task :environment do
require 'ohnomymoney'
end


# Generic YAML loading/backup code, for my fixture convenience
namespace :data do

desc "Restore a model using YAML backup"
task :restore => :environment do
model = ENV['model'].singularize.camelize.constantize
filename = "%s.yml" % File.join("data", model.table_name)

model.delete_all

YAML::load_file(filename).each do |data_row|
record = model.new
data_row.keys.each do |field|
record[field] = data_row[field] if data_row[field]
end
record.save
end
end


desc "Backup a model to YAML."
task :backup => :environment do
model = ENV['model'].singularize.camelize.constantize
filename = "%s.yml" % File.join("data", model.table_name)

data = model.all.reduce([]) do |records, record|
element = {}
model.columns.map(&:name).each do |field|
element[field] = record[field]
end
records << element
end

FileUtils.mkdir_p File.dirname(filename)
File.open(filename, "w") do |file|
YAML.dump data, file
end
end

end

0 comments on commit 6ced31c

Please sign in to comment.