Skip to content

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

Chunk sync_eligible inserts (#6659)

Adamfca4ccdcf623
diff --git a/lib/plausible/team_deletion_schedules.ex b/lib/plausible/team_deletion_schedules.ex
index 300bff8f7d..f45adcf1bb 100644
--- a/lib/plausible/team_deletion_schedules.ex
+++ b/lib/plausible/team_deletion_schedules.ex
@@ -16,13 +16,18 @@ defmodule Plausible.TeamDeletionSchedules do
alias Plausible.Teams
alias Plausible.Teams.DeletionSchedule
+ # Postgres protocol caps a single statement at 65535 bind
+ # parameters. build_schedule_row/3 produces 9 fields per row, so a single
+ # insert_all could only ever fit ~7k rows
+ @insert_chunk_size 1_000
+
@doc """
Finds newly eligible teams (expired trial/churned subscription) and
schedules each for the deletion pipeline. Reactivation is handled
via billing (Paddle) handlers.
"""
- @spec sync_eligible(Date.t()) :: non_neg_integer()
- def sync_eligible(today \\ Date.utc_today()) do
+ @spec sync_eligible(Date.t(), pos_integer()) :: non_neg_integer()
+ def sync_eligible(today \\ Date.utc_today(), insert_chunk_size \\ @insert_chunk_size) do
candidates =
Repo.all(
from(t in Teams.Team,
@@ -50,15 +55,19 @@ defmodule Plausible.TeamDeletionSchedules do
)
now = NaiveDateTime.utc_now(:second)
- rows = Enum.map(candidates, &build_schedule_row(&1, today, now))
- {count, _} =
- Repo.insert_all(TeamDeletionSchedule, rows,
- on_conflict: :nothing,
- conflict_target: {:unsafe_fragment, terminal_statuses_index_predicate()}
- )
+ candidates
+ |> Enum.map(&build_schedule_row(&1, today, now))
+ |> Enum.chunk_every(insert_chunk_size)
+ |> Enum.reduce(0, fn chunk, total ->
+ {count, _} =
+ Repo.insert_all(TeamDeletionSchedule, chunk,
+ on_conflict: :nothing,
+ conflict_target: {:unsafe_fragment, terminal_statuses_index_predicate()}
+ )
- count
+ total + count
+ end)
end
@doc """
diff --git a/test/plausible/team_deletion_schedules_test.exs b/test/plausible/team_deletion_schedules_test.exs
index 4a61b47aac..f9c2bfbaab 100644
--- a/test/plausible/team_deletion_schedules_test.exs
+++ b/test/plausible/team_deletion_schedules_test.exs
@@ -243,6 +243,35 @@ defmodule Plausible.TeamDeletionSchedulesTest do
end
end
+ describe "sync_eligible/2 - chunked inserts" do
+ test "inserts all candidates across multiple chunks" do
+ teams =
+ for _ <- 1..5 do
+ team = insert(:team, trial_expiry_date: Date.shift(@today, day: -1))
+ new_site(team: team)
+ team
+ end
+
+ assert TeamDeletionSchedules.sync_eligible(@today, 2) == 5
+
+ assert Repo.aggregate(TeamDeletionSchedule, :count) == 5
+
+ for team <- teams do
+ assert Repo.get_by!(TeamDeletionSchedule, team_id: team.id)
+ end
+ end
+
+ test "remains idempotent when chunked" do
+ team = insert(:team, trial_expiry_date: Date.shift(@today, day: -1))
+ new_site(team: team)
+
+ assert TeamDeletionSchedules.sync_eligible(@today, 2) == 1
+ assert TeamDeletionSchedules.sync_eligible(@today, 2) == 0
+
+ assert Repo.aggregate(TeamDeletionSchedule, :count) == 1
+ end
+ end
+
describe "cancel_for_team/1" do
test "cancels an active schedule when the team has an active subscription" do
team = insert(:team)