@@ -130,8 +130,8 @@ defmodule DatabaseMigration do
130
130
"description" => row [ "body" ] ,
131
131
"number" => row [ "number" ] ,
132
132
"url" => "https://github.com/#{ row [ "repo_owner" ] } /#{ row [ "repo_name" ] } /issues/#{ row [ "number" ] } " ,
133
- "inserted_at" => row [ "created_at" ] ,
134
- "updated_at" => row [ "updated_at" ]
133
+ "inserted_at" => "1970-01-01 00:00:00" ,
134
+ "updated_at" => "1970-01-01 00:00:00"
135
135
}
136
136
end
137
137
@@ -298,13 +298,19 @@ defmodule DatabaseMigration do
298
298
299
299
defp transform ( { "GithubUser" , User } , _row , _db ) , do: nil
300
300
301
- defp transform ( { "Account" , Identity } , row , _db ) do
301
+ defp transform ( { "Account" , Identity } , row , db ) do
302
+ user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == row [ "\" userId\" " ] ) )
303
+
304
+ if ! user do
305
+ raise "User not found: #{ inspect ( row ) } "
306
+ end
307
+
302
308
% {
303
309
"id" => row [ "id" ] ,
304
- "user_id" => row [ " \" userId \" "] ,
310
+ "user_id" => user [ "id "] ,
305
311
"provider" => row [ "provider" ] ,
306
312
"provider_token" => row [ "access_token" ] ,
307
- "provider_email" => nil ,
313
+ "provider_email" => user [ "email" ] ,
308
314
"provider_login" => nil ,
309
315
"provider_id" => row [ "\" providerAccountId\" " ] ,
310
316
"provider_meta" => nil ,
@@ -341,7 +347,11 @@ defmodule DatabaseMigration do
341
347
end
342
348
343
349
defp transform ( { "Bounty" , CommandResponse } , row , _db ) do
344
- if row [ "github_res_comment_id" ] != nil do
350
+ if row [ "task_id" ] == "clo0q1x540000mj0ghdgmezqw" do
351
+ IO . inspect ( row , label: "transform(Bounty -> CommandResponse)" )
352
+ end
353
+
354
+ if ! nullish? ( row [ "github_res_comment_id" ] ) do
345
355
% {
346
356
"id" => row [ "id" ] ,
347
357
"provider" => "github" ,
@@ -362,7 +372,7 @@ defmodule DatabaseMigration do
362
372
363
373
github_user = db |> Map . get ( "GithubUser" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == row [ "github_user_id" ] ) )
364
374
365
- user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] ) )
375
+ user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] || github_user [ "id" ] ) )
366
376
367
377
if ! bounty do
368
378
raise "Bounty not found: #{ inspect ( row ) } "
@@ -390,7 +400,7 @@ defmodule DatabaseMigration do
390
400
391
401
github_user = db |> Map . get ( "GithubUser" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == row [ "github_user_id" ] ) )
392
402
393
- user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] ) )
403
+ user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] || github_user [ "id" ] ) )
394
404
395
405
# TODO: this might be null
396
406
github_pull_request =
@@ -402,10 +412,11 @@ defmodule DatabaseMigration do
402
412
403
413
% {
404
414
"id" => row [ "id" ] ,
405
- "status" => nil ,
415
+ # TODO:
416
+ "status" => :pending ,
406
417
"type" =>
407
418
cond do
408
- row [ "github_pull_request_id" ] != nil -> "pull_request"
419
+ ! nullish? ( row [ "github_pull_request_id" ] ) -> "pull_request"
409
420
String . match? ( row [ "github_url" ] || "" , ~r{ ^https?://(?:www\. )?figma\. com/} ) -> "design"
410
421
true -> "pull_request"
411
422
end ,
@@ -468,9 +479,9 @@ defmodule DatabaseMigration do
468
479
defp transform ( { "BountyTransfer" , Tip } , row , db ) do
469
480
claim = db |> Map . get ( "Claim" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == row [ "claim_id" ] ) )
470
481
471
- github_user = db |> Map . get ( "GithubUser" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == row [ "github_user_id" ] ) )
482
+ github_user = db |> Map . get ( "GithubUser" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == claim [ "github_user_id" ] ) )
472
483
473
- user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] ) )
484
+ user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] || github_user [ "id" ] ) )
474
485
475
486
bounty = db |> Map . get ( "Bounty" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == claim [ "bounty_id" ] ) )
476
487
@@ -506,7 +517,7 @@ defmodule DatabaseMigration do
506
517
507
518
github_user = db |> Map . get ( "GithubUser" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == claim [ "github_user_id" ] ) )
508
519
509
- user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] ) )
520
+ user = db |> Map . get ( "User" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == github_user [ "user_id" ] || github_user [ "id" ] ) )
510
521
511
522
org = db |> Map . get ( "Org" , [ ] ) |> Enum . find ( & ( & 1 [ "id" ] == bounty [ "org_id" ] ) )
512
523
@@ -528,29 +539,32 @@ defmodule DatabaseMigration do
528
539
raise "BountyCharge not found: #{ inspect ( row ) } "
529
540
end
530
541
531
- [
532
- maybe_create_transaction ( "debit" , % {
533
- bounty_charge: bounty_charge ,
534
- bounty_transfer: row ,
535
- bounty: bounty ,
536
- claim: claim ,
537
- user: org
538
- } ) ,
539
- maybe_create_transaction ( "credit" , % {
540
- bounty_charge: bounty_charge ,
541
- bounty_transfer: row ,
542
- bounty: bounty ,
543
- claim: claim ,
544
- user: user
545
- } ) ,
546
- maybe_create_transaction ( "transfer" , % {
547
- bounty_charge: bounty_charge ,
548
- bounty_transfer: row ,
549
- bounty: bounty ,
550
- claim: claim ,
551
- user: user
552
- } )
553
- ]
542
+ Enum . reject (
543
+ [
544
+ maybe_create_transaction ( "debit" , % {
545
+ bounty_charge: bounty_charge ,
546
+ bounty_transfer: row ,
547
+ bounty: bounty ,
548
+ claim: claim ,
549
+ user: org
550
+ } ) ,
551
+ maybe_create_transaction ( "credit" , % {
552
+ bounty_charge: bounty_charge ,
553
+ bounty_transfer: row ,
554
+ bounty: bounty ,
555
+ claim: claim ,
556
+ user: user
557
+ } ) ,
558
+ maybe_create_transaction ( "transfer" , % {
559
+ bounty_charge: bounty_charge ,
560
+ bounty_transfer: row ,
561
+ bounty: bounty ,
562
+ claim: claim ,
563
+ user: user
564
+ } )
565
+ ] ,
566
+ & is_nil / 1
567
+ )
554
568
end
555
569
556
570
defp transform ( { "GithubInstallation" , Installation } , row , _db ) do
@@ -573,7 +587,7 @@ defmodule DatabaseMigration do
573
587
% {
574
588
"id" => row [ "id" ] ,
575
589
"provider" => "stripe" ,
576
- "provider_id" => row [ "stripe_id " ] ,
590
+ "provider_id" => row [ "id " ] ,
577
591
"provider_meta" => nil ,
578
592
"name" => nil ,
579
593
"details_submitted" => row [ "details_submitted" ] ,
@@ -606,7 +620,7 @@ defmodule DatabaseMigration do
606
620
end
607
621
608
622
defp transform ( { "StripePaymentMethod" , PaymentMethod } , row , db ) do
609
- customer = db |> Map . get ( "StripeCustomer" , [ ] ) |> Enum . find ( & ( & 1 [ "id " ] == row [ "customer_id " ] ) )
623
+ customer = db |> Map . get ( "StripeCustomer" , [ ] ) |> Enum . find ( & ( & 1 [ "org_id " ] == row [ "org_id " ] ) )
610
624
611
625
if ! customer do
612
626
raise "StripeCustomer not found: #{ inspect ( row ) } "
@@ -619,7 +633,7 @@ defmodule DatabaseMigration do
619
633
"provider_meta" => nil ,
620
634
"provider_customer_id" => customer [ "stripe_id" ] ,
621
635
"is_default" => row [ "is_default" ] ,
622
- "customer_id" => row [ "customer_id "] ,
636
+ "customer_id" => customer [ "id "] ,
623
637
"inserted_at" => row [ "created_at" ] ,
624
638
"updated_at" => row [ "updated_at" ]
625
639
}
@@ -694,23 +708,23 @@ defmodule DatabaseMigration do
694
708
end
695
709
696
710
cond do
697
- type == "transfer" && bounty_transfer [ "succeeded_at" ] != nil ->
711
+ type == "transfer" && ! nullish? ( bounty_transfer [ "succeeded_at" ] ) ->
698
712
Map . merge ( res , % {
699
713
"status" => :succeeded ,
700
714
"succeeded_at" => bounty_transfer [ "succeeded_at" ] ,
701
715
"inserted_at" => bounty_transfer [ "created_at" ] ,
702
716
"updated_at" => bounty_transfer [ "updated_at" ]
703
717
} )
704
718
705
- type == "debit" && bounty_charge [ "succeeded_at" ] != nil ->
719
+ type == "debit" && ! nullish? ( bounty_charge [ "succeeded_at" ] ) ->
706
720
Map . merge ( res , % {
707
721
"status" => :succeeded ,
708
722
"succeeded_at" => bounty_charge [ "succeeded_at" ] ,
709
723
"inserted_at" => bounty_charge [ "succeeded_at" ] ,
710
724
"updated_at" => bounty_charge [ "succeeded_at" ]
711
725
} )
712
726
713
- type == "credit" && bounty_charge [ "succeeded_at" ] != nil ->
727
+ type == "credit" && ! nullish? ( bounty_charge [ "succeeded_at" ] ) ->
714
728
Map . merge ( res , % {
715
729
"status" => :succeeded ,
716
730
"succeeded_at" => bounty_charge [ "succeeded_at" ] ,
@@ -1037,6 +1051,8 @@ defmodule DatabaseMigration do
1037
1051
1038
1052
defp deserialize_value ( value ) , do: value
1039
1053
1054
+ defp nullish? ( value ) , do: is_nil ( deserialize_value ( value ) )
1055
+
1040
1056
defp clear_tables! do
1041
1057
commands =
1042
1058
[
@@ -1073,24 +1089,29 @@ defmodule DatabaseMigration do
1073
1089
end
1074
1090
end
1075
1091
1092
+ defp time_step ( description , function ) do
1093
+ IO . puts ( "\n #{ description } ..." )
1094
+ { time , result } = :timer . tc ( function )
1095
+ IO . puts ( "✓ #{ description } completed in #{ time / 1_000_000 } seconds" )
1096
+ result
1097
+ end
1098
+
1076
1099
def run! do
1077
1100
input_file = ".local/prod_db.sql"
1078
1101
output_file = ".local/prod_db_new.sql"
1079
1102
1080
1103
if File . exists? ( input_file ) or File . exists? ( output_file ) do
1081
- IO . puts ( "Processing dump..." )
1082
- :ok = process_dump ( input_file , output_file )
1083
-
1084
- IO . puts ( "Clearing tables..." )
1085
- :ok = clear_tables! ( )
1086
-
1087
- IO . puts ( "Importing new data..." )
1088
- { :ok , _ } = psql ( [ "-f" , output_file ] )
1104
+ IO . puts ( "\n Starting migration..." )
1089
1105
1090
- IO . puts ( "Backfilling repositories..." )
1091
- :ok = Algora.Admin . backfill_repos! ( )
1106
+ { total_time , _ } =
1107
+ :timer . tc ( fn ->
1108
+ :ok = time_step ( "Processing dump" , fn -> process_dump ( input_file , output_file ) end )
1109
+ :ok = time_step ( "Clearing tables" , fn -> clear_tables! ( ) end )
1110
+ { :ok , _ } = time_step ( "Importing new data" , fn -> psql ( [ "-f" , output_file ] ) end )
1111
+ :ok = time_step ( "Backfilling repositories" , fn -> Algora.Admin . backfill_repos! ( ) end )
1112
+ end )
1092
1113
1093
- IO . puts ( "Migration completed successfully" )
1114
+ IO . puts ( "\n ✓ Migration completed successfully in #{ total_time / 1_000_000 } seconds " )
1094
1115
end
1095
1116
end
1096
1117
end
0 commit comments