-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rakefile
99 lines (89 loc) · 2.86 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
require 'dotenv/tasks'
task app: :dotenv do
require_relative './app'
end
namespace :db do
desc 'Run migrations'
task :migrate, [:version] => :app do |_t, args|
require 'sequel'
Sequel.extension :migration
db = Sinatra::Application.database
if args[:version]
puts "Migrating to version #{args[:version]}"
Sequel::Migrator.run(db, 'db/migrations', target: args[:version].to_i)
else
puts 'Migrating to latest'
Sequel::Migrator.run(db, 'db/migrations')
end
end
end
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'spec'
t.test_files = FileList['spec/*_spec.rb']
t.verbose = true
end
require 'rubocop/rake_task'
RuboCop::RakeTask.new
task default: :test
task cache: :app do
UpdateCacheJob.new.perform
end
task add_country_slug_to_featured_countries: :app do
FeaturedCountry.each do |fc|
country = Everypolitician.country(code: fc.country_code)
fc.update(country_slug: country.slug)
end
end
task create_country_uuids: :app do
db = Sinatra::Application.database
Everypolitician.countries.each do |country|
country.legislatures.each do |legislature|
puts "Creating country_uuids rows for #{country.name} - #{legislature.name}"
legislature.popolo.persons.each do |person|
db[:country_uuids].insert(
country_slug: country.slug,
legislature_slug: legislature.slug,
uuid: person[:id],
created_at: DateTime.now
)
end
end
end
end
def id_mapper(country_code, legislature_slug)
@mappers ||= {}
@mappers[country_code] ||= {}
@mappers[country_code][legislature_slug] ||= begin
legislature = Everypolitician.country(code: country_code).legislature(slug: legislature_slug)
LegacyIdMapper.new(legislature.popolo)
end
end
task migrate_responses_to_votes: :app do
db = Sinatra::Application.database
responses = db[:responses]
.select(:user_id, :politician_id, :choice, :country_code, :legislature_slug, :responses__created_at, :responses__updated_at)
.join(:legislative_periods, id: :legislative_period_id)
.order(Sequel.desc(:responses__created_at))
warn "Found #{responses.count} responses to migrate to votes"
responses.each do |response|
legacy_id_mapper = id_mapper(response[:country_code], response[:legislature_slug])
begin
db[:votes].insert(
user_id: response[:user_id],
person_uuid: legacy_id_mapper.reverse_map[response[:politician_id]],
choice: response[:choice],
created_at: response[:created_at],
updated_at: response[:updated_at]
)
rescue Sequel::UniqueConstraintViolation => e
warn e
end
end
end
desc 'Perform steps necessary for switching from responses to votes'
task :votes do
Rake::Task['create_country_uuids'].invoke
Rake::Task['add_country_slug_to_featured_countries'].invoke
Rake::Task['migrate_responses_to_votes'].invoke
end