akkoma/priv/repo/migrations/20200210050658_update_markers.exs

40 lines
874 B
Elixir
Raw Normal View History

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