@@ -445,7 +445,7 @@ defmodule DatabaseMigration do
445
445
"status" => if ( row [ "succeeded_at" ] == nil , do: :initialized , else: :succeeded ) ,
446
446
"succeeded_at" => row [ "succeeded_at" ] ,
447
447
"reversed_at" => nil ,
448
- "group_id" => nil ,
448
+ "group_id" => row [ "id" ] ,
449
449
## TODO: this might be null but shouldn't
450
450
"user_id" => user [ "id" ] ,
451
451
"contract_id" => nil ,
@@ -503,7 +503,9 @@ defmodule DatabaseMigration do
503
503
504
504
user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] ) )
505
505
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" ] ) )
507
509
508
510
if ! bounty do
509
511
raise "Bounty not found: #{ inspect ( row ) } "
@@ -513,48 +515,37 @@ defmodule DatabaseMigration do
513
515
raise "User not found: #{ inspect ( row ) } "
514
516
end
515
517
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
550
521
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 ) } "
557
524
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
+ ]
558
549
end
559
550
560
551
defp transform ( { "GithubInstallation" , Installation } , row , _db ) do
@@ -566,7 +557,7 @@ defmodule DatabaseMigration do
566
557
"avatar_url" => nil ,
567
558
"repository_selection" => nil ,
568
559
"owner_id" => nil ,
569
- "connected_user_id" => nil ,
560
+ "connected_user_id" => row [ "org_id" ] ,
570
561
"inserted_at" => row [ "created_at" ] ,
571
562
"updated_at" => row [ "updated_at" ] ,
572
563
"provider_user_id" => nil
@@ -631,6 +622,90 @@ defmodule DatabaseMigration do
631
622
632
623
defp transform ( _ , _row , _db ) , do: nil
633
624
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_transfer [ "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" => bounty_transfer [ "created_at" ] ,
663
+ "updated_at" => bounty_transfer [ "updated_at" ] ,
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
+ bounty_charge [ "succeeded_at" ] == nil ->
693
+ nil
694
+
695
+ type == "transfer" && bounty_transfer [ "succeeded_at" ] == nil ->
696
+ nil
697
+
698
+ type == "transfer" ->
699
+ res |> Map . put ( "status" , :succeeded ) |> Map . put ( "succeeded_at" , bounty_transfer [ "succeeded_at" ] )
700
+
701
+ type == "debit" ->
702
+ res |> Map . put ( "status" , :succeeded ) |> Map . put ( "succeeded_at" , bounty_charge [ "succeeded_at" ] )
703
+
704
+ type == "credit" ->
705
+ res |> Map . put ( "status" , :succeeded ) |> Map . put ( "succeeded_at" , bounty_charge [ "succeeded_at" ] )
706
+ end
707
+ end
708
+
634
709
def process_dump ( input_file , output_file ) do
635
710
db = collect_data ( input_file )
636
711
@@ -715,26 +790,27 @@ defmodule DatabaseMigration do
715
790
defp transform_section ( % { table: table , columns: _columns , data: data } , schema , db ) do
716
791
transformed_data =
717
792
data
718
- |> Enum . map ( fn row ->
793
+ |> Enum . flat_map ( fn row ->
719
794
# try do
720
- transform ( { table , schema } , row , db )
795
+ case transform ( { table , schema } , row , db ) do
796
+ nil -> [ ]
797
+ xs when is_list ( xs ) -> xs
798
+ x -> [ x ]
799
+ end
800
+
721
801
# rescue
722
802
# e ->
723
803
# IO.puts("Error transforming row in table #{table}: #{inspect(row)}")
724
804
# IO.puts("Error: #{inspect(e)}")
725
805
# nil
726
806
# end
727
807
end )
728
- |> Enum . reject ( & is_nil / 1 )
729
808
|> Enum . map ( & post_transform ( schema , & 1 ) )
730
809
731
- transformed_table_name = schema . __schema__ ( :source )
732
-
733
810
if Enum . empty? ( transformed_data ) do
734
811
nil
735
812
else
736
- transformed_columns = Map . keys ( hd ( transformed_data ) )
737
- % { table: transformed_table_name , columns: transformed_columns , data: transformed_data }
813
+ % { table: schema . __schema__ ( :source ) , columns: Map . keys ( hd ( transformed_data ) ) , data: transformed_data }
738
814
end
739
815
end
740
816
0 commit comments