Change set_reachable/1 to an upsert
This commit is contained in:
parent
6a6a631c81
commit
1ad0d94d6f
|
@ -0,0 +1 @@
|
||||||
|
Reduce the reachability timestamp update to a single upsert query
|
|
@ -97,13 +97,9 @@ def reachable?(url_or_host) when is_binary(url_or_host) do
|
||||||
def reachable?(url_or_host) when is_binary(url_or_host), do: true
|
def reachable?(url_or_host) when is_binary(url_or_host), do: true
|
||||||
|
|
||||||
def set_reachable(url_or_host) when is_binary(url_or_host) do
|
def set_reachable(url_or_host) when is_binary(url_or_host) do
|
||||||
with host <- host(url_or_host),
|
%Instance{host: host(url_or_host)}
|
||||||
%Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
|
|> changeset(%{unreachable_since: nil})
|
||||||
{:ok, _instance} =
|
|> Repo.insert(on_conflict: {:replace, [:unreachable_since]}, conflict_target: :host)
|
||||||
existing_record
|
|
||||||
|> changeset(%{unreachable_since: nil})
|
|
||||||
|> Repo.update()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_reachable(_), do: {:error, nil}
|
def set_reachable(_), do: {:error, nil}
|
||||||
|
|
|
@ -31,14 +31,6 @@ test "keeps nil `unreachable_since` of existing matching Instance record having
|
||||||
assert {:ok, instance} = Instance.set_reachable(instance.host)
|
assert {:ok, instance} = Instance.set_reachable(instance.host)
|
||||||
refute instance.unreachable_since
|
refute instance.unreachable_since
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does NOT create an Instance record in case of no existing matching record" do
|
|
||||||
host = "domain.org"
|
|
||||||
assert nil == Instance.set_reachable(host)
|
|
||||||
|
|
||||||
assert [] = Repo.all(Ecto.Query.from(i in Instance))
|
|
||||||
assert Instance.reachable?(host)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "set_unreachable/1" do
|
describe "set_unreachable/1" do
|
||||||
|
|
Loading…
Reference in New Issue