This commit is contained in:
Dan Milne
2025-11-14 16:35:49 +11:00
parent df94ac9720
commit 6433f6c5bb
30 changed files with 833 additions and 245 deletions

View File

@@ -211,7 +211,7 @@ class GeoliteCountryImporter
location_data = @locations_cache[geoname_id] || @locations_cache[registered_country_geoname_id] || {}
# Store raw GeoLite country data in network_data[:geolite]
geolite_data = {
geolite_country_data = {
country: {
geoname_id: geoname_id,
registered_country_geoname_id: registered_country_geoname_id,
@@ -227,16 +227,29 @@ class GeoliteCountryImporter
}
}.compact
# Use upsert with JSONB merge
# COALESCE handles the case where network_data might be NULL
# || is PostgreSQL's JSONB concatenation/merge operator
NetworkRange.upsert(
{
network: network,
country: location_data[:country_iso_code],
is_proxy: is_anonymous_proxy,
source: 'geolite_country',
network_data: { geolite: geolite_data },
network_data: { geolite: geolite_country_data },
updated_at: Time.current
},
unique_by: :index_network_ranges_on_network_unique
unique_by: :index_network_ranges_on_network_unique,
on_duplicate: Arel.sql("
country = EXCLUDED.country,
is_proxy = EXCLUDED.is_proxy,
network_data = COALESCE(network_ranges.network_data, '{}'::jsonb) ||
jsonb_build_object('geolite',
COALESCE(network_ranges.network_data->'geolite', '{}'::jsonb) ||
EXCLUDED.network_data->'geolite'
),
updated_at = EXCLUDED.updated_at
")
)
end