add upsert support for geoip imports to prevent duplicates

This commit is contained in:
Graham McIntire 2026-01-28 13:37:02 -06:00
parent b341f89332
commit 234ff7cbd2
3 changed files with 22 additions and 20 deletions

View file

@ -113,7 +113,13 @@ defmodule ToweropsWeb.Api.V1.GeoipController do
}
end)
{inserted, _} = Repo.insert_all(Location, entries)
# Use upsert to avoid duplicates - replace existing records with same geoname_id
{inserted, _} =
Repo.insert_all(Location, entries,
on_conflict: {:replace_all_except, [:inserted_at]},
conflict_target: :geoname_id
)
{:ok, inserted}
rescue
e ->
@ -144,7 +150,13 @@ defmodule ToweropsWeb.Api.V1.GeoipController do
}
end)
{inserted, _} = Repo.insert_all(Block, entries)
# Use upsert to avoid duplicates - replace existing records with same network
{inserted, _} =
Repo.insert_all(Block, entries,
on_conflict: {:replace_all_except, [:id, :inserted_at]},
conflict_target: :network
)
{:ok, inserted}
rescue
e ->

View file

@ -0,0 +1,8 @@
defmodule Towerops.Repo.Migrations.AddUniqueConstraintToGeoipBlocks do
use Ecto.Migration
def change do
# Add unique index on network field to prevent duplicate CIDR blocks
create unique_index(:geoip_blocks, [:network])
end
end

View file

@ -3,24 +3,6 @@ defmodule Oban.Web.Components.Icons do
# Helpers
attr :rest, :global,
default: %{
"aria-hidden": "true",
class: "w-4 h-4",
fill: "currentColor",
viewBox: "0 0 16 16"
}
slot :inner_block, required: true
defp svg_mini(assigns) do
~H"""
<svg {@rest}>
{render_slot(@inner_block)}
</svg>
"""
end
attr :rest, :global,
default: %{
"stroke-width": "1.5",