Skip to content

Commit c21ab7d

Browse files
authored
Merge pull request #2590 from alphagov/add-rake-tasks-to-fix-first-made-live-at
Add rake tasks to fix first_made_live_at for copied forms
2 parents 23d9c3c + 65d66d2 commit c21ab7d

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace :fix_first_made_live_at do
2+
desc "Updates forms to add first_made_live_at"
3+
task :update_date, [] => :environment do |_, args|
4+
Rails.logger.info "data_migration:update_date"
5+
6+
update_forms_first_made_live_at(args.to_a)
7+
end
8+
9+
desc "Dry run updates forms to add first_made_live_at"
10+
task :update_date_dry_run, [] => :environment do |_, args|
11+
ActiveRecord::Base.transaction do
12+
Rails.logger.info "data_migration:update_date_dry_run"
13+
14+
update_forms_first_made_live_at(args.to_a)
15+
16+
raise ActiveRecord::Rollback
17+
end
18+
end
19+
20+
desc "Clear first_made_live_at for draft forms"
21+
task clear_for_draft: :environment do
22+
forms = Form.draft.where.not(first_made_live_at: nil)
23+
Rails.logger.info "Found #{forms.count} draft forms with first_made_live_at set"
24+
forms.update_all(first_made_live_at: nil)
25+
Rails.logger.info "Finished clearing first_made_live_at for draft forms"
26+
end
27+
28+
desc "Clear first_made_live_at for draft forms"
29+
task clear_for_draft_dry_run: :environment do
30+
forms = Form.draft.where.not(first_made_live_at: nil)
31+
Rails.logger.info "Found #{forms.count} draft forms with first_made_live_at set"
32+
forms.find_each do |form|
33+
Rails.logger.info "Would clear first_made_live_at for form with ID #{form.id}"
34+
end
35+
end
36+
end
37+
38+
def update_forms_first_made_live_at(forms_and_dates)
39+
Rails.logger.info "Updating #{forms_and_dates.length} forms"
40+
41+
forms_and_dates.each do |form_and_date|
42+
form_id, first_made_live_at = form_and_date.split(":", 2)
43+
first_made_live_at = Time.zone.parse(first_made_live_at)
44+
45+
Rails.logger.info "Updating form #{form_id} to have first_made_live_at #{first_made_live_at}"
46+
47+
form = Form.find(form_id)
48+
form.first_made_live_at = first_made_live_at
49+
50+
begin
51+
form.save!
52+
53+
form.form_documents.each do |form_document|
54+
form_document.content[:first_made_live_at] = first_made_live_at
55+
56+
form_document.save!
57+
end
58+
rescue StandardError => e
59+
Rails.logger.info "Failed to update form #{form_id}: #{e.message}"
60+
end
61+
end
62+
end

lib/tasks/forms.rake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ namespace :forms do
153153
desc "Check selection options in Page DraftQuestion and FormDocument"
154154
task check_selection_options: :environment do
155155
pages_with_selection_options_with_no_value = Page.where(answer_type: "selection")
156-
.where("jsonb_path_exists(answer_settings, '$.selection_options[*] ? (!exists(@.value))')")
157-
.count
156+
.where("jsonb_path_exists(answer_settings, '$.selection_options[*] ? (!exists(@.value))')")
157+
.count
158158

159159
draft_questions_with_selection_options_with_no_value = DraftQuestion.where(answer_type: "selection")
160-
.where("jsonb_path_exists(answer_settings, '$.selection_options[*] ? (!exists(@.value))')")
161-
.count
160+
.where("jsonb_path_exists(answer_settings, '$.selection_options[*] ? (!exists(@.value))')")
161+
.count
162162

163163
Rails.logger.info "Pages with selection options with no value: #{pages_with_selection_options_with_no_value}"
164164
Rails.logger.info "DraftQuestions with selection options with no value: #{draft_questions_with_selection_options_with_no_value}"

0 commit comments

Comments
 (0)