From a02ec174beb9a2a1037e30c87f3a8b6a3fa69ea8 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 8 Sep 2021 09:35:46 -0400 Subject: [PATCH] Use unique index to prevent duplicate items from being created --- lib/frenzy/item.ex | 5 +++++ lib/frenzy/task/create_item.ex | 2 +- lib/frenzy/update_feeds.ex | 4 +--- .../migrations/20210908133238_creat_item_unique_index.exs | 7 +++++++ 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 priv/repo/migrations/20210908133238_creat_item_unique_index.exs diff --git a/lib/frenzy/item.ex b/lib/frenzy/item.ex index 76212e5..2b8bf08 100644 --- a/lib/frenzy/item.ex +++ b/lib/frenzy/item.ex @@ -83,5 +83,10 @@ defmodule Frenzy.Item do item |> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date, :tombstone]) |> 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 diff --git a/lib/frenzy/task/create_item.ex b/lib/frenzy/task/create_item.ex index e654a2f..6d9b1bc 100644 --- a/lib/frenzy/task/create_item.ex +++ b/lib/frenzy/task/create_item.ex @@ -62,7 +62,7 @@ defmodule Frenzy.Task.CreateItem do end case result do - {:err, error} -> + {:error, error} -> Logger.error(error) {:ok, item_params} -> diff --git a/lib/frenzy/update_feeds.ex b/lib/frenzy/update_feeds.ex index 9898d04..c1f0dac 100644 --- a/lib/frenzy/update_feeds.ex +++ b/lib/frenzy/update_feeds.ex @@ -158,11 +158,9 @@ defmodule Frenzy.UpdateFeeds do FetchFavicon.run(feed) end - feed = Repo.preload(feed, [:items]) - Enum.each(rss.items, fn entry -> # 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) end end) diff --git a/priv/repo/migrations/20210908133238_creat_item_unique_index.exs b/priv/repo/migrations/20210908133238_creat_item_unique_index.exs new file mode 100644 index 0000000..07dd655 --- /dev/null +++ b/priv/repo/migrations/20210908133238_creat_item_unique_index.exs @@ -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