mirror of
https://github.com/ClaperCo/Claper.git
synced 2026-08-29 01:58:48 +02:00
Resolve duplicate active user emails
This commit is contained in:
@@ -3,6 +3,21 @@ defmodule Claper.Repo.Migrations.AllowReusingDeletedUserEmails do
|
||||
|
||||
def change do
|
||||
drop_if_exists unique_index(:users, [:email])
|
||||
|
||||
execute """
|
||||
WITH ranked_users AS (
|
||||
SELECT
|
||||
id,
|
||||
ROW_NUMBER() OVER (PARTITION BY email ORDER BY inserted_at, id) AS email_position
|
||||
FROM users
|
||||
WHERE deleted_at IS NULL
|
||||
)
|
||||
UPDATE users
|
||||
SET deleted_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP
|
||||
FROM ranked_users
|
||||
WHERE users.id = ranked_users.id AND ranked_users.email_position > 1
|
||||
"""
|
||||
|
||||
create unique_index(:users, [:email], where: "deleted_at IS NULL")
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user