Created
December 10, 2014 19:47
-
-
Save brianburridge/8d2755a73dd5b4f0332b to your computer and use it in GitHub Desktop.
Revisions
-
brianburridge created this gist
Dec 10, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ namespace :json do desc "Export all data to JSON files" task :export => :environment do Rails.application.eager_load! ActiveRecord::Base.descendants.each do |model| file = File.open(File.join(Rails.root, "db", "export", "#{model.table_name}.json"), 'w') file.write model.all.to_json file.close end end desc "Import all data from JSON files" task :import => :environment do Dir["./db/export/*.json"].each do |file| table_name = file.split('/').last.split('.').first class_type = table_name.classify.constantize models = JSON.parse(File.read(file)) models.each do |model| model_var = class_type.new(model) model_var.save end ActiveRecord::Base.connection.reset_pk_sequence!(table_name) end end end