Skip to content

Commit 2600a66

Browse files
committed
in midst of getting migration script up to date
1 parent 0d32708 commit 2600a66

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

scripts/database_migration.exs

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ defmodule DatabaseMigration do
5757
@relevant_tables Map.keys(@table_mappings)
5858

5959
defp transform("Task", row, db) do
60-
github_issue =
61-
db |> Map.get("GithubIssue", []) |> Enum.find(&(&1["id"] == row["issue_id"]))
60+
if row["forge"] != "github" do
61+
raise "Unknown forge: #{row["forge"]}"
62+
end
63+
64+
github_issue = db |> Map.get("GithubIssue", []) |> Enum.find(&(&1["id"] == row["issue_id"]))
6265

63-
github_pull_request =
64-
db |> Map.get("GithubPullRequest", []) |> Enum.find(&(&1["id"] == row["pull_request_id"]))
66+
github_pull_request = db |> Map.get("GithubPullRequest", []) |> Enum.find(&(&1["id"] == row["pull_request_id"]))
6567

6668
row =
6769
cond do
@@ -96,10 +98,6 @@ defmodule DatabaseMigration do
9698
}
9799

98100
true ->
99-
if row["forge"] != "github" do
100-
raise "Unknown forge: #{row["forge"]}"
101-
end
102-
103101
%{
104102
"id" => row["id"],
105103
"provider" => row["forge"],
@@ -281,49 +279,66 @@ defmodule DatabaseMigration do
281279
defp transform("Bounty", row, db) do
282280
reward = db |> Map.get("Reward", []) |> Enum.find(&(&1["bounty_id"] == row["id"]))
283281

284-
amount =
285-
if reward, do: Money.from_integer(String.to_integer(reward["amount"]), reward["currency"])
282+
amount = if reward, do: Money.from_integer(String.to_integer(reward["amount"]), reward["currency"])
286283

287-
row
288-
|> Map.put("ticket_id", row["task_id"])
289-
|> Map.put("owner_id", row["org_id"])
290-
|> Map.put("creator_id", row["poster_id"])
291-
|> Map.put("inserted_at", row["created_at"])
292-
|> Map.put("updated_at", row["updated_at"])
293-
|> Map.put("amount", amount)
284+
%{
285+
"id" => row["id"],
286+
"amount" => amount,
287+
"ticket_id" => row["task_id"],
288+
"owner_id" => row["org_id"],
289+
"creator_id" => row["poster_id"],
290+
"inserted_at" => row["created_at"],
291+
"updated_at" => row["updated_at"]
292+
}
294293
end
295294

296295
defp transform("BountyTransfer", row, db) do
297-
claim =
298-
db |> Map.get("Claim", []) |> Enum.find(&(&1["id"] == row["claim_id"]))
296+
claim = db |> Map.get("Claim", []) |> Enum.find(&(&1["id"] == row["claim_id"]))
299297

300-
github_user =
301-
db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == claim["github_user_id"]))
298+
github_user = db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == claim["github_user_id"]))
302299

303-
user =
304-
db |> Map.get("User", []) |> Enum.find(&(&1["id"] == github_user["user_id"]))
300+
user = db |> Map.get("User", []) |> Enum.find(&(&1["id"] == github_user["user_id"]))
305301

306302
amount = Money.from_integer(String.to_integer(row["amount"]), row["currency"])
307303

308-
row =
309-
if claim && user do
310-
row
311-
|> Map.put("type", "transfer")
312-
|> Map.put("provider", "stripe")
313-
|> Map.put("provider_id", row["transfer_id"])
314-
|> Map.put("net_amount", amount)
315-
|> Map.put("gross_amount", amount)
316-
|> Map.put("total_fee", Money.zero(:USD))
317-
|> Map.put("bounty_id", claim["bounty_id"])
318-
## TODO: this might be null but shouldn't
319-
|> Map.put("user_id", user["id"])
320-
|> Map.put("inserted_at", row["created_at"])
321-
|> Map.put("updated_at", row["updated_at"])
322-
|> Map.put("status", if(row["succeeded_at"] == nil, do: :initialized, else: :succeeded))
323-
|> Map.put("succeeded_at", row["succeeded_at"])
324-
end
304+
if !claim || !user do
305+
raise "Claim or User not found: #{inspect(row)}"
306+
end
325307

326-
row
308+
# TODO: add corresponding credit & debit transactions
309+
%{
310+
"id" => row["id"],
311+
"provider" => "stripe",
312+
"provider_id" => row["transfer_id"],
313+
"provider_charge_id" => nil,
314+
"provider_payment_intent_id" => nil,
315+
"provider_transfer_id" => row["transfer_id"],
316+
"provider_invoice_id" => nil,
317+
"provider_balance_transaction_id" => nil,
318+
"provider_meta" => nil,
319+
"gross_amount" => amount,
320+
"net_amount" => amount,
321+
"total_fee" => Money.zero(:USD),
322+
"provider_fee" => nil,
323+
"line_items" => nil,
324+
"type" => "transfer",
325+
# TODO: only add debit & credit transactions if not succeeded
326+
"status" => if(row["succeeded_at"] == nil, do: :initialized, else: :succeeded),
327+
"succeeded_at" => row["succeeded_at"],
328+
"reversed_at" => nil,
329+
"group_id" => nil,
330+
## TODO: this might be null but shouldn't
331+
"user_id" => user["id"],
332+
"contract_id" => nil,
333+
"original_contract_id" => nil,
334+
"timesheet_id" => nil,
335+
"bounty_id" => claim["bounty_id"],
336+
"tip_id" => nil,
337+
"linked_transaction_id" => nil,
338+
"inserted_at" => row["created_at"],
339+
"updated_at" => row["updated_at"],
340+
"claim_id" => claim["id"]
341+
}
327342
end
328343

329344
defp transform(_, _row, _db), do: nil

0 commit comments

Comments
 (0)