diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex index 114214920..2f78d1291 100644 --- a/lib/plausible/site.ex +++ b/lib/plausible/site.ex @@ -57,6 +57,10 @@ defmodule Plausible.Site do |> unique_constraint(:domain, message: @domain_unique_error ) + |> unique_constraint(:domain, + name: "domain_change_disallowed", + message: @domain_unique_error + ) end def update_changeset(site, attrs \\ %{}, opts \\ []) do diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index 312126564..19e28b7e9 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -284,6 +284,23 @@ defmodule PlausibleWeb.SiteControllerTest do assert html_response(conn, 200) =~ "This domain cannot be registered. Perhaps one of your colleagues registered it?" end + + test "renders form again when domain was changed from elsewhere", %{conn: conn} do + :site + |> insert(domain: "example.com") + |> Plausible.Site.Domain.change("new.example.com") + + conn = + post(conn, "/sites", %{ + "site" => %{ + "domain" => "example.com", + "timezone" => "Europe/London" + } + }) + + assert html_response(conn, 200) =~ + "This domain cannot be registered. Perhaps one of your colleagues registered it?" + end end describe "GET /:website/snippet" do