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
Address team syncing discrepancies (#4739)
Adrian Gruntkowski27ac3b6b5bab
diff --git a/lib/plausible/data_migration/backfill_teams.ex b/lib/plausible/data_migration/backfill_teams.exindex 68647b3ad6..b6c04119b7 100644--- a/lib/plausible/data_migration/backfill_teams.ex+++ b/lib/plausible/data_migration/backfill_teams.ex@@ -17,7 +17,9 @@ defmodule Plausible.DataMigration.BackfillTeams doendend- def run() do+ def run(opts \\ []) do+ dry_run? = Keyword.get(opts, :dry_run?, true)+# Teams backfilldb_url =System.get_env(@@ -27,10 +29,10 @@ defmodule Plausible.DataMigration.BackfillTeams do@repo.start(db_url, pool_size: 2 * @max_concurrency)- backfill()+ backfill(dry_run?)end- defp backfill() do+ defp backfill(dry_run?) doorphaned_teams =from(t in Plausible.Teams.Team,@@ -45,9 +47,11 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(orphaned_teams)} orphaned teams...")- delete_orphaned_teams(orphaned_teams)+ if not dry_run? do+ delete_orphaned_teams(orphaned_teams)- log("Deleted orphaned teams")+ log("Deleted orphaned teams")+ endsites_without_teams =from(@@ -62,9 +66,11 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(sites_without_teams)} sites without teams...")- teams_count = backfill_teams(sites_without_teams)+ if not dry_run? do+ teams_count = backfill_teams(sites_without_teams)- log("Backfilled #{teams_count} teams.")+ log("Backfilled #{teams_count} teams.")+ endowner_site_memberships_query =from(@@ -90,9 +96,11 @@ defmodule Plausible.DataMigration.BackfillTeams do"Found #{length(users_with_subscriptions_without_sites)} users with subscriptions without sites...")- teams_count = backfill_teams_for_users(users_with_subscriptions_without_sites)+ if not dry_run? do+ teams_count = backfill_teams_for_users(users_with_subscriptions_without_sites)- log("Backfilled #{teams_count} teams from users with subscriptions without sites.")+ log("Backfilled #{teams_count} teams from users with subscriptions without sites.")+ end# Stale teams sync@@ -123,11 +131,13 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(stale_teams)} teams which have fields out of sync...")- sync_teams(stale_teams)+ if not dry_run? do+ sync_teams(stale_teams)- # Subsciprtions backfill+ log("Brought out of sync teams up to date.")+ end- log("Brought out of sync teams up to date.")+ # Subsciprtions backfillsubscriptions_without_teams =from(@@ -143,9 +153,11 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(subscriptions_without_teams)} subscriptions without team...")- backfill_subscriptions(subscriptions_without_teams)+ if not dry_run? do+ backfill_subscriptions(subscriptions_without_teams)- log("All subscriptions are linked to a team now.")+ log("All subscriptions are linked to a team now.")+ end# Enterprise plans backfill@@ -163,9 +175,11 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(enterprise_plans_without_teams)} enterprise plans without team...")- backfill_enterprise_plans(enterprise_plans_without_teams)+ if not dry_run? do+ backfill_enterprise_plans(enterprise_plans_without_teams)- log("All enterprise plans are linked to a team now.")+ log("All enterprise plans are linked to a team now.")+ end# Guest Memberships cleanup@@ -190,17 +204,19 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(guest_memberships_to_remove)} guest memberships to remove...")- team_ids_to_prune = remove_guest_memberships(guest_memberships_to_remove)+ if not dry_run? do+ team_ids_to_prune = remove_guest_memberships(guest_memberships_to_remove)- log("Pruning guest team memberships for #{length(team_ids_to_prune)} teams...")+ log("Pruning guest team memberships for #{length(team_ids_to_prune)} teams...")- from(t in Teams.Team, where: t.id in ^team_ids_to_prune)- |> @repo.all(timeout: :infinity)- |> Enum.each(fn team ->- Plausible.Teams.Memberships.prune_guests(team)- end)+ from(t in Teams.Team, where: t.id in ^team_ids_to_prune)+ |> @repo.all(timeout: :infinity)+ |> Enum.each(fn team ->+ Plausible.Teams.Memberships.prune_guests(team)+ end)- log("Guest memberships cleared.")+ log("Guest memberships cleared.")+ end# Guest Memberships backfill@@ -230,9 +246,11 @@ defmodule Plausible.DataMigration.BackfillTeams do"Found #{length(site_memberships_to_backfill)} site memberships without guest membership...")- backfill_guest_memberships(site_memberships_to_backfill)+ if not dry_run? do+ backfill_guest_memberships(site_memberships_to_backfill)- log("Backfilled missing guest memberships.")+ log("Backfilled missing guest memberships.")+ end# Stale guest memberships sync@@ -253,9 +271,11 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(stale_guest_memberships)} guest memberships with role out of sync...")- sync_guest_memberships(stale_guest_memberships)+ if not dry_run? do+ sync_guest_memberships(stale_guest_memberships)- log("All guest memberships are up to date now.")+ log("All guest memberships are up to date now.")+ end# Guest invitations cleanup@@ -281,17 +301,19 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(guest_invitations_to_remove)} guest invitations to remove...")- team_ids_to_prune = remove_guest_invitations(guest_invitations_to_remove)+ if not dry_run? do+ team_ids_to_prune = remove_guest_invitations(guest_invitations_to_remove)- log("Pruning guest team invitations for #{length(team_ids_to_prune)} teams...")+ log("Pruning guest team invitations for #{length(team_ids_to_prune)} teams...")- from(t in Teams.Team, where: t.id in ^team_ids_to_prune)- |> @repo.all(timeout: :infinity)- |> Enum.each(fn team ->- Plausible.Teams.Invitations.prune_guest_invitations(team)- end)+ from(t in Teams.Team, where: t.id in ^team_ids_to_prune)+ |> @repo.all(timeout: :infinity)+ |> Enum.each(fn team ->+ Plausible.Teams.Invitations.prune_guest_invitations(team)+ end)- log("Guest invitations cleared.")+ log("Guest invitations cleared.")+ end# Guest invitations backfill@@ -321,9 +343,11 @@ defmodule Plausible.DataMigration.BackfillTeams do"Found #{length(site_invitations_to_backfill)} site invitations without guest invitation...")- backfill_guest_invitations(site_invitations_to_backfill)+ if not dry_run? do+ backfill_guest_invitations(site_invitations_to_backfill)- log("Backfilled missing guest invitations.")+ log("Backfilled missing guest invitations.")+ end# Stale guest invitations sync@@ -344,9 +368,11 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(stale_guest_invitations)} guest invitations with role out of sync...")- sync_guest_invitations(stale_guest_invitations)+ if not dry_run? do+ sync_guest_invitations(stale_guest_invitations)- log("All guest invitations are up to date now.")+ log("All guest invitations are up to date now.")+ end# Site transfers cleanup@@ -368,9 +394,11 @@ defmodule Plausible.DataMigration.BackfillTeams dolog("Found #{length(site_transfers_to_remove)} site transfers to remove...")- remove_site_transfers(site_transfers_to_remove)+ if not dry_run? do+ remove_site_transfers(site_transfers_to_remove)- log("Site transfers cleared.")+ log("Site transfers cleared.")+ end# Site transfers backfill@@ -398,11 +426,13 @@ defmodule Plausible.DataMigration.BackfillTeams do"Found #{length(site_invitations_to_backfill)} ownership transfers without site transfer...")- backfill_site_transfers(site_invitations_to_backfill)+ if not dry_run? do+ backfill_site_transfers(site_invitations_to_backfill)- log("Backfilled missing site transfers.")+ log("Backfilled missing site transfers.")- log("All data are up to date now!")+ log("All data are up to date now!")+ endenddef delete_orphaned_teams(teams) dodiff --git a/lib/plausible/site/memberships/accept_invitation.ex b/lib/plausible/site/memberships/accept_invitation.exindex bde5bb9dc8..f5c9d6b39b 100644--- a/lib/plausible/site/memberships/accept_invitation.ex+++ b/lib/plausible/site/memberships/accept_invitation.ex@@ -75,22 +75,26 @@ defmodule Plausible.Site.Memberships.AcceptInvitation dosite = Repo.preload(invitation.site, :owner)with :ok <- Invitations.ensure_can_take_ownership(site, user) do- Plausible.Teams.Invitations.accept_transfer_sync(invitation, user)-site|> add_and_transfer_ownership(membership, user)|> Multi.delete(:invitation, invitation)+ |> Multi.run(:sync_transfer, fn _repo, _context ->+ Plausible.Teams.Invitations.accept_transfer_sync(invitation, user)+ {:ok, nil}+ end)|> finalize_invitation(invitation)endenddefp do_accept_invitation(invitation, user) do- Plausible.Teams.Invitations.accept_invitation_sync(invitation, user)-membership = get_or_create_membership(invitation, user)invitation|> add(membership, user)+ |> Multi.run(:sync_invitation, fn _repo, _context ->+ Plausible.Teams.Invitations.accept_invitation_sync(invitation, user)+ {:ok, nil}+ end)|> finalize_invitation(invitation)enddiff --git a/lib/workers/clean_invitations.ex b/lib/workers/clean_invitations.exindex c822e93a59..a494a06e19 100644--- a/lib/workers/clean_invitations.ex+++ b/lib/workers/clean_invitations.ex@@ -20,6 +20,11 @@ defmodule Plausible.Workers.CleanInvitations dowhere: ti.inserted_at < ^cutoff_time)+ Repo.delete_all(+ from ti in Plausible.Teams.SiteTransfer,+ where: ti.inserted_at < ^cutoff_time+ )+:okendenddiff --git a/test/support/factory.ex b/test/support/factory.exindex 53bf4f357c..aeb164b4f6 100644--- a/test/support/factory.ex+++ b/test/support/factory.ex@@ -38,6 +38,13 @@ defmodule Plausible.Factory do}end+ def site_transfer_factory do+ %Plausible.Teams.SiteTransfer{+ transfer_id: Nanoid.generate(),+ email: sequence(:email, &"email-#{&1}@example.com")+ }+ end+def user_factory(attrs) dopw = Map.get(attrs, :password, "password")diff --git a/test/workers/clean_invitations_test.exs b/test/workers/clean_invitations_test.exsindex d354b98af3..ff0ee03cac 100644--- a/test/workers/clean_invitations_test.exs+++ b/test/workers/clean_invitations_test.exs@@ -2,7 +2,7 @@ defmodule Plausible.Workers.CleanInvitationsTest douse Plausible.DataCasealias Plausible.Workers.CleanInvitations- test "cleans invitation that is more than 48h old" do+ test "cleans invitations and transfers that are more than 48h old" donow = NaiveDateTime.utc_now(:second)insert(:invitation,@@ -11,12 +11,38 @@ defmodule Plausible.Workers.CleanInvitationsTest doinviter: build(:user))+ site = insert(:site, team: build(:team))++ team_invitation =+ insert(:team_invitation,+ inserted_at: NaiveDateTime.shift(now, hour: -49),+ team: site.team,+ inviter: build(:user),+ role: :guest+ )++ insert(:guest_invitation,+ inserted_at: NaiveDateTime.shift(now, hour: -49),+ team_invitation: team_invitation,+ site: site,+ role: :viewer+ )++ insert(:site_transfer,+ inserted_at: NaiveDateTime.shift(now, hour: -49),+ site: site,+ initiator: build(:user)+ )+CleanInvitations.perform(nil)refute Repo.exists?(Plausible.Auth.Invitation)+ refute Repo.exists?(Plausible.Teams.Invitation)+ refute Repo.exists?(Plausible.Teams.GuestInvitation)+ refute Repo.exists?(Plausible.Teams.SiteTransfer)end- test "does not clean invitation that is less than 48h old" do+ test "does not clean invitations and transfers that are less than 48h old" donow = NaiveDateTime.utc_now(:second)insert(:invitation,@@ -25,6 +51,29 @@ defmodule Plausible.Workers.CleanInvitationsTest doinviter: build(:user))+ site = insert(:site, team: build(:team))++ team_invitation =+ insert(:team_invitation,+ inserted_at: NaiveDateTime.shift(now, hour: -47),+ team: site.team,+ inviter: build(:user),+ role: :guest+ )++ insert(:guest_invitation,+ inserted_at: NaiveDateTime.shift(now, hour: -47),+ team_invitation: team_invitation,+ site: site,+ role: :viewer+ )++ insert(:site_transfer,+ inserted_at: NaiveDateTime.shift(now, hour: -47),+ site: site,+ initiator: build(:user)+ )+CleanInvitations.perform(nil)assert Repo.exists?(Plausible.Auth.Invitation)