2020-02-10 01:01:45 -05:00
|
|
|
defmodule Pleroma.Repo.Migrations.UpdateMarkers do
|
2019-10-30 16:22:38 -04:00
|
|
|
use Ecto.Migration
|
|
|
|
import Ecto.Query
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
def up do
|
2019-11-02 14:19:01 -04:00
|
|
|
update_markers()
|
2019-10-30 16:22:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def down do
|
2020-02-10 01:01:45 -05:00
|
|
|
:ok
|
2019-10-30 16:22:38 -04:00
|
|
|
end
|
2019-11-02 14:19:01 -04:00
|
|
|
|
2020-02-10 01:01:45 -05:00
|
|
|
defp update_markers do
|
2019-11-02 14:19:01 -04: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,
|
2020-04-14 12:31:30 -04:00
|
|
|
on_conflict: {:replace, [:last_read_id, :unread_count]},
|
2019-11-02 14:19:01 -04:00
|
|
|
conflict_target: [:user_id, :timeline]
|
|
|
|
)
|
|
|
|
end
|
2019-10-30 16:22:38 -04:00
|
|
|
end
|