Skip to content

Commit 332dc19

Browse files
committed
feat: backfill claims after migration
1 parent e8dd51f commit 332dc19

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/algora/admin/admin.ex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Algora.Admin do
33
import Ecto.Query
44

55
alias Algora.Accounts.User
6+
alias Algora.Bounties.Claim
7+
alias Algora.Parser
68
alias Algora.Payments
79
alias Algora.Repo
810
alias Algora.Workspace
@@ -54,6 +56,28 @@ defmodule Algora.Admin do
5456
:ok
5557
end
5658

59+
def backfill_claims! do
60+
query =
61+
from(t in Claim,
62+
where: not is_nil(t.url),
63+
distinct: t.url,
64+
select: t.url
65+
)
66+
67+
{success, failure} =
68+
query
69+
|> Repo.all()
70+
|> Task.async_stream(&backfill_claim/1, max_concurrency: 1, timeout: :infinity)
71+
|> Enum.reduce({0, 0}, fn
72+
{:ok, {:ok, _}}, {s, f} -> {s + 1, f}
73+
{:ok, {:error, _}}, {s, f} -> {s, f + 1}
74+
{:exit, _}, {s, f} -> {s, f + 1}
75+
end)
76+
77+
IO.puts("Claim backfill complete: #{success} succeeded, #{failure} failed")
78+
:ok
79+
end
80+
5781
def backfill_repo(url) do
5882
with %URI{host: "api.github.com", path: "/repos/" <> path} <- URI.parse(url),
5983
[owner, repo] <- String.split(path, "/", trim: true),
@@ -70,6 +94,22 @@ defmodule Algora.Admin do
7094
end
7195
end
7296

97+
def backfill_claim(url) do
98+
with {:ok, [ticket_ref: [owner: owner, repo: repo, type: _type, number: number]], _, _, _, _} <-
99+
Parser.full_ticket_ref(url),
100+
{:ok, ticket} <- Workspace.ensure_ticket(token!(), owner, repo, number),
101+
:ok <- update_claims(url, ticket.id) do
102+
{:ok, ticket}
103+
else
104+
{:error, "404 Not Found"} = error ->
105+
error
106+
107+
error ->
108+
Logger.error("Failed to backfill repo #{url}: #{inspect(error)}")
109+
{:error, error}
110+
end
111+
end
112+
73113
def make_admin!(user_handle, is_admin) when is_boolean(is_admin) do
74114
user_handle
75115
|> Algora.Accounts.get_user_by_handle()
@@ -93,4 +133,10 @@ defmodule Algora.Admin do
93133

94134
:ok
95135
end
136+
137+
defp update_claims(url, source_id) do
138+
Repo.update_all(from(t in Claim, where: t.url == ^url), set: [source_id: source_id])
139+
140+
:ok
141+
end
96142
end

scripts/database_migration.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ defmodule DatabaseMigration do
427427
bounty = find_by_index(db, "Bounty", "id", row["bounty_id"])
428428
task = find_by_index(db, "Task", "id", bounty["task_id"])
429429
github_user = find_by_index(db, "GithubUser", "id", row["github_user_id"])
430-
github_pull_request = find_by_index(db, "GithubPullRequest", "id", row["github_pull_request_id"])
431430

432431
user_id = or_else(github_user["user_id"], github_user["id"])
433432

@@ -455,7 +454,7 @@ defmodule DatabaseMigration do
455454
"url" => or_else(row["github_url"], "https://algora.io"),
456455
"group_id" => row["id"],
457456
"group_share" => nil,
458-
"source_id" => github_pull_request["task_id"],
457+
"source_id" => nil,
459458
"target_id" => task["id"],
460459
"user_id" => user_id,
461460
"inserted_at" => row["created_at"],
@@ -1354,6 +1353,7 @@ defmodule DatabaseMigration do
13541353
:ok = time_step("Clearing tables", fn -> clear_tables!() end)
13551354
{:ok, _} = time_step("Importing new data", fn -> psql(["-f", output_file]) end)
13561355
:ok = time_step("Backfilling repositories", fn -> Algora.Admin.backfill_repos!() end)
1356+
:ok = time_step("Backfilling claims", fn -> Algora.Admin.backfill_claims!() end)
13571357
end)
13581358

13591359
IO.puts("✅ Migration completed successfully in #{total_time / 1_000_000} seconds")

0 commit comments

Comments
 (0)