Merge branch 'develop' into issue/1276
[akkoma] / priv / repo / migrations / 20200210050658_update_markers.exs
1 defmodule Pleroma.Repo.Migrations.UpdateMarkers do
2 use Ecto.Migration
3 import Ecto.Query
4 alias Pleroma.Repo
5
6 def up do
7 update_markers()
8 end
9
10 def down do
11 :ok
12 end
13
14 defp update_markers do
15 now = NaiveDateTime.utc_now()
16
17 markers_attrs =
18 from(q in "notifications",
19 select: %{
20 timeline: "notifications",
21 user_id: q.user_id,
22 last_read_id:
23 type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
24 },
25 group_by: [q.user_id]
26 )
27 |> Repo.all()
28 |> Enum.map(fn attrs ->
29 attrs
30 |> Map.put_new(:inserted_at, now)
31 |> Map.put_new(:updated_at, now)
32 end)
33
34 Repo.insert_all("markers", markers_attrs,
35 on_conflict: {:replace, [:last_read_id, :unread_count]},
36 conflict_target: [:user_id, :timeline]
37 )
38 end
39 end