Migrations: generate unset user keys
authorHélène <pleroma-dev@helene.moe>
Mon, 5 Sep 2022 01:51:35 +0000 (03:51 +0200)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 11 Sep 2022 18:53:31 +0000 (19:53 +0100)
User keys are now generated on user creation instead of "when needed",
to prevent race conditions in federation and a few other issues. This
migration will generate keys missing for local users.

priv/repo/migrations/20220905011454_generate_unset_user_keys.exs [new file with mode: 0644]

diff --git a/priv/repo/migrations/20220905011454_generate_unset_user_keys.exs b/priv/repo/migrations/20220905011454_generate_unset_user_keys.exs
new file mode 100644 (file)
index 0000000..43bc710
--- /dev/null
@@ -0,0 +1,28 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Repo.Migrations.GenerateUnsetUserKeys do
+  use Ecto.Migration
+  import Ecto.Query
+  alias Pleroma.Keys
+  alias Pleroma.Repo
+  alias Pleroma.User
+
+  def change do
+    query =
+      from(u in User,
+        where: u.local == true,
+        where: is_nil(u.keys),
+        select: u
+      )
+
+    Repo.stream(query)
+    |> Enum.each(fn user ->
+      with {:ok, pem} <- Keys.generate_rsa_pem() do
+        Ecto.Changeset.cast(user, %{keys: pem}, [:keys])
+        |> Repo.update()
+      end
+    end)
+  end
+end