logo

pleroma

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

20190414125034_migrate_old_bookmarks.exs (1064B)


  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 Pleroma.Repo.Migrations.MigrateOldBookmarks do
  5. use Ecto.Migration
  6. import Ecto.Query
  7. alias Pleroma.Activity
  8. alias Pleroma.Bookmark
  9. alias Pleroma.Repo
  10. def up do
  11. query =
  12. from(u in "users",
  13. where: u.local == true,
  14. where: fragment("array_length(?, 1)", u.bookmarks) > 0,
  15. select: %{id: u.id, bookmarks: u.bookmarks}
  16. )
  17. Repo.stream(query)
  18. |> Enum.each(fn %{id: user_id, bookmarks: bookmarks} ->
  19. Enum.each(bookmarks, fn ap_id ->
  20. activity =
  21. ap_id
  22. |> Activity.create_by_object_ap_id()
  23. |> Repo.one()
  24. unless is_nil(activity), do: {:ok, _} = Bookmark.create(user_id, activity.id)
  25. end)
  26. end)
  27. alter table(:users) do
  28. remove(:bookmarks)
  29. end
  30. end
  31. def down do
  32. alter table(:users) do
  33. add(:bookmarks, {:array, :string}, null: false, default: [])
  34. end
  35. end
  36. end