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