-
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
Changes from 1 commit
ee6bb4d
2d5e6d0
34879c6
8f4c159
48a58fa
a549c32
4c91377
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace :data_migrations do | ||
task migrate_contact_metadata_to_native_fields: :environment do | ||
Account.find_each do |account| | ||
puts "Migrating contacts for account: #{account.id}" | ||
|
||
account.contacts.find_each do |contact| | ||
ApplicationRecord.transaction do | ||
contact.iso_country_code = PhonyRails.country_from_number(contact.msisdn) | ||
contact.language_code = contact.metadata["language_code"] | ||
contact.save! | ||
|
||
# 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 commentThe 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 commentThe 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. |
||
|
||
contact.addresses.find_or_create_by!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have a unique index on There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
iso_region_code: commune.province.iso3166_2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember we discussed about denormalizing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😂 |
||
administrative_division_level_2_code: commune.district_id, | ||
administrative_division_level_3_code: commune.id | ||
) | ||
end | ||
|
||
# EWS Laos | ||
contact.metadata.fetch("registered_districts", []).each do | district_code | | ||
district = CallFlowLogic::EWSLaosRegistration::DISTRICTS.find { |d| d.code == district_code } | ||
|
||
contact.addresses.find_or_create_by!( | ||
iso_region_code: district.province.iso3166, | ||
samnang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
administrative_division_level_2_code: district.code, | ||
) | ||
end | ||
|
||
# Africa's Voices (Somalia) | ||
# 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. For For |
||
end | ||
end | ||
end | ||
end | ||
end |
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 errorThere 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.