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,
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)
}
)

View File

@ -25,25 +25,21 @@ defmodule Pleroma.Repo.Migrations.AddUnreadToMarker 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