-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb_utils.rb
73 lines (65 loc) · 1.36 KB
/
db_utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'data_mapper'
require 'json'
require 'yaml'
require 'pry'
require 'nokogiri'
require 'open-uri'
Dir.glob("models/*.rb").each {|x| require_relative x}
def import_model(model_class)
infile = File.new("dumped_#{model_class.to_s.downcase}.json")
json_data = JSON.parse(infile.read)
count = 0
json_data.each do |row|
count += 1
model_class.create(row)
end
p "Importer #{count} instances of #{model_class}"
end
def dump_model(model_class)
collector = []
model_class.all.each do |m|
collector << JSON.parse(m.to_json)
end
outfile = File.new("dumped_#{model_class.to_s.downcase}.json", "w")
outfile.write(collector.to_json)
end
def import(destructive)
DataMapper.auto_migrate! if destructive
import_model(Monster)
import_model(User)
end
def export
dump_model(User)
dump_model(Monster)
end
def run_upgrade
DataMapper.auto_upgrade!
end
def console
binding.pry
end
def print_usage
puts "USAGE: ruby db_utils.rb [IMPORT|EXPORT|CONSOLE|MIGRATE]"
end
config = YAML.load(File.read("database_config.yaml"))
DataMapper.setup(:default, config)
DataMapper.finalize
command = ARGV.first
if command.nil?
print_usage
else
case command
when 'migrate'
run_upgrade
when 'import'
import(true)
when 'append'
import(false)
when 'export'
export
when 'console'
console
else
print_usage
end
end