2020-02-10 06:01:45 +00:00
|
|
|
defmodule Pleroma.Repo.Migrations.UpdateMarkers do
|
2019-10-30 20:22:38 +00:00
|
|
|
use Ecto.Migration
|
|
|
|
import Ecto.Query
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
def up do
|
2019-11-02 18:19:01 +00:00
|
|
|
update_markers()
|
2019-10-30 20:22:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def down do
|
2020-02-10 06:01:45 +00:00
|
|
|
:ok
|
2019-10-30 20:22:38 +00:00
|
|
|
end
|
2019-11-02 18:19:01 +00:00
|
|
|
|
2020-02-10 06:01:45 +00:00
|
|
|
defp update_markers do
|
2019-11-02 18:19:01 +00:00
|
|
|
now = NaiveDateTime.utc_now()
|
|
|
|
|
|
|
|
markers_attrs =
|
|
|
|
from(q in "notifications",
|
|
|
|
select: %{
|
|
|
|
timeline: "notifications",
|
|
|
|
user_id: q.user_id,
|
|
|
|
last_read_id:
|
|
|
|
type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
|
|
|
|
},
|
|
|
|
group_by: [q.user_id]
|
|
|
|
)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.map(fn attrs ->
|
|
|
|
attrs
|
|
|
|
|> Map.put_new(:inserted_at, now)
|
|
|
|
|> Map.put_new(:updated_at, now)
|
|
|
|
end)
|
|
|
|
|
|
|
|
Repo.insert_all("markers", markers_attrs,
|
|
|
|
on_conflict: {:replace, [:last_read_id, :unread_count]},
|
|
|
|
conflict_target: [:user_id, :timeline]
|
|
|
|
)
|
|
|
|
end
|
2019-10-30 20:22:38 +00:00
|
|
|
end
|