fix migrate

This commit is contained in:
Maksim Pechnikov 2019-10-23 11:54:52 +03:00
parent 9a4afbd2a0
commit aa64b3108b
2 changed files with 10 additions and 13 deletions

View File

@ -56,8 +56,9 @@ defmodule Pleroma.Marker do
where: q.user_id == ^user.id, where: q.user_id == ^user.id,
select: %{ select: %{
timeline: "notifications", timeline: "notifications",
user_id: ^user.id, user_id: type(^user.id, :string),
unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END ) as unread_count") 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)
} }
) )

View File

@ -25,25 +25,21 @@ defmodule Pleroma.Repo.Migrations.AddUnreadToMarker do
select: %{ select: %{
timeline: "notifications", timeline: "notifications",
user_id: q.user_id, user_id: q.user_id,
unread_count: fragment("COUNT(*) FILTER (WHERE seen = false) as unread_count"), unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"),
last_read_id: fragment("(MAX(id) FILTER (WHERE seen = true)::text) as last_read_id ") last_read_id: type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
}, },
group_by: [q.user_id] group_by: [q.user_id]
) )
|> Repo.all() |> Repo.all()
|> Enum.reduce(Ecto.Multi.new(), fn attrs, multi -> |> Enum.each(fn attrs ->
marker = Pleroma.Marker
Pleroma.Marker |> struct(attrs)
|> struct(attrs) |> Ecto.Changeset.change()
|> Ecto.Changeset.change() |> Pleroma.Repo.insert(
multi
|> Ecto.Multi.insert(attrs[:user_id], marker,
returning: true, returning: true,
on_conflict: {:replace, [:last_read_id, :unread_count]}, on_conflict: {:replace, [:last_read_id, :unread_count]},
conflict_target: [:user_id, :timeline] conflict_target: [:user_id, :timeline]
) )
end) end)
|> Pleroma.Repo.transaction()
end end
end end