Forked from brianburridge/gist:8d2755a73dd5b4f0332b
Last active
May 14, 2021 15:17
-
-
Save Ch4s3/d6af9c942505f4647f777be4a00a73ad to your computer and use it in GitHub Desktop.
Revisions
-
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 0 additions and 1 deletion.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 @@ -28,7 +28,6 @@ model_var = class_type.new(model) model_var.save end end end -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 1 addition and 1 deletion.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 @@ -4,7 +4,7 @@ Rails.application.eager_load! ApplicationRecord.descendants.each do |model| next if model.table_name.nil? || model.table_name == '' begin data = model.all next if data == [] -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 3 additions and 1 deletion.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 @@ -6,8 +6,10 @@ ApplicationRecord.descendants.each do |model| next if model.table_name.nil? || modle.table_name == '' begin data = model.all next if data == [] file = File.open(File.join(Rails.root, 'db', 'export', "#{model.table_name}.json"), 'w') file.write data.to_json file.close rescue => e puts e -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 4 additions and 4 deletions.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 @@ -1,12 +1,12 @@ namespace :json do desc 'Export all data to JSON files' task :export => :environment do Rails.application.eager_load! ApplicationRecord.descendants.each do |model| next if model.table_name.nil? || modle.table_name == '' begin file = File.open(File.join(Rails.root, 'db', 'export', "#{model.table_name}.json"), 'w') file.write model.all.to_json file.close rescue => e @@ -15,7 +15,7 @@ end end desc 'Import all data from JSON files' task :import => :environment do Dir["./db/export/*.json"].each do |file| -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 7 additions and 3 deletions.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 @@ -5,9 +5,13 @@ ApplicationRecord.descendants.each do |model| next if model.table_name.nil? begin file = File.open(File.join(Rails.root, "db", "export", "#{model.table_name}.json"), 'w') file.write model.all.to_json file.close rescue => e puts e end end end -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 1 addition and 0 deletions.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 @@ -4,6 +4,7 @@ Rails.application.eager_load! ApplicationRecord.descendants.each do |model| next if model.table_name.nil? file = File.open(File.join(Rails.root, "db", "export", "#{model.table_name}.json"), 'w') file.write model.all.to_json file.close -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 1 addition and 1 deletion.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 @@ -3,7 +3,7 @@ task :export => :environment do Rails.application.eager_load! ApplicationRecord.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 -
Ch4s3 renamed this gist
Aug 28, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 1 addition and 1 deletion.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 @@ -3,7 +3,7 @@ task :export => :environment do Rails.application.eager_load! ApplicationRecord.descendants.find do |model| file = File.open(File.join(Rails.root, "db", "export", "#{model.table_name}.json"), 'w') file.write model.all.to_json file.close -
Ch4s3 revised this gist
Aug 28, 2018 . 1 changed file with 2 additions and 2 deletions.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 @@ -3,7 +3,7 @@ task :export => :environment do Rails.application.eager_load! ApplicationRecord.descendants.find_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 @@ -21,7 +21,7 @@ model_var = class_type.new(model) model_var.save end ApplicationRecord.connection.reset_pk_sequence!(table_name) end end -
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