[pg migration] Delete account with multiple subscriptions (#3220)
* Migrate user deletion So that subscriptions, google * Make CRM deletion functional * Preload subscriptions in an uniform mannerpull/3227/head
parent
92a7e01df5
commit
054d0655af
@ -0,0 +1,36 @@
|
||||
defmodule Plausible.Users do
|
||||
@moduledoc """
|
||||
User context
|
||||
"""
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias Plausible.Auth.User
|
||||
alias Plausible.Billing.Subscription
|
||||
alias Plausible.Repo
|
||||
|
||||
def with_subscription(%User{id: user_id} = user) do
|
||||
Repo.preload(user, subscription: last_subscription_query(user_id))
|
||||
end
|
||||
|
||||
def with_subscription(user_id) when is_integer(user_id) do
|
||||
Repo.one(
|
||||
from(user in User,
|
||||
left_join: last_subscription in subquery(last_subscription_query(user_id)),
|
||||
on: last_subscription.user_id == user.id,
|
||||
left_join: subscription in Subscription,
|
||||
on: subscription.id == last_subscription.id,
|
||||
where: user.id == ^user_id,
|
||||
preload: [subscription: subscription]
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
defp last_subscription_query(user_id) do
|
||||
from(subscription in Plausible.Billing.Subscription,
|
||||
where: subscription.user_id == ^user_id,
|
||||
order_by: [desc: subscription.inserted_at],
|
||||
limit: 1
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,43 @@
|
||||
defmodule Plausible.Repo.Migrations.CascadeDeleteUser do
|
||||
use Ecto.Migration
|
||||
|
||||
def up do
|
||||
drop(constraint(:subscriptions, "subscriptions_user_id_fkey"))
|
||||
|
||||
alter table(:subscriptions) do
|
||||
modify(:user_id, references(:users, on_delete: :delete_all), null: false)
|
||||
end
|
||||
|
||||
drop(constraint(:site_memberships, "site_memberships_user_id_fkey"))
|
||||
|
||||
alter table(:site_memberships) do
|
||||
modify(:user_id, references(:users, on_delete: :delete_all), null: false)
|
||||
end
|
||||
|
||||
drop(constraint(:google_auth, "google_auth_user_id_fkey"))
|
||||
|
||||
alter table(:google_auth) do
|
||||
modify(:user_id, references(:users, on_delete: :delete_all), null: false)
|
||||
end
|
||||
end
|
||||
|
||||
def down do
|
||||
drop(constraint(:subscriptions, "subscriptions_user_id_fkey"))
|
||||
|
||||
alter table(:subscriptions) do
|
||||
modify(:user_id, references(:users), null: false)
|
||||
end
|
||||
|
||||
drop(constraint(:site_memberships, "site_memberships_user_id_fkey"))
|
||||
|
||||
alter table(:site_memberships) do
|
||||
modify(:user_id, references(:users), null: false)
|
||||
end
|
||||
|
||||
drop(constraint(:google_auth, "google_auth_user_id_fkey"))
|
||||
|
||||
alter table(:google_auth) do
|
||||
modify(:user_id, references(:users), null: false)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue