/
20210212111156_migrate_mail_log.rb
108 lines (90 loc) · 2.97 KB
/
20210212111156_migrate_mail_log.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
104
105
106
107
108
# frozen_string_literal: true
# Copyright (c) 2020, CVP Schweiz. This file is part of
# hitobito and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito.
class MigrateMailLog < ActiveRecord::Migration[6.0]
def up
change_table(:mail_logs) { |t| t.belongs_to :message }
# temporarly add mail log id for migration
add_column :messages, :mail_log_id, :bigint
MailLog.reset_column_information
Message.reset_column_information
say_with_time 'creating bulk mail messages' do
create_bulk_mail_messages
end
say_with_time 'linking mail-logs to messages' do
assign_mail_logs_message_id
end
remove_columns :mail_logs, :mailing_list_id, :mail_subject
remove_columns :messages, :mail_log_id
Message.reset_column_information
end
def down
add_column :mail_logs, :mail_subject, :string
change_table :mail_logs do |t|
t.belongs_to :mailing_list
end
MailLog.reset_column_information
say_with_time 'extract mail-logs from messages' do
to_mail_log
end
remove_column :mail_logs, :message_id
end
private
def create_bulk_mail_messages
MailLog.find_in_batches do |logs|
message_rows = logs.map { |log| message_attrs(log) }
create_messages(message_rows)
end
end
def create_messages(message_rows)
Message.insert_all!(message_rows)
rescue => e
if (row = e.message.scan(/.*row (\d+)$/).flatten.first)
msg = message_rows.delete_at(row.to_i - 1)
say "Error creating Message", :subitem
puts e.message
puts msg.inspect
say "skipping it and retrying the insert", :subitem
retry
end
end
def message_attrs(log)
now = Time.zone.now
message_status = MailLog::BULK_MESSAGE_STATUS[log.status.to_sym]
{ subject: log.mail_subject,
state: message_status,
mail_log_id: log.id,
type: Message::BulkMail.sti_name,
created_at: now,
updated_at: now,
sent_at: log.updated_at,
mailing_list_id: log.mailing_list_id }
end
def assign_mail_logs_message_id
Message::BulkMail.find_in_batches do |msgs|
now = Time.zone.now
# created_at, updated_at has to be set since upsert_all
# is complaining about missing default values
log_rows = msgs.map do |m|
{ id: m.mail_log_id, message_id: m.id, created_at: now, updated_at: now }
end
MailLog.upsert_all(log_rows)
end
end
def to_mail_log
Message::BulkMail.includes(:mail_log).find_in_batches do |msgs|
now = Time.zone.now
# created_at, updated_at has to be set since upsert_all
# is complaining about missing default values
log_rows = msgs.map do |m|
{ id: m.mail_log.id, mail_subject: m.subject,
mailing_list_id: m.mailing_list_id,
created_at: now, updated_at: now }
end
MailLog.upsert_all(log_rows)
end
Message::BulkMail.destroy_all
end
end