-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_import.rb
103 lines (83 loc) · 2.97 KB
/
main_import.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
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
require 'csv'
class MainImport
include ImportUtils
attr_reader :skipped, :warned, :authorization_request_ids
def initialize(authorization_request_ids: [])
@skipped = []
@warned = []
@authorization_request_ids = authorization_request_ids
end
def perform
organizations = import(:organizations, { load_from_sql: ENV['DUMP'] == 'true' })
import(:users, { load_from_sql: ENV['DUMP'] == 'true' })
authorization_requests = import(:authorization_requests, { load_from_sql: ENV['DUMP'] == 'true' })
if types_to_import.any?
valid_authorization_request_ids = AuthorizationRequest.where(id: authorization_requests.pluck(:id), type: types_to_import).pluck(:id)
else
valid_authorization_request_ids = authorization_requests.pluck(:id)
end
import(:authorization_request_events, { dump_sql: ENV['DUMP'] == 'true', valid_authorization_request_ids: })
%i[warned skipped].each do |kind|
export(kind)
print_stats(kind)
end
end
private
def types_to_import
%w[
AuthorizationRequest::HubEEDila
AuthorizationRequest::HubEECertDC
]
end
def import(klass_name, options = {})
if authorization_request_ids.any?
options[:authorization_request_ids] = authorization_request_ids
options[:authorization_requests_sql_where] = "id IN (#{authorization_request_ids.join(',')})"
options[:users_sql_where] = "id IN (select user_id from events where authorization_request_id IN (#{authorization_request_ids.join(',')})) or id IN (select user_id from enrollments where id IN(#{authorization_request_ids.join(',')}))"
options[:authorization_request_events_sql_where] = "authorization_request_id IN (#{authorization_request_ids.join(',')})"
end
Import.const_get(klass_name.to_s.classify << 's').new(global_options.merge(options)).perform
end
def export(kind)
return unless ENV['LOCAL'] == 'true'
data = public_send(kind)
CSV.open(export_path(kind), 'w') do |csv|
csv << %w[id target_api kind]
data.each do |datum|
csv << [datum.id, datum.target_api, datum.kind]
end
end
end
def print_stats(kind)
data = public_send(kind)
log("# #{kind}: #{data.count}")
log("#{kind.to_s.humanize} stats:")
log(' - by target_api:')
data.group_by(&:target_api).each do |target_api, skipped|
log(" #{target_api}: #{skipped.count}")
end
log(' - by error type:')
data.group_by(&:kind).each do |k, skipped|
log(" #{k}: #{skipped.count}")
end
end
def export_path(kind)
Rails.root.join("app/migration/dumps/#{kind}.csv")
end
def global_options
{
authorization_requests_filter: ->(enrollment_row) do
%w[
5
26
129
25590
54115
].exclude?(enrollment_row['id'])
end,
authorization_requests_sql_where: 'target_api in (\'hubee_portail\', \'hubee_portail_dila\') order by id desc',
skipped: @skipped,
warned: @warned,
}
end
end