Skip to content

Commit 2928762

Browse files
committed
Backfill NotifyLogEntry purpose for existing records
Add a data migration to populate `notify_log_entries.purpose` from the template ID→name mapping. The mapping includes both current template IDs and historical IDs (recovered from git history) so older records can be backfilled accurately. Jira-Issue: MAV-502
1 parent 0115c49 commit 2928762

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
TEMPLATE_NAME_BY_TEMPLATE_ID = {
4+
**GOVUK_NOTIFY_UNUSED_TEMPLATES,
5+
**GOVUK_NOTIFY_EMAIL_TEMPLATES.invert,
6+
**GOVUK_NOTIFY_SMS_TEMPLATES.invert
7+
}.freeze
8+
9+
class DataMigration::BackfillNotifyLogEntries
10+
def call
11+
progress_bar =
12+
# rubocop:disable Rails/SaveBang
13+
ProgressBar.create(
14+
total: NotifyLogEntry.count,
15+
format: "%a %b\u{15E7}%i %p%% %t",
16+
progress_mark: " ",
17+
remainder_mark: "\u{FF65}"
18+
)
19+
# rubocop:enable Rails/SaveBang
20+
21+
NotifyLogEntry.find_in_batches do |notify_log_entries|
22+
notify_log_entries.filter_map do |notify_log_entry|
23+
template_name =
24+
TEMPLATE_NAME_BY_TEMPLATE_ID.fetch(notify_log_entry.template_id, nil)
25+
26+
next unless template_name
27+
28+
purpose = NotifyLogEntry.purpose_for_template_name(template_name)
29+
30+
next unless purpose
31+
32+
notify_log_entry.update_column(
33+
:purpose,
34+
NotifyLogEntry.purposes.fetch(purpose)
35+
)
36+
end
37+
38+
progress_bar.progress += 1
39+
end
40+
41+
progress_bar.finish
42+
end
43+
44+
def self.call(...) = new(...).call
45+
46+
private_class_method :new
47+
end

lib/tasks/data_migration.rake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
namespace :data_migration do
4+
desc "Backfill purpose column for NotifyLogEntry records"
5+
task backfill_notify_log_entries: :environment do
6+
DataMigration::BackfillNotifyLogEntries.call
7+
end
8+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
describe DataMigration::BackfillNotifyLogEntries do
4+
describe ".call" do
5+
let!(:mapped_entry) do
6+
create(
7+
:notify_log_entry,
8+
:email,
9+
template_id: "14e88a09-4281-4257-9574-6afeaeb42715",
10+
purpose: nil
11+
)
12+
end
13+
let!(:unknown_entry) do
14+
create(
15+
:notify_log_entry,
16+
:email,
17+
template_id: "99999999-9999-9999-9999-999999999999",
18+
purpose: nil
19+
)
20+
end
21+
22+
it "backfills purpose from historical template ID mappings" do
23+
described_class.call
24+
25+
expect(mapped_entry.reload.purpose).to eq("consent_request")
26+
expect(unknown_entry.reload.purpose).to be_nil
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)