logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20220905011454_generate_unset_user_keys.exs (793B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule User do
  5. use Ecto.Schema
  6. schema "users" do
  7. field(:keys, :string)
  8. field(:local, :boolean, default: true)
  9. end
  10. end
  11. defmodule Pleroma.Repo.Migrations.GenerateUnsetUserKeys do
  12. use Ecto.Migration
  13. import Ecto.Query
  14. alias Pleroma.Keys
  15. alias Pleroma.Repo
  16. def change do
  17. query =
  18. from(u in User,
  19. where: u.local == true,
  20. where: is_nil(u.keys),
  21. select: u
  22. )
  23. Repo.stream(query)
  24. |> Enum.each(fn user ->
  25. with {:ok, pem} <- Keys.generate_rsa_pem() do
  26. Ecto.Changeset.cast(user, %{keys: pem}, [:keys])
  27. |> Repo.update()
  28. end
  29. end)
  30. end
  31. end