-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Override old "documents" fields with new "attachments" fields
If both are set and the attachments field is the same size as the documents field. If they're not the same size an error is logged. We can use this to be confident that, after republishing everything, we've not somehow lost any data. When we're happy to remove the old field, we can revert this commit and just use the new field directly.
- Loading branch information
Showing
4 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
app/presenters/content_item/featured_attachments_migration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module ContentItem | ||
module FeaturedAttachmentsMigration | ||
def choose_field(new_field_name:, old_field_name:) | ||
new_list = content_item["details"][new_field_name] | ||
old_list = content_item["details"][old_field_name] || [] | ||
|
||
# don't raise an error just because a document hasn't been | ||
# republished to have the new field yet. | ||
return old_list if new_list.nil? | ||
|
||
if new_list.length == old_list.length | ||
new_list | ||
else | ||
GovukError.notify( | ||
"Mismatch between attachments and documents", | ||
extra: { error_message: "Document with #{new_list.length} #{new_field_name} but #{old_list.length} #{old_field_name} at #{base_path}" }, | ||
) | ||
|
||
old_list | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/presenters/content_item/featured_attachments_migration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require "test_helper" | ||
|
||
class FeaturedAttachmentsMigrationTest < ActiveSupport::TestCase | ||
class DummyContentItem | ||
include ContentItem::FeaturedAttachmentsMigration | ||
attr_accessor :content_item | ||
|
||
def initialize(old_list, new_list) | ||
@content_item = { | ||
"base_path" => "/a/base/path", | ||
"details" => { "old_badness" => old_list, "new_hotness" => new_list }.compact, | ||
"links" => {}, | ||
} | ||
end | ||
|
||
def choose | ||
choose_field( | ||
old_field_name: "old_badness", | ||
new_field_name: "new_hotness", | ||
) | ||
end | ||
end | ||
|
||
test "presents the old field if the new one is missing" do | ||
item = DummyContentItem.new(%w(1 2 3), nil) | ||
|
||
assert_equal %w(1 2 3), item.choose | ||
end | ||
|
||
test "presents the old field if the new one is a different size" do | ||
item = DummyContentItem.new(%w(1 2 3), %w(foo bar baz bat)) | ||
|
||
assert_equal %w(1 2 3), item.choose | ||
end | ||
|
||
test "presents the new field if present and the old one is the same size" do | ||
item = DummyContentItem.new(%w(1 2 3), %w(foo bar baz)) | ||
|
||
assert_equal %w(foo bar baz), item.choose | ||
end | ||
end |