Skip to content

Commit 3f1dc74

Browse files
committed
add missing transaction data
1 parent 4e775c7 commit 3f1dc74

File tree

3 files changed

+151
-63
lines changed

3 files changed

+151
-63
lines changed

scripts/database_migration.exs

Lines changed: 138 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ defmodule DatabaseMigration do
445445
"status" => if(row["succeeded_at"] == nil, do: :initialized, else: :succeeded),
446446
"succeeded_at" => row["succeeded_at"],
447447
"reversed_at" => nil,
448-
"group_id" => nil,
448+
"group_id" => row["id"],
449449
## TODO: this might be null but shouldn't
450450
"user_id" => user["id"],
451451
"contract_id" => nil,
@@ -503,7 +503,9 @@ defmodule DatabaseMigration do
503503

504504
user = db |> Map.get("User", []) |> Enum.find(&(&1["id"] == github_user["user_id"]))
505505

506-
amount = Money.from_integer(String.to_integer(row["amount"]), row["currency"])
506+
org = db |> Map.get("Org", []) |> Enum.find(&(&1["id"] == bounty["org_id"]))
507+
508+
bounty_charge = db |> Map.get("BountyCharge", []) |> Enum.find(&(&1["id"] == row["bounty_charge_id"]))
507509

508510
if !bounty do
509511
raise "Bounty not found: #{inspect(row)}"
@@ -513,48 +515,37 @@ defmodule DatabaseMigration do
513515
raise "User not found: #{inspect(row)}"
514516
end
515517

516-
# TODO: add corresponding credit & debit transactions
517-
row = %{
518-
"id" => row["id"],
519-
"provider" => "stripe",
520-
"provider_id" => row["transfer_id"],
521-
"provider_charge_id" => nil,
522-
"provider_payment_intent_id" => nil,
523-
"provider_transfer_id" => row["transfer_id"],
524-
"provider_invoice_id" => nil,
525-
"provider_balance_transaction_id" => nil,
526-
"provider_meta" => nil,
527-
"gross_amount" => amount,
528-
"net_amount" => amount,
529-
"total_fee" => Money.zero(:USD),
530-
"provider_fee" => nil,
531-
"line_items" => nil,
532-
"type" => "transfer",
533-
# TODO: only add debit & credit transactions if not succeeded
534-
"status" => if(row["succeeded_at"] == nil, do: :initialized, else: :succeeded),
535-
"succeeded_at" => row["succeeded_at"],
536-
"reversed_at" => nil,
537-
"group_id" => nil,
538-
## TODO: this might be null but shouldn't
539-
"user_id" => user["id"],
540-
"contract_id" => nil,
541-
"original_contract_id" => nil,
542-
"timesheet_id" => nil,
543-
"bounty_id" => nil,
544-
"tip_id" => nil,
545-
"linked_transaction_id" => nil,
546-
"inserted_at" => row["created_at"],
547-
"updated_at" => row["updated_at"],
548-
"claim_id" => nil
549-
}
518+
if !org do
519+
raise "Org not found: #{inspect(row)}"
520+
end
550521

551-
if bounty["type"] == "tip" do
552-
Map.put(row, "tip_id", bounty["id"] <> user["id"])
553-
else
554-
row
555-
|> Map.put("bounty_id", claim["bounty_id"])
556-
|> Map.put("claim_id", claim["id"])
522+
if !bounty_charge do
523+
raise "BountyCharge not found: #{inspect(row)}"
557524
end
525+
526+
[
527+
maybe_create_transaction("debit", %{
528+
bounty_charge: bounty_charge,
529+
bounty_transfer: row,
530+
bounty: bounty,
531+
claim: claim,
532+
user: org
533+
}),
534+
maybe_create_transaction("credit", %{
535+
bounty_charge: bounty_charge,
536+
bounty_transfer: row,
537+
bounty: bounty,
538+
claim: claim,
539+
user: user
540+
}),
541+
maybe_create_transaction("transfer", %{
542+
bounty_charge: bounty_charge,
543+
bounty_transfer: row,
544+
bounty: bounty,
545+
claim: claim,
546+
user: user
547+
})
548+
]
558549
end
559550

560551
defp transform({"GithubInstallation", Installation}, row, _db) do
@@ -566,7 +557,7 @@ defmodule DatabaseMigration do
566557
"avatar_url" => nil,
567558
"repository_selection" => nil,
568559
"owner_id" => nil,
569-
"connected_user_id" => nil,
560+
"connected_user_id" => row["org_id"],
570561
"inserted_at" => row["created_at"],
571562
"updated_at" => row["updated_at"],
572563
"provider_user_id" => nil
@@ -631,6 +622,102 @@ defmodule DatabaseMigration do
631622

632623
defp transform(_, _row, _db), do: nil
633624

625+
defp maybe_create_transaction(type, %{
626+
bounty_charge: bounty_charge,
627+
bounty_transfer: bounty_transfer,
628+
bounty: bounty,
629+
claim: claim,
630+
user: user
631+
}) do
632+
amount = Money.from_integer(String.to_integer(bounty_transfer["amount"]), bounty_transfer["currency"])
633+
634+
res = %{
635+
"id" => String.slice(type, 0, 2) <> "_" <> bounty_transfer["id"],
636+
"provider" => "stripe",
637+
"provider_id" => nil,
638+
"provider_charge_id" => bounty_charge["charge_id"],
639+
"provider_payment_intent_id" => nil,
640+
"provider_transfer_id" => bounty_transfer["transfer_id"],
641+
"provider_invoice_id" => nil,
642+
"provider_balance_transaction_id" => nil,
643+
"provider_meta" => nil,
644+
"gross_amount" => amount,
645+
"net_amount" => amount,
646+
"total_fee" => Money.zero(:USD),
647+
"provider_fee" => nil,
648+
"line_items" => nil,
649+
"type" => type,
650+
"status" => nil,
651+
"succeeded_at" => nil,
652+
"reversed_at" => nil,
653+
"group_id" => bounty_charge["id"],
654+
## TODO: this might be null but shouldn't
655+
"user_id" => user["id"],
656+
"contract_id" => nil,
657+
"original_contract_id" => nil,
658+
"timesheet_id" => nil,
659+
"bounty_id" => nil,
660+
"tip_id" => nil,
661+
"linked_transaction_id" => nil,
662+
"inserted_at" => nil,
663+
"updated_at" => nil,
664+
"claim_id" => nil
665+
}
666+
667+
res =
668+
if bounty["type"] == "tip" do
669+
Map.put(res, "tip_id", bounty["id"] <> user["id"])
670+
else
671+
res
672+
|> Map.put("bounty_id", claim["bounty_id"])
673+
|> Map.put("claim_id", claim["id"])
674+
end
675+
676+
res =
677+
case type do
678+
"transfer" ->
679+
Map.put(res, "provider_id", bounty_transfer["transfer_id"])
680+
681+
"debit" ->
682+
Map.put(res, "linked_transaction_id", "cr_" <> bounty_transfer["id"])
683+
684+
"credit" ->
685+
Map.put(res, "linked_transaction_id", "de_" <> bounty_transfer["id"])
686+
687+
_ ->
688+
res
689+
end
690+
691+
cond do
692+
type == "transfer" && bounty_transfer["succeeded_at"] != nil ->
693+
Map.merge(res, %{
694+
"status" => :succeeded,
695+
"succeeded_at" => bounty_transfer["succeeded_at"],
696+
"inserted_at" => bounty_transfer["created_at"],
697+
"updated_at" => bounty_transfer["updated_at"]
698+
})
699+
700+
type == "debit" && bounty_charge["succeeded_at"] != nil ->
701+
Map.merge(res, %{
702+
"status" => :succeeded,
703+
"succeeded_at" => bounty_charge["succeeded_at"],
704+
"inserted_at" => bounty_charge["succeeded_at"],
705+
"updated_at" => bounty_charge["succeeded_at"]
706+
})
707+
708+
type == "credit" && bounty_charge["succeeded_at"] != nil ->
709+
Map.merge(res, %{
710+
"status" => :succeeded,
711+
"succeeded_at" => bounty_charge["succeeded_at"],
712+
"inserted_at" => bounty_charge["succeeded_at"],
713+
"updated_at" => bounty_charge["succeeded_at"]
714+
})
715+
716+
true ->
717+
nil
718+
end
719+
end
720+
634721
def process_dump(input_file, output_file) do
635722
db = collect_data(input_file)
636723

@@ -715,26 +802,27 @@ defmodule DatabaseMigration do
715802
defp transform_section(%{table: table, columns: _columns, data: data}, schema, db) do
716803
transformed_data =
717804
data
718-
|> Enum.map(fn row ->
805+
|> Enum.flat_map(fn row ->
719806
# try do
720-
transform({table, schema}, row, db)
807+
case transform({table, schema}, row, db) do
808+
nil -> []
809+
xs when is_list(xs) -> xs
810+
x -> [x]
811+
end
812+
721813
# rescue
722814
# e ->
723815
# IO.puts("Error transforming row in table #{table}: #{inspect(row)}")
724816
# IO.puts("Error: #{inspect(e)}")
725817
# nil
726818
# end
727819
end)
728-
|> Enum.reject(&is_nil/1)
729820
|> Enum.map(&post_transform(schema, &1))
730821

731-
transformed_table_name = schema.__schema__(:source)
732-
733822
if Enum.empty?(transformed_data) do
734823
nil
735824
else
736-
transformed_columns = Map.keys(hd(transformed_data))
737-
%{table: transformed_table_name, columns: transformed_columns, data: transformed_data}
825+
%{table: schema.__schema__(:source), columns: Map.keys(hd(transformed_data)), data: transformed_data}
738826
end
739827
end
740828

scripts/v1-progress.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
- updated_at: 1
9191
- succeeded_at: 1
9292
- charge_id: 1
93-
- user_id: 0
93+
- user_id: -1
9494
- org_id: 1
9595
- amount: 1
9696
- currency: 1
@@ -113,7 +113,7 @@
113113
- created_at: 1
114114
- updated_at: 1
115115
- succeeded_at: 1
116-
- bounty_charge_id: 0
116+
- bounty_charge_id: 1
117117
- claim_id: 1
118118
- transfer_id: 1
119119
- amount: 1
@@ -203,8 +203,8 @@
203203
- created_at: 1
204204
- updated_at: 1
205205
- github_id: 1
206-
- org_id: 0
207-
- github_org_handle: 0
206+
- org_id: 1
207+
- github_org_handle: -1
208208
- "GithubIssue":
209209
- id: 1
210210
- url: -1

scripts/v2-progress.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
- id: 1
4646
- status: 0
4747
- type: 0
48-
- url: 0
48+
- url: 1
4949
- group_id: 0
5050
- group_share: 0
5151
- source_id: 1
@@ -125,11 +125,11 @@
125125
- provider_meta: -1
126126
- avatar_url: -1
127127
- repository_selection: -1
128-
- owner_id: 0
129-
- connected_user_id: 0
128+
- owner_id: -2
129+
- connected_user_id: 1
130130
- inserted_at: 1
131131
- updated_at: 1
132-
- provider_user_id: 0
132+
- provider_user_id: -1
133133
- members:
134134
- id: 1
135135
- role: 1
@@ -194,7 +194,7 @@
194194
- description: 1
195195
- number: 1
196196
- url: 1
197-
- repository_id: 0
197+
- repository_id: -1
198198
- inserted_at: 1
199199
- updated_at: 1
200200
- timesheets:
@@ -233,14 +233,14 @@
233233
- status: 1
234234
- succeeded_at: 1
235235
- reversed_at: -1
236-
- group_id: 0
237-
- user_id: 0
236+
- group_id: 1
237+
- user_id: 1
238238
- contract_id: -1
239239
- original_contract_id: -1
240240
- timesheet_id: -1
241241
- bounty_id: 1
242-
- tip_id: 0
243-
- linked_transaction_id: 0
242+
- tip_id: 1
243+
- linked_transaction_id: 1
244244
- inserted_at: 1
245245
- updated_at: 1
246246
- claim_id: 1

0 commit comments

Comments
 (0)