fix: wrap Gaiia sync upserts in transaction and remove stale device_role_source ref (#118)

- Wrap upsert_nodes in Repo.transaction so all upserts share one DB
  connection, preventing pool exhaustion under load
- Remove stale device_role_source reference from form test

Reviewed-on: graham/towerops-web#118
This commit is contained in:
Graham McIntire 2026-03-22 16:47:17 -05:00 committed by graham
parent 1ecc05add4
commit 7d24e56bd9
2 changed files with 15 additions and 4 deletions

View file

@ -11,6 +11,7 @@ defmodule Towerops.Gaiia.Sync do
alias Towerops.Gaiia.Client
alias Towerops.Gaiia.SubscriberMatching
alias Towerops.Integrations
alias Towerops.Repo
require Logger
@ -57,13 +58,24 @@ defmodule Towerops.Gaiia.Sync do
end
defp upsert_nodes(org_id, nodes, type) do
case Repo.transaction(fn -> do_upsert_nodes(org_id, nodes, type) end) do
{:ok, count} ->
count
{:error, reason} ->
Logger.error("Gaiia #{type} upsert transaction failed: #{inspect(reason)}")
0
end
end
defp do_upsert_nodes(org_id, nodes, type) do
Enum.reduce(nodes, 0, fn node, count ->
case upsert_node(org_id, node, type) do
{:ok, _} ->
count + 1
{:error, changeset} ->
Logger.warning("Failed to upsert Gaiia #{type}: #{inspect(changeset.errors)}")
{:error, cs} ->
Logger.warning("Failed to upsert Gaiia #{type}: #{inspect(cs.errors)}")
count
end
end)

View file

@ -336,8 +336,7 @@ defmodule ToweropsWeb.DeviceLive.FormTest do
site_id: site.id,
organization_id: organization.id,
snmp_enabled: true,
device_role: "router",
device_role_source: "manual"
device_role: "router"
})
{:ok, _view, html} = live(conn, ~p"/devices/#{device.id}/edit")