From b4eac06227b2a16159330cc71def916280e18551 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Tue, 11 Feb 2025 10:30:25 +0800 Subject: [PATCH] refactor: streamline geo data syncing process - Remove unnecessary debug output for country syncing. - Improve country determination logic by adding checks for both country code and country name. - Ensure state records are associated with the correct country ID post-refactor. These changes enhance the clarity and efficiency of the geo data synchronization code, making it easier to maintain and reducing the risk of errors during data syncing. --- lib/tasks/sync_geo_data.rake | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/tasks/sync_geo_data.rake b/lib/tasks/sync_geo_data.rake index 482e191..ae5204e 100644 --- a/lib/tasks/sync_geo_data.rake +++ b/lib/tasks/sync_geo_data.rake @@ -63,7 +63,6 @@ namespace :geo do count = 1 countries.each do |data| - # puts "Syncing countries for #{data["name"]}" # 处理 Region region = if data["region_id"] Region.find_by(id: data["region_id"]) @@ -125,12 +124,22 @@ namespace :geo do puts "Syncing State[#{count}/#{sum}] [#{data["name"]}] country:[#{data["country_name"]}]" count += 1 + country = + if data["country_code"] + Region.find_by(id: data["country_code"]) + elsif data["country_name"] + Region.find_by(name: data["country_name"]) + else + puts "Skip states #{data["name"]} without country" + next + end + state = State.find_or_create_by!(name: data["name"]) do |s| - s.country_id = data["country_id"] + s.country_id = country.id end state.update!( - country_code: data["country_code"], + country_code: country.code, fips_code: data["fips_code"], iso2: data["iso2"], code: data["state_code"],