Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Enforce goals unique (#3343)
hq18c605865b643
diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.exindex 547bc8a7fd..d4dee4a6c1 100644--- a/lib/plausible/goal/schema.ex+++ b/lib/plausible/goal/schema.ex@@ -42,6 +42,8 @@ defmodule Plausible.Goal do|> validate_event_name_and_page_path()|> update_change(:event_name, &String.trim/1)|> update_change(:page_path, &String.trim/1)+ |> unique_constraint(:event_name, name: :goals_event_name_unique)+ |> unique_constraint(:page_path, name: :goals_page_path_unique)|> validate_length(:event_name, max: 120)|> check_constraint(:event_name,name: :check_event_name_or_page_path,diff --git a/priv/repo/migrations/20230914071245_goals_unique.exs b/priv/repo/migrations/20230914071245_goals_unique.exsnew file mode 100644index 0000000000..61e4abb06a--- /dev/null+++ b/priv/repo/migrations/20230914071245_goals_unique.exs@@ -0,0 +1,50 @@+defmodule Plausible.Repo.Migrations.GoalsUnique do+ use Ecto.Migration++ @disable_ddl_transaction true+ @disable_migration_lock true++ def up do+ execute """+ DELETE+ FROM+ goals g+ WHERE+ g.id NOT IN (+ SELECT+ min(g2.id)+ FROM+ goals g2+ GROUP BY+ (g2.site_id,+ CASE+ WHEN g2.page_path IS NOT NULL THEN g2.page_path+ WHEN g2.event_name IS NOT NULL THEN g2.event_name+ END )+ )+ AND g.id NOT IN (+ SELECT fs.goal_id+ FROM funnel_steps fs+ );+ """++ create(+ unique_index(:goals, [:site_id, :event_name],+ where: "event_name IS NOT NULL",+ name: :goals_event_name_unique+ )+ )++ create(+ unique_index(:goals, [:site_id, :page_path],+ where: "page_path IS NOT NULL",+ name: :goals_page_path_unique+ )+ )+ end++ def down do+ drop(unique_index(:goals, [:user_id, :event_name], name: :goals_event_name_unique))+ drop(unique_index(:goals, [:user_id, :page_path], name: :goals_page_path_unique))+ end+enddiff --git a/test/plausible/data_migration/rewrite_funnel_dupes_test.exs b/test/plausible/data_migration/rewrite_funnel_dupes_test.exsindex d9ad24ae0a..5938dac4b3 100644--- a/test/plausible/data_migration/rewrite_funnel_dupes_test.exs+++ b/test/plausible/data_migration/rewrite_funnel_dupes_test.exs@@ -6,6 +6,7 @@ defmodule Plausible.DataMigration.RewriteFunnelDupesTest doimport ExUnit.CaptureIOfor goal_type <- ["event_name", "page_path"] do+ @tag :skiptest "deletes a funnel that cannot be cleaned up from dupe goals (#{goal_type})" dosite = insert(:site)@@ -25,6 +26,7 @@ defmodule Plausible.DataMigration.RewriteFunnelDupesTest dorefute Repo.reload(funnel)end+ @tag :skiptest "reduces a funnel if possible (#{goal_type})" dosite = insert(:site)@@ -50,6 +52,7 @@ defmodule Plausible.DataMigration.RewriteFunnelDupesTest doendend+ @tag :skiptest "dupe names in mixed goal types don't matter" dosite = insert(:site)@@ -69,6 +72,7 @@ defmodule Plausible.DataMigration.RewriteFunnelDupesTest doassert_funnel(site.id, funnel.id, gs)end+ @tag :skiptest "goals across multiple funnels" dosite = insert(:site)diff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exsindex daeebff4af..6fdba92d3a 100644--- a/test/plausible/goals_test.exs+++ b/test/plausible/goals_test.exs@@ -24,6 +24,20 @@ defmodule Plausible.GoalsTest doassert {"should be at most %{count} character(s)", _} = changeset.errors[:event_name]end+ test "create/2 fails to create the same pageview goal twice" do+ site = insert(:site)+ {:ok, _} = Goals.create(site, %{"page_path" => "foo bar"})+ assert {:error, changeset} = Goals.create(site, %{"page_path" => "foo bar"})+ assert {"has already been taken", _} = changeset.errors[:page_path]+ end++ test "create/2 fails to create the same custom event goal twice" do+ site = insert(:site)+ {:ok, _} = Goals.create(site, %{"event_name" => "foo bar"})+ assert {:error, changeset} = Goals.create(site, %{"event_name" => "foo bar"})+ assert {"has already been taken", _} = changeset.errors[:event_name]+ end+test "create/2 sets site.updated_at for revenue goal" dosite_1 = insert(:site, updated_at: DateTime.add(DateTime.utc_now(), -3600)){:ok, _goal_1} = Goals.create(site_1, %{"event_name" => "Checkout", "currency" => "BRL"})@@ -170,7 +184,7 @@ defmodule Plausible.GoalsTest doassert Enum.count(f1.steps) == 2refute Plausible.Funnels.get(site.id, f2.id)- assert Repo.all(from fs in Plausible.Funnel.Step, where: fs.funnel_id == ^f2.id) == []+ assert Repo.all(from(fs in Plausible.Funnel.Step, where: fs.funnel_id == ^f2.id)) == []assert [^g3, ^g2] = Goals.for_site(site)end