summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20200604150318_migrate_seen_to_unread_in_chat_message_references.exs
blob: fd6bc7bc7ced09410ba4d3ef8cd443a2eb8374b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
defmodule Pleroma.Repo.Migrations.MigrateSeenToUnreadInChatMessageReferences do
  use Ecto.Migration

  def change do
    drop(
      index(:chat_message_references, [:chat_id],
        where: "seen = false",
        name: "unseen_messages_count_index"
      )
    )

    alter table(:chat_message_references) do
      add(:unread, :boolean, default: true)
    end

    execute("update chat_message_references set unread = not seen")

    alter table(:chat_message_references) do
      modify(:unread, :boolean, default: true, null: false)
      remove(:seen, :boolean, default: false, null: false)
    end

    create(
      index(:chat_message_references, [:chat_id],
        where: "unread = true",
        name: "unread_messages_count_index"
      )
    )
  end
end