From aa64b3108ba6aa4294e541e86da323ba1e1a7243 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Wed, 23 Oct 2019 11:54:52 +0300 Subject: [PATCH] fix migrate --- lib/pleroma/marker.ex | 5 +++-- .../20191021113356_add_unread_to_marker.exs | 18 +++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/pleroma/marker.ex b/lib/pleroma/marker.ex index 4b8198690..098fe3bbd 100644 --- a/lib/pleroma/marker.ex +++ b/lib/pleroma/marker.ex @@ -56,8 +56,9 @@ def multi_set_unread_count(multi, %User{} = user, "notifications") do where: q.user_id == ^user.id, select: %{ timeline: "notifications", - user_id: ^user.id, - unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END ) as unread_count") + user_id: type(^user.id, :string), + unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"), + last_read_id: type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string) } ) diff --git a/priv/repo/migrations/20191021113356_add_unread_to_marker.exs b/priv/repo/migrations/20191021113356_add_unread_to_marker.exs index 32789b7f9..964c7fb98 100644 --- a/priv/repo/migrations/20191021113356_add_unread_to_marker.exs +++ b/priv/repo/migrations/20191021113356_add_unread_to_marker.exs @@ -25,25 +25,21 @@ def update_markers do select: %{ timeline: "notifications", user_id: q.user_id, - unread_count: fragment("COUNT(*) FILTER (WHERE seen = false) as unread_count"), - last_read_id: fragment("(MAX(id) FILTER (WHERE seen = true)::text) as last_read_id ") + unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"), + last_read_id: type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string) }, group_by: [q.user_id] ) |> Repo.all() - |> Enum.reduce(Ecto.Multi.new(), fn attrs, multi -> - marker = - Pleroma.Marker - |> struct(attrs) - |> Ecto.Changeset.change() - - multi - |> Ecto.Multi.insert(attrs[:user_id], marker, + |> Enum.each(fn attrs -> + Pleroma.Marker + |> struct(attrs) + |> Ecto.Changeset.change() + |> Pleroma.Repo.insert( returning: true, on_conflict: {:replace, [:last_read_id, :unread_count]}, conflict_target: [:user_id, :timeline] ) end) - |> Pleroma.Repo.transaction() end end