Use unique index to prevent duplicate items from being created

This commit is contained in:
Shadowfacts 2021-09-08 09:35:46 -04:00
parent cd36b40978
commit a02ec174be
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 14 additions and 4 deletions

View File

@ -83,5 +83,10 @@ defmodule Frenzy.Item do
item item
|> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date, :tombstone]) |> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date, :tombstone])
|> validate_required([:guid, :url, :date, :content, :feed]) |> validate_required([:guid, :url, :date, :content, :feed])
|> unique_constraint(:items_feed_guid_index)
end
def exists?(feed_id, guid) do
Frenzy.Repo.exists?(__MODULE__, feed_id: feed_id, guid: guid)
end end
end end

View File

@ -62,7 +62,7 @@ defmodule Frenzy.Task.CreateItem do
end end
case result do case result do
{:err, error} -> {:error, error} ->
Logger.error(error) Logger.error(error)
{:ok, item_params} -> {:ok, item_params} ->

View File

@ -158,11 +158,9 @@ defmodule Frenzy.UpdateFeeds do
FetchFavicon.run(feed) FetchFavicon.run(feed)
end end
feed = Repo.preload(feed, [:items])
Enum.each(rss.items, fn entry -> Enum.each(rss.items, fn entry ->
# todo: use Repo.exists for this # todo: use Repo.exists for this
if !Enum.any?(feed.items, fn item -> item.guid == entry.guid end) do unless Item.exists?(feed.id, entry.guid) do
CreateItem.start_link(feed, entry) CreateItem.start_link(feed, entry)
end end
end) end)

View File

@ -0,0 +1,7 @@
defmodule Frenzy.Repo.Migrations.CreatItemUniqueIndex do
use Ecto.Migration
def change do
create unique_index(:items, [:feed_id, :guid], name: :items_feed_guid_index)
end
end