From b6891fe190d3ae186d777ab34c4778c5f48fa18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9l=C3=A8ne?= Date: Mon, 5 Sep 2022 03:51:35 +0200 Subject: [PATCH] Migrations: generate unset user keys 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. --- ...0220905011454_generate_unset_user_keys.exs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 priv/repo/migrations/20220905011454_generate_unset_user_keys.exs 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 index 000000000..43bc7100b --- /dev/null +++ b/priv/repo/migrations/20220905011454_generate_unset_user_keys.exs @@ -0,0 +1,28 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# 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