Skip to content

Commit

Permalink
Merge pull request #968 from alphagov/allow-gss
Browse files Browse the repository at this point in the history
Add task to create duplicate datasets with gss codes
  • Loading branch information
KludgeKML authored Oct 24, 2023
2 parents 9ad13a9 + 5b2f0ab commit 820c475
Show file tree
Hide file tree
Showing 2 changed files with 450 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/tasks/once_off/convert_snac_to_gss.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace :once_off do
desc "Creates duplicate datasets with GSS codes instead of SNACs for local authority data_sets"
task convert_snac_to_gss: [:environment] do
snac_to_gss_lookup = load_snac_to_gss_lookup

affected_services.each do |service|
puts("INFO: Updating Service: #{service.slug}")
duplicate = service.active_data_set.duplicate

duplicate.change_notes += ", then updated by once_off:convert_snac_to_gss task"
duplicate.save!

duplicate.places.each do |place|
next if already_gss?(place.gss)

snac = place.gss
gss = snac_to_gss_lookup[snac]
if gss.nil?
puts(" - ERROR: Failed to update SNAC #{snac} for #{service.slug}")
else
place.gss = gss
place.save!
end
end
end
end
end

def load_snac_to_gss_lookup
lookup_cache = {}
CSV.foreach("lib/tasks/once_off/snac_to_gss_translation.csv") do |row|
lookup_cache[row[0]] = row[1]
end
lookup_cache
end

def affected_services
Service.where(location_match_type: "local_authority")
end

def already_gss?(code)
code.length > 4
end
Loading

0 comments on commit 820c475

Please sign in to comment.