forked from turingschool-examples/little-esty-shop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
96 lines (86 loc) · 3.04 KB
/
Rakefile
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require_relative 'config/application'
require 'csv'
Rails.application.load_tasks
csv_tasks = [:customers, :merchants, :invoices, :items, :invoice_items, :transactions]
namespace :load_csv do
task :customers => :environment do
start = Time.now
puts '-- clearing old data on customers'
Customer.destroy_all
puts '-- Loading Customers ... '
CSV.foreach('./db/data/customers.csv', headers: true) do |row|
Customer.create!(row.to_hash)
ActiveRecord::Base.connection.reset_pk_sequence!(:customers)
end
finish = Time.now
puts "-- Done in %0.1f seconds" % [finish - start]
end
task :invoice_items => :environment do
start = Time.now
puts '-- clearing old data on invoice_items'
InvoiceItem.destroy_all
puts '-- Loading Invoice Items ... '
CSV.foreach('./db/data/invoice_items.csv', headers: true) do |row|
InvoiceItem.create!(row.to_hash)
ActiveRecord::Base.connection.reset_pk_sequence!(:invoice_items)
end
finish = Time.now
puts "-- Done in %0.1f seconds" % [finish - start]
end
task :invoices => :environment do
start = Time.now
puts '-- clearing old data on invoices'
Invoice.destroy_all
puts '-- Loading Invoices ... '
CSV.foreach('./db/data/invoices.csv', headers: true) do |row|
Invoice.create!(row.to_hash)
ActiveRecord::Base.connection.reset_pk_sequence!(:invoices)
end
finish = Time.now
puts "-- Done in %0.1f seconds" % [finish - start]
end
task :items => :environment do
start = Time.now
puts '-- clearing old data on items'
Item.destroy_all
puts '-- Loading Items ... '
CSV.foreach('./db/data/items.csv', headers: true) do |row|
Item.create!(row.to_hash)
ActiveRecord::Base.connection.reset_pk_sequence!(:items)
end
finish = Time.now
puts "-- Done in %0.1f seconds" % [finish - start]
end
task :merchants => :environment do
start = Time.now
puts '-- clearing old data on merchants'
Merchant.destroy_all
puts '-- Loading Merchants ... '
CSV.foreach('./db/data/merchants.csv', headers: true) do |row|
Merchant.create!(row.to_hash.to_hash)
ActiveRecord::Base.connection.reset_pk_sequence!(:merchants)
end
finish = Time.now
puts "-- Done in %0.1f seconds" % [finish - start]
end
task :transactions => :environment do
start = Time.now
puts '-- clearing old data on transactions'
Transaction.destroy_all
puts '-- Loading Transactions ... '
CSV.foreach('./db/data/transactions.csv', headers: true) do |row|
Transaction.create!(row.to_hash)
ActiveRecord::Base.connection.reset_pk_sequence!(:transactions)
end
finish = Time.now
puts "-- Done in %0.1f seconds" % [finish - start]
end
task :all => :environment do
csv_tasks.each do |task|
Rake::Task["load_csv:#{task.to_s}"].reenable
Rake::Task["load_csv:#{task.to_s}"].invoke
end
end
end