logo

pleroma

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

20200415181818_update_markers.exs (1222B)


  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.UpdateMarkers do
  5. use Ecto.Migration
  6. import Ecto.Query
  7. alias Pleroma.Repo
  8. def up do
  9. update_markers()
  10. end
  11. def down do
  12. :ok
  13. end
  14. defp update_markers do
  15. now = NaiveDateTime.utc_now()
  16. markers_attrs =
  17. from(q in "notifications",
  18. select: %{
  19. timeline: "notifications",
  20. user_id: q.user_id,
  21. last_read_id:
  22. type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
  23. },
  24. group_by: [q.user_id]
  25. )
  26. |> Repo.all()
  27. |> Enum.map(fn %{last_read_id: last_read_id} = attrs ->
  28. attrs
  29. |> Map.put(:last_read_id, last_read_id || "")
  30. |> Map.put_new(:inserted_at, now)
  31. |> Map.put_new(:updated_at, now)
  32. end)
  33. markers_attrs
  34. |> Enum.chunk_every(1000)
  35. |> Enum.each(fn markers_attrs_chunked ->
  36. Repo.insert_all("markers", markers_attrs_chunked,
  37. on_conflict: {:replace, [:last_read_id]},
  38. conflict_target: [:user_id, :timeline]
  39. )
  40. end)
  41. end
  42. end