Skip to content

Commit

Permalink
Make media subscriptions unique
Browse files Browse the repository at this point in the history
  • Loading branch information
milesmcc committed Nov 20, 2023
1 parent 22a982b commit 1d85185
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Platform.Repo.Migrations.MakeSubscriptionsUnique do
use Ecto.Migration

def change do
# Delete all non-unique media_subscriptions
execute """
WITH CTE AS (
SELECT
id,
ROW_NUMBER() OVER (PARTITION BY user_id, media_id ORDER BY inserted_at) as rn
FROM
media_subscriptions
)
DELETE FROM
media_subscriptions
WHERE
id IN (SELECT id FROM CTE WHERE rn > 1)
"""

create unique_index(:media_subscriptions, [:user_id, :media_id])
end
end

0 comments on commit 1d85185

Please sign in to comment.