2020-02-09 23:01:45 -07:00
|
|
|
defmodule Pleroma.Repo.Migrations.UpdateMarkers do
|
2019-10-30 14:22:38 -06:00
|
|
|
use Ecto.Migration
|
|
|
|
import Ecto.Query
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
def up do
|
2019-11-02 12:19:01 -06:00
|
|
|
update_markers()
|
2019-10-30 14:22:38 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def down do
|
2020-02-09 23:01:45 -07:00
|
|
|
:ok
|
2019-10-30 14:22:38 -06:00
|
|
|
end
|
2019-11-02 12:19:01 -06:00
|
|
|
|
2020-02-09 23:01:45 -07:00
|
|
|
defp update_markers do
|
2019-11-02 12:19:01 -06: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 14:22:38 -06:00
|
|
|
end
|