Skip to content

Commit

Permalink
Data migration
Browse files Browse the repository at this point in the history
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
mec committed Nov 25, 2024
1 parent 4eae5f2 commit f24f31b
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
67 changes: 67 additions & 0 deletions db/data/20241114082139_fix_hybrid_beis_dsit_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Run me with `rails runner db/data/20241114082139_fix_iati_reporting_org.rb`

# 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|
other_identifier_starts_with_beis(activity.previous_identifier) &&
transparency_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 other_identifier_starts_with_beis(other_identifier)
return if other_identifier.nil?

other_identifier.slice(0, 9).eql?("GB-GOV-13")
end

def transparency_identifier_starts_with_dsit(transparency_identifier)
return if transparency_identifier.nil?

transparency_identifier.slice(0, 9).eql?("GB-GOV-26")
end
end
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 "#other_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.other_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.other_identifier_starts_with_beis(valid_identifier)).to be false
end
end

describe "#transparency_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.transparency_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.transparency_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

0 comments on commit f24f31b

Please sign in to comment.