Skip to content

Commit

Permalink
Allow multiple project scheduled deletion in the same txn (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe authored Feb 26, 2025
1 parent 1ae154a commit 86f20c9
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/lightning/projects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -700,24 +700,25 @@ defmodule Lightning.Projects do
"""
def schedule_project_deletion(project) do
Multi.new()
|> scheduled_project_deletion_changes(project)
|> scheduled_project_deletion_changes(project: project)
|> Repo.transaction()
|> case do
{:ok, %{project: updated_project}} -> {:ok, updated_project}
{:error, _op, changeset, _changes} -> {:error, changeset}
end
end

def scheduled_project_deletion_changes(multi, project) do
def scheduled_project_deletion_changes(multi, [{project_op, project}]) do
date =
case Lightning.Config.purge_deleted_after_days() do
nil -> DateTime.utc_now()
integer -> DateTime.utc_now() |> Timex.shift(days: integer)
end

multi
|> Multi.all(:triggers, project_triggers_query(project))
|> Multi.merge(fn %{triggers: triggers} ->
|> Multi.merge(fn _changes ->
triggers = project_triggers_query(project) |> Repo.all()

Enum.reduce(triggers, Multi.new(), fn trigger, multi ->
Multi.update(
multi,
Expand All @@ -727,7 +728,7 @@ defmodule Lightning.Projects do
end)
end)
|> Multi.update(
:project,
project_op,
Ecto.Changeset.change(project, %{
scheduled_deletion: DateTime.truncate(date, :second)
})
Expand Down

0 comments on commit 86f20c9

Please sign in to comment.