Find supernets, don't create them

This commit is contained in:
Dan Milne
2025-12-27 11:56:19 +11:00
parent 108caf2fe6
commit e53e782223
2 changed files with 41 additions and 56 deletions

View File

@@ -136,33 +136,6 @@ class NetworkRange < ApplicationRecord
.order("masklen(network) ASC") # Least specific child first
end
# Find or create an ancestor network at a specific prefix length
# For example, given 192.168.1.0/24 and prefix 16, returns 192.168.0.0/16
def find_or_create_ancestor_at_prefix(target_prefix)
return self if prefix_length <= target_prefix
# Use PostgreSQL's set_masklen to create the ancestor CIDR
result = self.class.connection.execute(
"SELECT set_masklen('#{network}'::inet, #{target_prefix})::text as ancestor_cidr"
).first
return self unless result
ancestor_cidr = result["ancestor_cidr"]
return self if ancestor_cidr == cidr
# Find or create the ancestor network range
ancestor = NetworkRange.find_by(network: ancestor_cidr)
if ancestor.nil?
# Create a virtual ancestor (not persisted, just for reference)
# The caller can decide whether to persist it
ancestor = NetworkRange.new(network: ancestor_cidr, source: 'inherited')
end
ancestor
end
# Find nearest parent with intelligence data
def parent_with_intelligence
# Find all parent ranges (networks that contain this network)