-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate contact's metadata into native columns #1670
Conversation
|
||
# EWS Cambodia | ||
contact.metadata.fetch("commune_ids", []).each do | commune_id | | ||
commune = Pumi::Commune.find_by_id(phone_call_metadata(:commune_code)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line might return nil. I think there will be some invalid communes in there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, it's was not assigned by the user, but still there are some invalid codes? We will add a check to skip it.
Account.find_each do |account| | ||
puts "Migrating contacts for account: #{account.id}" | ||
|
||
account.contacts.find_each do |contact| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
account.contacts.where(iso_country_code: nil)
This will allow us to run it multiple times in case of an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. We can say if iso_country_code
has the value, we already migrated or they created from the new codes.
contact.metadata.fetch("commune_ids", []).each do | commune_id | | ||
commune = Pumi::Commune.find_by_id(phone_call_metadata(:commune_code)) | ||
|
||
contact.addresses.find_or_create_by!( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a unique index on [beneficiary_id, iso_region_code, administrative_division_level_2_code, administrative_division_level_3_code, administrative_division_level_4_code]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also thought about that too, if we add the index, we should also at least add not null on iso_region_code
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After I thought on it again, we shouldn't add or reply on this because some of those columns can be null.
It is possible a contact has iso_region_code, administrative_division_level_2_code, administrative_division_level_3_name instead, so create_or_find_by
may not work.
commune = Pumi::Commune.find_by_id(phone_call_metadata(:commune_code)) | ||
|
||
contact.addresses.find_or_create_by!( | ||
iso_region_code: commune.province.iso3166_2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember we discussed about denormalizing iso_country_code
in the addresses table, but I can't remember did we decide to do it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we discussed and just leave them for now, but I couldn't remember 100% as well 😂
# metadata: {"name"=>"John Doe", "district"=>"Kalabo", "language"=>"Lozi", "province"=>"Western"} | ||
|
||
# PIN Zambia | ||
# metadata: {"name"=>"John Doe", "address"=>"Newa", "district"=>"Nalolo", "facility"=>"Mouyo", "language"=>"silozi", "province"=>"Western", "date_of_registration"=>"29/10/2021"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
language_code = "loz" # # https://en.wikipedia.org/wiki/Lozi_language
iso_region_code = "ZM-01" # https://en.wikipedia.org/wiki/ISO_3166-2:ZM
administrative_division_level_2_name = "Nalolo"
administrative_division_level_3_name = "Mouyo"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For language_code
, currently we don't have any validations.
For iso_region_code
, I will pull it from there to lookup the name into the code.
See #1651