Skip to content

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

Update `team.locked` in parallel with `sites.locked` in SiteLocker (#5315)

Adrian Gruntkowski63f75c90d510
diff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.ex
index f4f67353be..cb4c770b10 100644
--- a/lib/plausible/billing/site_locker.ex
+++ b/lib/plausible/billing/site_locker.ex
@@ -46,7 +46,7 @@ defmodule Plausible.Billing.SiteLocker do
end
end
- @spec set_lock_status_for(Teams.Team.t(), boolean()) :: {:ok, non_neg_integer()}
+ @spec set_lock_status_for(Teams.Team.t(), boolean()) :: :ok
def set_lock_status_for(team, status) do
site_ids = Teams.owned_sites_ids(team)
@@ -56,9 +56,13 @@ defmodule Plausible.Billing.SiteLocker do
where: s.id in ^site_ids
)
- {num_updated, _} = Repo.update_all(site_q, set: [locked: status])
+ {_, _} = Repo.update_all(site_q, set: [locked: status])
- {:ok, num_updated}
+ query = from(t in Teams.Team, where: t.id == ^team.id)
+
+ {_, _} = Repo.update_all(query, set: [locked: status])
+
+ :ok
end
defp send_grace_period_end_email(team) do
diff --git a/lib/plausible/data_migration/backfill_teams.ex b/lib/plausible/data_migration/backfill_teams.ex
index bf98cbec7e..dbe1bfbb54 100644
--- a/lib/plausible/data_migration/backfill_teams.ex
+++ b/lib/plausible/data_migration/backfill_teams.ex
@@ -27,7 +27,8 @@ defmodule Plausible.DataMigration.BackfillTeams do
left_join: tm in assoc(t, :team_memberships),
where: is_nil(tm.id),
left_join: s in assoc(t, :sites),
- where: is_nil(s.id)
+ where: is_nil(s.id),
+ select: %{t | locked: nil}
)
|> Repo.all(timeout: :infinity)
@@ -108,7 +109,7 @@ defmodule Plausible.DataMigration.BackfillTeams do
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)
+ from(t in Teams.Team, where: t.id in ^team_ids_to_prune, select: %{t | locked: nil})
|> Repo.all(timeout: :infinity)
|> Enum.each(fn team ->
Plausible.Teams.Memberships.prune_guests(team)
@@ -145,7 +146,7 @@ defmodule Plausible.DataMigration.BackfillTeams do
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)
+ from(t in Teams.Team, where: t.id in ^team_ids_to_prune, select: %{t | locked: nil})
|> Repo.all(timeout: :infinity)
|> Enum.each(fn team ->
Plausible.Teams.Memberships.prune_guests(team)
@@ -179,7 +180,7 @@ defmodule Plausible.DataMigration.BackfillTeams do
where: not exists(guest_memberships_query),
select: %{
user: u,
- site: %{s | team: t},
+ site: %{s | team: %{t | locked: nil}},
inserted_at: sm.inserted_at,
updated_at: sm.updated_at,
role: sm.role
@@ -252,7 +253,7 @@ defmodule Plausible.DataMigration.BackfillTeams do
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)
+ from(t in Teams.Team, where: t.id in ^team_ids_to_prune, select: %{t | locked: nil})
|> Repo.all(timeout: :infinity)
|> Enum.each(fn team ->
Plausible.Teams.Invitations.prune_guest_invitations(team)
@@ -290,7 +291,7 @@ defmodule Plausible.DataMigration.BackfillTeams do
role: si.role,
invitation_id: si.invitation_id,
email: si.email,
- site: %{s | team: t},
+ site: %{s | team: %{t | locked: nil}},
inviter: inv
}
)
diff --git a/lib/plausible/teams.ex b/lib/plausible/teams.ex
index d3672d1213..3936b778ae 100644
--- a/lib/plausible/teams.ex
+++ b/lib/plausible/teams.ex
@@ -66,6 +66,11 @@ defmodule Plausible.Teams do
def on_trial?(_), do: always(true)
end
+ @spec locked?(Teams.Team.t() | nil) :: boolean()
+ def locked?(nil), do: false
+
+ def locked?(%Teams.Team{locked: locked}), do: locked
+
@spec trial_days_left(Teams.Team.t()) :: integer()
def trial_days_left(nil) do
nil
diff --git a/lib/plausible/teams/team.ex b/lib/plausible/teams/team.ex
index a799415014..e152e3ae89 100644
--- a/lib/plausible/teams/team.ex
+++ b/lib/plausible/teams/team.ex
@@ -27,6 +27,7 @@ defmodule Plausible.Teams.Team do
field :trial_expiry_date, :date
field :accept_traffic_until, :date
field :allow_next_upgrade_override, :boolean, default: false
+ field :locked, :boolean, default: false
field :setup_complete, :boolean, default: false
field :setup_at, :naive_datetime