Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Cascade site_membership deletion

Uku Tahtbc44879bf85d
diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex
index fdce9aeaaf..144629dedd 100644
--- a/lib/plausible_web/controllers/auth_controller.ex
+++ b/lib/plausible_web/controllers/auth_controller.ex
@@ -329,10 +329,6 @@ defmodule PlausibleWeb.AuthController do
|> Repo.preload(:sites)
|> Repo.preload(:subscription)
- for site_membership <- user.site_memberships do
- Repo.delete!(site_membership)
- end
-
for site <- user.sites do
Repo.delete!(site)
end
diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex
index 505e1fe972..da3384387b 100644
--- a/lib/plausible_web/controllers/site_controller.ex
+++ b/lib/plausible_web/controllers/site_controller.ex
@@ -297,15 +297,7 @@ defmodule PlausibleWeb.SiteController do
end
def delete_site(conn, %{"website" => website}) do
- site =
- Sites.get_for_user!(conn.assigns[:current_user].id, website)
- |> Repo.preload(:google_auth)
-
- Repo.delete_all(from sm in "site_memberships", where: sm.site_id == ^site.id)
-
- if site.google_auth do
- Repo.delete!(site.google_auth)
- end
+ site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
Repo.delete!(site)
Plausible.ClickhouseRepo.clear_stats_for(site.domain)
diff --git a/priv/repo/migrations/20210128083453_cascade_site_deletion.exs b/priv/repo/migrations/20210128083453_cascade_site_deletion.exs
new file mode 100644
index 0000000000..b7d2e2eb13
--- /dev/null
+++ b/priv/repo/migrations/20210128083453_cascade_site_deletion.exs
@@ -0,0 +1,11 @@
+defmodule Plausible.Repo.Migrations.CascadeSiteDeletion do
+ use Ecto.Migration
+
+ def change do
+ drop constraint("site_memberships", "site_memberships_site_id_fkey")
+
+ alter table(:site_memberships) do
+ modify :site_id, references(:sites, on_delete: :delete_all), null: false
+ end
+ end
+end