forked from sunny-moore/little-esty-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
153 lines (135 loc) · 4.32 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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
namespace :csv_load do
desc 'Seed Merchant Table!'
task :merchants => :environment do
csv_text = File.read("db/data/merchants.csv")
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
t = Merchant.new
t.id = row['id']
t.name = row['name']
t.created_at = row['created_at']
t.updated_at = row['updated_at']
t.save
puts "Merchant #{t.name} is created"
end
ActiveRecord::Base.connection.reset_pk_sequence!('merchants')
end
desc 'Seed Items Table!'
task :items => :environment do
csv_text = File.read("db/data/items.csv")
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
t = Item.new
t.id = row['id']
t.name = row['name']
t.description = row['description']
t.unit_price = row['unit_price']
t.merchant_id = row['merchant_id']
t.created_at = row['created_at']
t.updated_at = row['updated_at']
t.save
puts "Item #{t.name} is created"
end
ActiveRecord::Base.connection.reset_pk_sequence!('items')
end
desc 'Seed Customers Table!'
task :customers => :environment do
csv_text = File.read("db/data/customers.csv")
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
t = Customer.new
t.id = row['id']
t.first_name = row['first_name']
t.last_name = row['last_name']
t.created_at = row['created_at']
t.updated_at = row['updated_at']
t.save
puts "Customer #{t.first_name} #{t.last_name} is created"
end
ActiveRecord::Base.connection.reset_pk_sequence!('customers')
end
desc 'Seed Invoices Table!'
task :invoices => :environment do
csv_text = File.read("db/data/invoices.csv")
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
t = Invoice.new
t.id = row['id']
t.customer_id = row['customer_id']
if row['status'] == "cancelled"
t.status = 0
elsif row['status'] == "in progress"
t.status = 1
elsif row['status'] == "completed"
t.status = 2
end
t.created_at = row['created_at']
t.updated_at = row['updated_at']
t.save
puts "Invoice #{t.id} is created"
end
ActiveRecord::Base.connection.reset_pk_sequence!('invoices')
end
desc 'Seed Invoice_Items Table!'
task :invoice_items => :environment do
csv_text = File.read("db/data/invoice_items.csv")
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
t = InvoiceItem.new
t.id = row['id']
t.item_id = row['item_id']
t.invoice_id = row['invoice_id']
t.quantity = row['quantity']
t.unit_price = row['unit_price']
if row['status'] == "packaged"
t.status = 0
elsif row['status'] == "pending"
t.status = 1
elsif row['status'] == "shipped"
t.status = 2
end
t.created_at = row['created_at']
t.updated_at = row['updated_at']
t.save
puts "Invoice_Item #{t.item_id} is created"
end
ActiveRecord::Base.connection.reset_pk_sequence!('invoice_items')
end
desc 'Seed Transaction Table!'
task :transactions => :environment do
csv_text = File.read("db/data/transactions.csv")
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
t = Transaction.new
t.id = row['id']
t.invoice_id = row['invoice_id']
t.credit_card_number = row['credit_card_number']
t.credit_card_expiration_date = row['credit_card_expiration_date']
if row['result'] == "failed"
t.result = 0
elsif row['result'] == "success"
t.result = 1
end
t.created_at = row['created_at']
t.updated_at = row['updated_at']
t.save
puts "#{t.id} is created"
end
ActiveRecord::Base.connection.reset_pk_sequence!('merchants')
end
task delete_all: :environment do
Merchant.destroy_all
Item.destroy_all
Customer.destroy_all
Invoice.destroy_all
InvoiceItem.destroy_all
Transaction.destroy_all
puts "'Delete All' Complete"
end
task create_all: [:merchants, :items, :customers, :invoices, :invoice_items, :transactions]
end