First
[anni] / priv / repo / migrations / 20200831142509_chat_constraints.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Repo.Migrations.ChatConstraints do
6   use Ecto.Migration
7
8   def change do
9     remove_orphans = """
10     delete from chats where not exists(select id from users where ap_id = chats.recipient);
11     """
12
13     execute(remove_orphans)
14
15     drop(constraint(:chats, "chats_user_id_fkey"))
16
17     alter table(:chats) do
18       modify(:user_id, references(:users, type: :uuid, on_delete: :delete_all))
19
20       modify(
21         :recipient,
22         references(:users, column: :ap_id, type: :string, on_delete: :delete_all)
23       )
24     end
25   end
26 end