fix: ensure creation and updating of geo data

- Replace `find_or_create_by` and `find_or_initialize_by` with `find_or_create_by!` and `find_or_initialize_by!` for better error handling.
- Use the `update!` method instead of `update` to raise exceptions on failure.
- These changes improve the reliability of geographic data synchronization by ensuring failures are not silently ignored and facilitate easier troubleshooting.
This commit is contained in:
songtianlun 2025-02-11 09:48:54 +08:00
parent 29ad6be241
commit d3dae4f079

View File

@ -22,11 +22,11 @@ namespace :geo do
count = 1 count = 1
regions.each do |data| regions.each do |data|
region = Region.find_or_create_by(name: data["name"]) region = Region.find_or_create_by!(name: data["name"])
puts "Sync Regions[#{count}/#{sum}]: [#{region.name}]" puts "Sync Regions[#{count}/#{sum}]: [#{region.name}]"
count += 1 count += 1
region.update( region.update!(
code: data["name"], code: data["name"],
translations: data["translations"], translations: data["translations"],
flag: data["flag"] || true, flag: data["flag"] || true,
@ -42,13 +42,13 @@ namespace :geo do
count = 1 count = 1
subregions.each do |data| subregions.each do |data|
subregion = Subregion.find_or_initialize_by(name: data["name"]) do |s| subregion = Subregion.find_or_create_by!(name: data["name"]) do |s|
s.region_id = data["region_id"] s.region_id = data["region_id"]
end end
puts "Sync Subregions[#{count}/#{sum}]: [#{subregion.name}]" puts "Sync Subregions[#{count}/#{sum}]: [#{subregion.name}]"
count += 1 count += 1
subregion.update( subregion.update!(
translations: data["translations"], translations: data["translations"],
flag: data["flag"] || true, flag: data["flag"] || true,
wiki_data_id: data["wikiDataId"] wiki_data_id: data["wikiDataId"]
@ -82,13 +82,11 @@ namespace :geo do
count += 1 count += 1
# 查找或初始化 Country # 查找或初始化 Country
country = Country.find_by(code: data["iso2"]) country = Country.find_or_create_by!(code: data["iso2"], name: data["name"])
if country.nil?
country = Country.new
end
# 更新 Country 属性 # 更新 Country 属性
country.update( country.update!(
name: data["name"],
iso3: data["iso3"], iso3: data["iso3"],
numeric_code: data["numeric_code"], numeric_code: data["numeric_code"],
iso2: data["iso2"], iso2: data["iso2"],
@ -110,8 +108,8 @@ namespace :geo do
flag: data["flag"] || true, flag: data["flag"] || true,
wiki_data_id: data["wikiDataId"] wiki_data_id: data["wikiDataId"]
) )
country.update(region: region) if region != nil country.update!(region: region) if region != nil
country.update(subregion: subregion) if subregion != nil country.update!(subregion: subregion) if subregion != nil
end end
end end
@ -125,11 +123,11 @@ namespace :geo do
puts "Syncing State[#{count}/#{sum}] [#{data["name"]}] " puts "Syncing State[#{count}/#{sum}] [#{data["name"]}] "
count += 1 count += 1
state = State.find_or_initialize_by(name: data["name"]) do |s| state = State.find_or_initialize_by!(name: data["name"]) do |s|
s.country_id = data["country_id"] s.country_id = data["country_id"]
end end
state.update( state.update!(
country_code: data["country_code"], country_code: data["country_code"],
fips_code: data["fips_code"], fips_code: data["fips_code"],
iso2: data["iso2"], iso2: data["iso2"],
@ -152,7 +150,7 @@ namespace :geo do
count = 1 count = 1
cities.each do |data| cities.each do |data|
city = City.find_or_initialize_by(name: data["name"]) city = City.find_or_initialize_by!(name: data["name"])
country = Country.find_by(name: data["country_name"]) country = Country.find_by(name: data["country_name"])
state = State.find_by(name: data["state_name"]) state = State.find_by(name: data["state_name"])
@ -160,7 +158,7 @@ namespace :geo do
puts "Syncing City[#{count}/#{sum}] [#{data["name"]}] Country:[#{country&.name}] " puts "Syncing City[#{count}/#{sum}] [#{data["name"]}] Country:[#{country&.name}] "
count += 1 count += 1
city.update( city.update!(
latitude: data["latitude"], latitude: data["latitude"],
longitude: data["longitude"], longitude: data["longitude"],
flag: data["flag"] || true, flag: data["flag"] || true,
@ -172,10 +170,10 @@ namespace :geo do
country_code: country.code, country_code: country.code,
) )
end end
city.update(state: state) unless state == nil city.update!(state: state) unless state == nil
city.update(state_code: state.code) unless state == nil city.update!(state_code: state.code) unless state == nil
city.update(active: false) if city.active == nil city.update!(active: false) if city.active == nil
city.update(priority: 10) if city.priority == nil city.update!(priority: 10) if city.priority == nil
end end
end end
end end