generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This data migration fixes the data in the application that was previously updated in error. We previously updated the `transparency_identifier` which should be immutable. This data migration reverts that change. We then set the new `hybrid_beis_dsit_activity` which makes the application display 'previous reporting organisation' info in the IATI xml. A 'hybrid activity' is one that was started under BEIS but continued under DSIT and so has to show: - the previous reporting organisation - retain the old IATI identifier
- Loading branch information
Showing
2 changed files
with
163 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Require me in the console with `require Rails.root + "db/data/20241114082139_fix_hybrid_beis_dsit_identifier.rb"` | ||
# then run me with `FixHybridBeisDsitIdentifier.new.migrate!` | ||
# | ||
# Description: | ||
# | ||
# For every Activity where `previous_identifier` is NOT nil | ||
# When the `previous_identifier` starts "GB-GOV-13" AND `transparency_identifier` starts "GB-GOV-26" | ||
# Then set `transparency_identifier` to start "GB-GOV-13" | ||
# AND set `previous_identifier` to nil | ||
# AND set hybrid_beis_dsit_activity to true. | ||
# | ||
class FixHybridBeisDsitIdentifier | ||
attr_reader :target, :updated | ||
|
||
def initialize | ||
@target = 0 | ||
@updated = 0 | ||
end | ||
|
||
def migrate! | ||
target_activities.each do |activity| | ||
fix_activity(activity) | ||
end | ||
|
||
puts "Total target activities: #{@target}" | ||
puts "Total updated activities: #{@updated}" | ||
|
||
true | ||
end | ||
|
||
def target_activities | ||
activities = Activity.where.not(previous_identifier: nil).select do |activity| | ||
identifier_starts_with_beis(activity.previous_identifier) && | ||
identifier_starts_with_dsit(activity.transparency_identifier) | ||
end | ||
|
||
@target = activities.count | ||
activities | ||
end | ||
|
||
def fix_activity(activity) | ||
puts "Updating activity id #{activity.id} with IATI ID: #{activity.transparency_identifier} and Previous IATI ID: #{activity.previous_identifier}" | ||
|
||
activity.update!( | ||
transparency_identifier: activity.previous_identifier, | ||
previous_identifier: nil, | ||
hybrid_beis_dsit_activity: true | ||
) | ||
|
||
@updated += 1 | ||
puts "Activity Updated!" | ||
end | ||
|
||
def identifier_starts_with_beis(identifier) | ||
return if identifier.nil? | ||
|
||
identifier.slice(0, 9).eql?("GB-GOV-13") | ||
end | ||
|
||
def identifier_starts_with_dsit(identifier) | ||
return if identifier.nil? | ||
|
||
identifier.slice(0, 9).eql?("GB-GOV-26") | ||
end | ||
end |
98 changes: 98 additions & 0 deletions
98
spec/db/data/20241114082139_fix_hybrid_beis_dsit_identifier_spec.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,98 @@ | ||
require Rails.root + "db/data/20241114082139_fix_hybrid_beis_dsit_identifier.rb" | ||
|
||
RSpec.describe FixHybridBeisDsitIdentifier do | ||
describe "#migrate!" do | ||
it "updates only the appropriate activities and includes a count" do | ||
activity_to_update = create( | ||
:programme_activity, | ||
transparency_identifier: "GB-GOV-26-AAAA-BBBB-CCC-DDDDDDD", | ||
previous_identifier: "GB-GOV-13-AAAA-BBBB-CCC-DDDDDDD", | ||
hybrid_beis_dsit_activity: false | ||
) | ||
activity_to_ignore = create( | ||
:programme_activity, | ||
transparency_identifier: "GB-GOV-26-EEEE-FFFF-GGG-HHHHHHH", | ||
previous_identifier: nil, | ||
hybrid_beis_dsit_activity: false | ||
) | ||
other_activity_to_ignore = create( | ||
:programme_activity, | ||
transparency_identifier: "GB-GOV-13-EEEE-FFFF-GGG-HHHHHHH", | ||
previous_identifier: "GB-GOV-13-IIII_JJJJ_KKK_LL", | ||
hybrid_beis_dsit_activity: false | ||
) | ||
|
||
migration = described_class.new | ||
migration.migrate! | ||
|
||
expect(activity_to_update.reload.transparency_identifier).to eql "GB-GOV-13-AAAA-BBBB-CCC-DDDDDDD" | ||
expect(activity_to_update.hybrid_beis_dsit_activity).to be true | ||
|
||
expect(activity_to_ignore.reload.transparency_identifier).to eql "GB-GOV-26-EEEE-FFFF-GGG-HHHHHHH" | ||
expect(activity_to_ignore.hybrid_beis_dsit_activity).to be false | ||
|
||
expect(other_activity_to_ignore.reload.transparency_identifier).to eql "GB-GOV-13-EEEE-FFFF-GGG-HHHHHHH" | ||
expect(other_activity_to_ignore.hybrid_beis_dsit_activity).to be false | ||
|
||
expect(migration.target).to be 1 | ||
expect(migration.updated).to be 1 | ||
end | ||
end | ||
|
||
describe "#identifier_starts_with_beis" do | ||
it "returns true for identifiers that start GB-GOV-13" do | ||
valid_identifier = "GB-GOV-13-1234-5678-91011" | ||
|
||
expect(described_class.new.identifier_starts_with_beis(valid_identifier)).to be true | ||
end | ||
|
||
it "returns false for identifiers that do not start GB-GOV-13" do | ||
valid_identifier = "GB-GOV-10-1234-5678-91011" | ||
|
||
expect(described_class.new.identifier_starts_with_beis(valid_identifier)).to be false | ||
end | ||
end | ||
|
||
describe "#identifier_starts_with_dsit" do | ||
it "returns true for identifiers that start GB-GOV-26" do | ||
valid_identifier = "GB-GOV-26-1234-5678-91011" | ||
|
||
expect(described_class.new.identifier_starts_with_dsit(valid_identifier)).to be true | ||
end | ||
|
||
it "returns false for identifiers that do not start GB-GOV-26" do | ||
valid_identifier = "GB-GOV-10-1234-5678-91011" | ||
|
||
expect(described_class.new.identifier_starts_with_dsit(valid_identifier)).to be false | ||
end | ||
end | ||
|
||
describe "#fix_activity" do | ||
let(:activity) do | ||
create( | ||
:programme_activity, | ||
transparency_identifier: "GB-GOV-26-AAAA-BBBB-CCC-DDDDDDD", | ||
previous_identifier: "GB-GOV-13-AAAA-BBBB-CCC-DDDDDDD", | ||
hybrid_beis_dsit_activity: false | ||
) | ||
end | ||
|
||
it "updates the transparency_identifier to the value of previous_identifier" do | ||
described_class.new.fix_activity(activity) | ||
|
||
expect(activity.transparency_identifier).to eql "GB-GOV-13-AAAA-BBBB-CCC-DDDDDDD" | ||
end | ||
|
||
it "updates the previous_identifier to nil" do | ||
described_class.new.fix_activity(activity) | ||
|
||
expect(activity.previous_identifier).to be_nil | ||
end | ||
|
||
it "updates the hybrid BEIS DSIT state to true" do | ||
described_class.new.fix_activity(activity) | ||
|
||
expect(activity.hybrid_beis_dsit_activity).to be true | ||
end | ||
end | ||
end |