Skip to content

Commit ffa0f44

Browse files
committed
Backfill NotifyLogEntry purpose column using data migrate gem
Taking the opportunity to backfill a column using this gem instead of having to do it manually.
1 parent 9246530 commit ffa0f44

5 files changed

Lines changed: 72 additions & 84 deletions

File tree

app/lib/data_migration/backfill_notify_log_entries.rb

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
class BackfillPurposeForNotifyLogEntries < ActiveRecord::Migration[8.1]
4+
TEMPLATE_NAME_BY_TEMPLATE_ID = {
5+
**GOVUK_NOTIFY_UNUSED_TEMPLATES,
6+
**GOVUK_NOTIFY_EMAIL_TEMPLATES.invert,
7+
**GOVUK_NOTIFY_SMS_TEMPLATES.invert
8+
}.freeze
9+
10+
def up
11+
migration = self.class.name
12+
started_at = Time.zone.now
13+
14+
batch_size = 1000
15+
total_records = NotifyLogEntry.count
16+
total_batches = (total_records / batch_size.to_f).ceil
17+
18+
batch_processed = 0
19+
records_updated = 0
20+
21+
# Helps us monitor progress in CloudWatch
22+
Rails.logger.info(event: "data_migration_start", migration:, total_records:, total_batches:)
23+
24+
NotifyLogEntry.find_in_batches(batch_size:) do |notify_log_entries|
25+
notify_log_entries.filter_map do |notify_log_entry|
26+
template_name =
27+
TEMPLATE_NAME_BY_TEMPLATE_ID.fetch(notify_log_entry.template_id, nil)
28+
29+
next unless template_name
30+
31+
purpose = NotifyLogEntry.purpose_for_template_name(template_name)
32+
33+
next unless purpose
34+
35+
notify_log_entry.update_column(
36+
:purpose,
37+
NotifyLogEntry.purposes.fetch(purpose)
38+
)
39+
40+
records_updated += 1
41+
end
42+
43+
batch_processed += 1
44+
45+
Rails.logger.info(
46+
event: "data_migration_batch",
47+
migration:,
48+
records_updated:,
49+
batch_size:,
50+
batch_processed:,
51+
total_batches:,
52+
)
53+
end
54+
55+
duration_minutes = ((Time.zone.now - started_at) / 60.0).round
56+
57+
Rails.logger.info(
58+
event: "data_migration_finish",
59+
migration:,
60+
duration_minutes:,
61+
total_records:,
62+
records_updated:,
63+
)
64+
end
65+
66+
def down
67+
raise ActiveRecord::IrreversibleMigration
68+
end
69+
end

db/data_schema.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
DataMigrate::Data.define(version: 20_260_304_173_751)

lib/tasks/data_migration.rake

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/lib/data_migration/backfill_notify_log_entries_spec.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)