Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some generic YAML backup/restore code for my convenience

  • Loading branch information...
commit 6ced31c54e30576fa54040e10a8d59d7e0c32925 1 parent a621677
@konklone konklone authored
Showing with 45 additions and 2 deletions.
  1. +2 −1  .gitignore
  2. +43 −1 Rakefile
View
3  .gitignore
@@ -1,2 +1,3 @@
/config/database.yml
-/*.sqlite3
+/*.sqlite3
+/data
View
44 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
Please sign in to comment.
Something went wrong with that request. Please try again.