-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRakefile
241 lines (189 loc) · 6.16 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require 'i18n'
require_relative 'helpers/environment'
namespace :db do
desc "Run DB migrations on db/migrate directory"
task :migrate do
require 'sequel'
Sequel.extension :migration
env ||= ENV['RACK_ENV'] || :development
FunkyWorldCup::Helpers.init_environment(env)
DB = FunkyWorldCup::Helpers.database
puts 'Running migrations...'
Sequel::Migrator.run(DB, "db/migrate")
puts 'Done!'
end
desc "Seed the database with the teams and matches for FIFA World Cup"
task :seed, [:year] do |t, args|
env ||= ENV['RACK_ENV'] || :development
FunkyWorldCup::Helpers.init_environment(env)
Dir["./models/**/*.rb"].each { |file| require file }
puts "***** WARNING!!! *****"
puts "This will delete previous matches, results and predictions data."
puts "Make sure you have your data backed up!!"
puts
puts "Do you want to continue now? [y/N]"
cont = STDIN.gets
unless cont.strip == "y"
puts "Alright, back it up and I'll see you later!"
exit 0
end
puts "Deleting previous World Cup data..."
DB = FunkyWorldCup::Helpers.database
DB.execute "TRUNCATE user_notifications, match_predictions, match_penalties_predictions, matches, teams, results, cup_groups, group_positions"
require './lib/seed_loader'
puts 'Seeding...'
if args[:year]
SeedLoader.new(true, "db/seeds/worldcup_#{args[:year]}.yml").seed true
else
SeedLoader.new.seed true
end
puts 'Done!'
end
namespace :schema do
desc "Dump the DB schema to db/schema.rb"
task :dump do
require 'sequel'
env ||= ENV['RACK_ENV'] || :development
FunkyWorldCup::Helpers.init_environment(env)
db = FunkyWorldCup::Helpers.database
db.extension :schema_dumper
puts "Dumping schema to db/schema.rb..."
File.open("db/schema.rb", "w") do |f|
f.puts db.dump_schema_migration
end
puts "Done!"
end
desc "Load the DB schema defined in db/schema.rb"
task :load do
require 'sequel'
Sequel.extension :migration
puts "Loading schema..."
env ||= ENV['RACK_ENV'] || :test
FunkyWorldCup::Helpers.init_environment(env)
db = FunkyWorldCup::Helpers.database
migration = eval(File.read('./db/schema.rb'))
puts "Dropping old tables..."
db.drop_table *db.tables, cascade: true
puts "Applying new schema..."
migration.apply(db, :up)
puts "Done!"
end
end
namespace :test do
desc "Prepares test DB by copying current dev schema"
task :prepare do
require 'sequel'
env_val = ENV['RACK_ENV']
ENV['RACK_ENV'] = 'development'
Rake::Task["db:schema:dump"].invoke
ENV['RACK_ENV'] = 'test'
Rake::Task["db:schema:load"].invoke
end
end
end
namespace :test do
@env = 'test'
desc "Test all"
task :all do
load_files "test/helpers/*.rb"
load_files "test/routes/*.rb"
load_files "test/lib/*.rb"
load_files "test/unit/*.rb"
load_files "test/jobs/*.rb"
end
end
namespace :teams do
desc "Fixes teams to use FIFA names"
task :fifa do
require './app'
puts "Updating..."
cote_ivory = Team.find(:iso_code => "CI")
cote_ivory.update(:name => "Côte d'Ivoire") if cote_ivory
england = Team.find(:iso_code => "GB")
england.update(:name => "England") if england
bosnia = Team.find(:iso_code => "BA")
bosnia.update(:name => "Bosnia and Herzegovina") if bosnia
korea = Team.find(:iso_code => "KR")
korea.update(:name => "Korea Republic") if korea
usa = Team.find(:iso_code => "US")
usa.update(:name => "USA") if usa
puts "Done!"
end
desc "Update teams flags to SVG formate"
task :update_flags_files do
require "./app"
Team.each do |team|
team.update(flag: "#{team.flag[0..1]}.svg")
end
end
end
namespace :positions do
desc "Regenerate first phase positions tables"
task :regenerate do
require './app'
puts "Renerating..."
FunkyWorldCup::Helpers.database.transaction do
GroupPosition.dataset.destroy
CupGroup.groups_phase.all.each do |group|
puts "Renerating...Group #{group.name}"
group.teams.each do |team|
GroupPosition.create(
group_id: group.id,
team_id: team.iso_code
)
end
group.matches.each do |match|
GroupPosition.update_positions(match, match.result) unless match.result.nil?
end
end
end
puts "Done!"
end
end
namespace :phase do
namespace :semifinals do
desc 'Fix inverted semifinals'
task :fix do
require './app'
puts "Starting..."
FunkyWorldCup::Helpers.database.transaction do
CupGroup.where(phase: 'semi_finals').first.matches.each do |match|
puts "Match: #{match.host_id} vs #{match.rival_id}"
puts "--> Invert match"
match.update(
rival_id: match.host_id,
rival_code: match.host_code,
rival_description: match.host_description,
host_id: match.rival_id,
host_code: match.rival_code,
host_description: match.rival_description
)
puts "<-- OK"
unless match.result.nil?
puts "--> Invert result"
match.result.update(
host_score: match.result.rival_score,
host_penalties_score: match.result.rival_penalties_score,
rival_score: match.result.host_score,
rival_penalties_score: match.result.host_penalties_score,
)
puts "<-- OK"
end
puts "--> Invert Predictions"
MatchPrediction.where(match_id: match.id).all.each do |prediction|
prediction.update(host_score: prediction.rival_score, rival_score: prediction.host_score)
end
puts "<-- OK"
puts "--> Invert Penalties Predictions"
MatchPenaltiesPrediction.where(match_id: match.id).all.each do |prediction|
prediction.update(host_score: prediction.rival_score, rival_score: prediction.host_score)
end
puts "<-- OK"
end
end
end
end
end
def load_files(dir)
Dir[dir].each { |file| load file }
end