Skip to content
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

Merged
merged 7 commits into from
Jan 8, 2025

Conversation

samnang
Copy link
Member

@samnang samnang commented Dec 18, 2024

See #1651


# EWS Cambodia
contact.metadata.fetch("commune_ids", []).each do | commune_id |
commune = Pumi::Commune.find_by_id(phone_call_metadata(:commune_code))
Copy link
Collaborator

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

Copy link
Member Author

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|
Copy link
Collaborator

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

Copy link
Member Author

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!(
Copy link
Collaborator

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]?

Copy link
Member Author

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.

Copy link
Member Author

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,
Copy link
Collaborator

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?

Copy link
Member Author

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"}
Copy link
Collaborator

@dwilkie dwilkie Dec 18, 2024

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"

Copy link
Member Author

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.

@samnang samnang linked an issue Dec 30, 2024 that may be closed by this pull request
@samnang samnang merged commit ab53653 into develop Jan 8, 2025
7 checks passed
@samnang samnang deleted the contact-data-migrations branch January 8, 2025 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Beneficiaries Data Migration
2 participants