Skip to content

Commit f01a3b5

Browse files
committed
fix miscellaneous issues
1 parent 8e7b2ea commit f01a3b5

File tree

5 files changed

+148
-58
lines changed

5 files changed

+148
-58
lines changed

lib/algora/payments/schemas/account.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Algora.Payments.Account do
99
typed_schema "accounts" do
1010
field :provider, :string, null: false
1111
field :provider_id, :string, null: false
12-
field :provider_meta, :map, null: false
12+
field :provider_meta, :map
1313

1414
field :name, :string
1515
field :details_submitted, :boolean, default: false, null: false
@@ -19,7 +19,7 @@ defmodule Algora.Payments.Account do
1919
field :payout_speed, :integer
2020
field :default_currency, :string
2121
field :service_agreement, :string
22-
field :country, :string, null: false
22+
field :country, :string
2323
field :type, Ecto.Enum, values: [:standard, :express], null: false
2424
field :stale, :boolean, default: false, null: false
2525

lib/algora/workspace/schemas/command_response.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule Algora.Workspace.CommandResponse do
77

88
typed_schema "command_responses" do
99
field :provider, :string, null: false
10-
field :provider_meta, :map, null: false
10+
field :provider_meta, :map
1111
field :provider_command_id, :string
1212
field :provider_response_id, :string, null: false
1313
field :command_source, Ecto.Enum, values: [:ticket, :comment], null: false

lib/algora/workspace/schemas/installation.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ defmodule Algora.Workspace.Installation do
99
typed_schema "installations" do
1010
field :provider, :string, null: false
1111
field :provider_id, :string, null: false
12-
field :provider_meta, :map, null: false
13-
field :provider_user_id, :string, null: false
12+
field :provider_meta, :map
13+
field :provider_user_id, :string
1414

1515
field :avatar_url, :string
1616
field :repository_selection, :string
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
defmodule Algora.Repo.Migrations.MakeProviderMetaNullable do
2+
use Ecto.Migration
3+
4+
def up do
5+
alter table(:command_responses) do
6+
modify :provider_meta, :map, null: true
7+
end
8+
9+
alter table(:installations) do
10+
modify :provider_meta, :map, null: true
11+
modify :provider_user_id, :string, null: true
12+
end
13+
14+
alter table(:customers) do
15+
modify :provider_meta, :map, null: true
16+
end
17+
18+
alter table(:payment_methods) do
19+
modify :provider_meta, :map, null: true
20+
end
21+
22+
alter table(:accounts) do
23+
modify :provider_meta, :map, null: true
24+
modify :country, :string, null: true
25+
end
26+
27+
alter table(:identities) do
28+
modify :provider_meta, :map, null: true
29+
modify :provider_login, :string, null: true
30+
end
31+
32+
alter table(:claims) do
33+
modify :source_id, :string, null: true
34+
end
35+
end
36+
37+
def down do
38+
alter table(:command_responses) do
39+
modify :provider_meta, :map, null: false
40+
end
41+
42+
alter table(:installations) do
43+
modify :provider_meta, :map, null: false
44+
modify :provider_user_id, :string, null: false
45+
end
46+
47+
alter table(:customers) do
48+
modify :provider_meta, :map, null: false
49+
end
50+
51+
alter table(:payment_methods) do
52+
modify :provider_meta, :map, null: false
53+
end
54+
55+
alter table(:accounts) do
56+
modify :provider_meta, :map, null: false
57+
modify :country, :string, null: false
58+
end
59+
60+
alter table(:identities) do
61+
modify :provider_meta, :map, null: false
62+
modify :provider_login, :string, null: false
63+
end
64+
65+
alter table(:claims) do
66+
modify :source_id, :string, null: false
67+
end
68+
end
69+
end

scripts/database_migration.exs

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ defmodule DatabaseMigration do
130130
"description" => row["body"],
131131
"number" => row["number"],
132132
"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"
135135
}
136136
end
137137

@@ -298,13 +298,19 @@ defmodule DatabaseMigration do
298298

299299
defp transform({"GithubUser", User}, _row, _db), do: nil
300300

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+
302308
%{
303309
"id" => row["id"],
304-
"user_id" => row["\"userId\""],
310+
"user_id" => user["id"],
305311
"provider" => row["provider"],
306312
"provider_token" => row["access_token"],
307-
"provider_email" => nil,
313+
"provider_email" => user["email"],
308314
"provider_login" => nil,
309315
"provider_id" => row["\"providerAccountId\""],
310316
"provider_meta" => nil,
@@ -341,7 +347,11 @@ defmodule DatabaseMigration do
341347
end
342348

343349
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
345355
%{
346356
"id" => row["id"],
347357
"provider" => "github",
@@ -362,7 +372,7 @@ defmodule DatabaseMigration do
362372

363373
github_user = db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == row["github_user_id"]))
364374

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"]))
366376

367377
if !bounty do
368378
raise "Bounty not found: #{inspect(row)}"
@@ -390,7 +400,7 @@ defmodule DatabaseMigration do
390400

391401
github_user = db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == row["github_user_id"]))
392402

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"]))
394404

395405
# TODO: this might be null
396406
github_pull_request =
@@ -402,10 +412,11 @@ defmodule DatabaseMigration do
402412

403413
%{
404414
"id" => row["id"],
405-
"status" => nil,
415+
# TODO:
416+
"status" => :pending,
406417
"type" =>
407418
cond do
408-
row["github_pull_request_id"] != nil -> "pull_request"
419+
!nullish?(row["github_pull_request_id"]) -> "pull_request"
409420
String.match?(row["github_url"] || "", ~r{^https?://(?:www\.)?figma\.com/}) -> "design"
410421
true -> "pull_request"
411422
end,
@@ -468,9 +479,9 @@ defmodule DatabaseMigration do
468479
defp transform({"BountyTransfer", Tip}, row, db) do
469480
claim = db |> Map.get("Claim", []) |> Enum.find(&(&1["id"] == row["claim_id"]))
470481

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"]))
472483

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"]))
474485

475486
bounty = db |> Map.get("Bounty", []) |> Enum.find(&(&1["id"] == claim["bounty_id"]))
476487

@@ -506,7 +517,7 @@ defmodule DatabaseMigration do
506517

507518
github_user = db |> Map.get("GithubUser", []) |> Enum.find(&(&1["id"] == claim["github_user_id"]))
508519

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"]))
510521

511522
org = db |> Map.get("Org", []) |> Enum.find(&(&1["id"] == bounty["org_id"]))
512523

@@ -528,29 +539,32 @@ defmodule DatabaseMigration do
528539
raise "BountyCharge not found: #{inspect(row)}"
529540
end
530541

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+
)
554568
end
555569

556570
defp transform({"GithubInstallation", Installation}, row, _db) do
@@ -573,7 +587,7 @@ defmodule DatabaseMigration do
573587
%{
574588
"id" => row["id"],
575589
"provider" => "stripe",
576-
"provider_id" => row["stripe_id"],
590+
"provider_id" => row["id"],
577591
"provider_meta" => nil,
578592
"name" => nil,
579593
"details_submitted" => row["details_submitted"],
@@ -606,7 +620,7 @@ defmodule DatabaseMigration do
606620
end
607621

608622
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"]))
610624

611625
if !customer do
612626
raise "StripeCustomer not found: #{inspect(row)}"
@@ -619,7 +633,7 @@ defmodule DatabaseMigration do
619633
"provider_meta" => nil,
620634
"provider_customer_id" => customer["stripe_id"],
621635
"is_default" => row["is_default"],
622-
"customer_id" => row["customer_id"],
636+
"customer_id" => customer["id"],
623637
"inserted_at" => row["created_at"],
624638
"updated_at" => row["updated_at"]
625639
}
@@ -694,23 +708,23 @@ defmodule DatabaseMigration do
694708
end
695709

696710
cond do
697-
type == "transfer" && bounty_transfer["succeeded_at"] != nil ->
711+
type == "transfer" && !nullish?(bounty_transfer["succeeded_at"]) ->
698712
Map.merge(res, %{
699713
"status" => :succeeded,
700714
"succeeded_at" => bounty_transfer["succeeded_at"],
701715
"inserted_at" => bounty_transfer["created_at"],
702716
"updated_at" => bounty_transfer["updated_at"]
703717
})
704718

705-
type == "debit" && bounty_charge["succeeded_at"] != nil ->
719+
type == "debit" && !nullish?(bounty_charge["succeeded_at"]) ->
706720
Map.merge(res, %{
707721
"status" => :succeeded,
708722
"succeeded_at" => bounty_charge["succeeded_at"],
709723
"inserted_at" => bounty_charge["succeeded_at"],
710724
"updated_at" => bounty_charge["succeeded_at"]
711725
})
712726

713-
type == "credit" && bounty_charge["succeeded_at"] != nil ->
727+
type == "credit" && !nullish?(bounty_charge["succeeded_at"]) ->
714728
Map.merge(res, %{
715729
"status" => :succeeded,
716730
"succeeded_at" => bounty_charge["succeeded_at"],
@@ -1037,6 +1051,8 @@ defmodule DatabaseMigration do
10371051

10381052
defp deserialize_value(value), do: value
10391053

1054+
defp nullish?(value), do: is_nil(deserialize_value(value))
1055+
10401056
defp clear_tables! do
10411057
commands =
10421058
[
@@ -1073,24 +1089,29 @@ defmodule DatabaseMigration do
10731089
end
10741090
end
10751091

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+
10761099
def run! do
10771100
input_file = ".local/prod_db.sql"
10781101
output_file = ".local/prod_db_new.sql"
10791102

10801103
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("\nStarting migration...")
10891105

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)
10921113

1093-
IO.puts("Migration completed successfully")
1114+
IO.puts("\nMigration completed successfully in #{total_time / 1_000_000} seconds")
10941115
end
10951116
end
10961117
end

0 commit comments

Comments
 (0)