Skip to content

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

Migration: allow domain change within the same team (#6477)

Adam Rutkowski5fb4f6175ee1
diff --git a/priv/repo/migrations/20260625000000_allow_same_team_domain_swap.exs b/priv/repo/migrations/20260625000000_allow_same_team_domain_swap.exs
new file mode 100644
index 0000000000..dbd01ead8e
--- /dev/null
+++ b/priv/repo/migrations/20260625000000_allow_same_team_domain_swap.exs
@@ -0,0 +1,37 @@
+defmodule Plausible.Repo.Migrations.AllowSameTeamDomainSwap do
+ use Ecto.Migration
+
+ def up do
+ execute("""
+ CREATE OR REPLACE FUNCTION check_domain() RETURNS TRIGGER AS $$
+ BEGIN
+ IF EXISTS (
+ SELECT 1 FROM sites
+ WHERE (NEW.domain = domain_changed_from AND NEW.id != id AND (team_id IS NULL OR NEW.team_id IS NULL OR team_id != NEW.team_id))
+ OR (OLD IS NULL AND NEW.domain_changed_from = domain)
+ ) THEN
+ RAISE unique_violation USING CONSTRAINT = 'domain_change_disallowed';
+ END IF;
+ RETURN NEW;
+ END;
+ $$ LANGUAGE plpgsql;
+ """)
+ end
+
+ def down do
+ execute("""
+ CREATE OR REPLACE FUNCTION check_domain() RETURNS TRIGGER AS $$
+ BEGIN
+ IF EXISTS (
+ SELECT 1 FROM sites
+ WHERE (NEW.domain = domain_changed_from AND NEW.id != id)
+ OR (OLD IS NULL AND NEW.domain_changed_from = domain)
+ ) THEN
+ RAISE unique_violation USING CONSTRAINT = 'domain_change_disallowed';
+ END IF;
+ RETURN NEW;
+ END;
+ $$ LANGUAGE plpgsql;
+ """)
+ end
+end