rewrite this maybe?

This commit is contained in:
Charlotte 🦝 Delenk 2022-09-10 17:58:57 +01:00
parent 723a64ef9d
commit 5dafd44d0b
Signed by: darkkirb
GPG key ID: AB2BD8DAF2E37122

View file

@ -1,15 +1,16 @@
diff --git a/lib/mix/migrator.ex b/lib/mix/migrator.ex diff --git a/lib/mix/migrator.ex b/lib/mix/migrator.ex
new file mode 100644 new file mode 100644
index 000000000..946c2d892 index 000000000..ec7b6f812
--- /dev/null --- /dev/null
+++ b/lib/mix/migrator.ex +++ b/lib/mix/migrator.ex
@@ -0,0 +1,112 @@ @@ -0,0 +1,125 @@
+defmodule Mix.Pleroma.Migrator do +defmodule Mix.Pleroma.Migrator do
+ import Mix.Pleroma + import Mix.Pleroma
+ alias Pleroma.Activity + alias Pleroma.Activity
+ alias Pleroma.Object + alias Pleroma.Object
+ alias Pleroma.Repo + alias Pleroma.Repo
+ alias Pleroma.Web.ActivityPub.Transmogrifier + alias Pleroma.Web.ActivityPub.Transmogrifier
+ alias Pleroma.Web.ActivityPub.ActivityPub
+ +
+ @doc "Common functions to be reused in migrator tasks" + @doc "Common functions to be reused in migrator tasks"
+ def keys_to_atoms(map) do + def keys_to_atoms(map) do
@ -73,6 +74,16 @@ index 000000000..946c2d892
+ activity_params = params + activity_params = params
+ |> Map.delete(:object_data) + |> Map.delete(:object_data)
+ +
+ {:ok, object} = try_create_object(params)
+ if object do
+ try do
+ {:ok, _activity, _meta} = ActivityPub.persist(activity_params, local: true)
+ rescue
+ Ecto.ConstraintError ->
+ shell_info("Activity already in database, skipping")
+ end
+ end
+
+ try do + try do
+ {:ok, _activity} = Repo.insert(struct(Activity, activity_params)) + {:ok, _activity} = Repo.insert(struct(Activity, activity_params))
+ shell_info("Activity created") + shell_info("Activity created")
@ -108,11 +119,13 @@ index 000000000..946c2d892
+ } + }
+ +
+ try do + try do
+ {:ok, _object} = Repo.insert(struct(Object, object_params)) + {:ok, oobject} = Repo.insert(struct(Object, object_params))
+ shell_info("Object created") + shell_info("Object created")
+ {:ok, oobject}
+ rescue + rescue
+ Ecto.ConstraintError -> + Ecto.ConstraintError ->
+ shell_info("Object already in database, skipping") + shell_info("Object already in database, skipping")
+ {nil, nil}
+ end + end
+ end + end
+end +end
@ -499,10 +512,10 @@ index 000000000..1b1e439e0
+end +end
diff --git a/lib/mix/tasks/migrator/import/statuses.ex b/lib/mix/tasks/migrator/import/statuses.ex diff --git a/lib/mix/tasks/migrator/import/statuses.ex b/lib/mix/tasks/migrator/import/statuses.ex
new file mode 100644 new file mode 100644
index 000000000..9d4c46bef index 000000000..e96aa2f3c
--- /dev/null --- /dev/null
+++ b/lib/mix/tasks/migrator/import/statuses.ex +++ b/lib/mix/tasks/migrator/import/statuses.ex
@@ -0,0 +1,24 @@ @@ -0,0 +1,23 @@
+defmodule Mix.Tasks.Pleroma.Migrator.Import.Statuses do +defmodule Mix.Tasks.Pleroma.Migrator.Import.Statuses do
+ use Mix.Task + use Mix.Task
+ import Mix.Pleroma + import Mix.Pleroma
@ -519,7 +532,6 @@ index 000000000..9d4c46bef
+ params = + params =
+ Jason.decode!(line) + Jason.decode!(line)
+ |> keys_to_atoms + |> keys_to_atoms
+ |> loop_fields([:object], &keys_to_atoms/1)
+ |> loop_fields([:inserted_at, :updated_at], &parse_timestamp/1) + |> loop_fields([:inserted_at, :updated_at], &parse_timestamp/1)
+ |> Map.delete(:id) + |> Map.delete(:id)
+ +