diff options
author | Struan Donald <struan@exo.org.uk> | 2020-08-14 14:33:41 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2020-09-18 15:16:40 +0100 |
commit | 0db97450c94e955a49dc5d3ab975a22ceccdd274 (patch) | |
tree | 046141f300a9d0accce43adbb5de02eec5837825 | |
parent | 2a78b728a2573d3494289c5967788472bc46c574 (diff) |
[Northamptonshire] script to update external ids
The migration from V1 to V2 changes the IDs so take a CSV of the changes
and apply it, saving the old id just in case.
-rwxr-xr-x | bin/northamptonshire/update_ids_for_v2 | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/bin/northamptonshire/update_ids_for_v2 b/bin/northamptonshire/update_ids_for_v2 new file mode 100755 index 000000000..eac52b938 --- /dev/null +++ b/bin/northamptonshire/update_ids_for_v2 @@ -0,0 +1,59 @@ +#!/usr/bin/env perl + +# The migration from Alloy V1 to V2 causes the IDs to change. This takes a CSV +# with a mapping of old ids to new ones and applies it to the external_id, saving +# the old one in extra in case it's relevant. + +use strict; +use warnings; +use v5.14; + +BEGIN { + use File::Basename qw(dirname); + use File::Spec; + my $d = dirname(File::Spec->rel2abs($0)); + require "$d/../../setenv.pl"; +} + +use FixMyStreet::DB; + +use Getopt::Long; +use Text::CSV; + +my $NEW_ID = 'alloy_item_id'; +my $OLD_ID = 'res_id'; + +my ($commit, $file); +GetOptions( + 'commit' => \$commit, + 'file=s' => \$file, +); + +if (!$commit) { + say "*** DRY RUN ***"; +} + +my $csv = Text::CSV->new({ binary => 1 }); +open my $fh, "<:encoding(utf-8)", $file or die "Failed to open $file: $!\n"; +$csv->header($fh); + +my $count = 0; +my $northants = FixMyStreet::DB->resultset("Body")->find({ name => 'Northamptonshire County Council' }); +if ($northants) { + while (my $report = $csv->getline_hr( $fh ) ) { + next unless $report->{$OLD_ID}; + my $p = FixMyStreet::DB->resultset('Problem')->to_body( $northants->id )->search({ external_id => $report->{$OLD_ID} })->first; + next unless $p; + + if ($commit) { + $p->set_extra_metadata('old_external_id', $p->external_id); + $p->external_id($report->{$NEW_ID}); + $p->update; + } + $count++; + } + + say "updated $count rows"; +} else { + say STDERR "Could not find Northamptonshire"; +} |