CI: Bump lint stage to elixir-1.12
[akkoma] / priv / repo / migrations / 20200428221338_insert_skeletons_for_deleted_users.exs
1 defmodule Pleroma.Repo.Migrations.InsertSkeletonsForDeletedUsers do
2 use Ecto.Migration
3
4 alias Pleroma.User
5 alias Pleroma.Repo
6
7 import Ecto.Query
8
9 def change do
10 Application.ensure_all_started(:flake_id)
11
12 local_ap_id =
13 User.Query.build(%{local: true})
14 |> select([u], u.ap_id)
15 |> limit(1)
16 |> Repo.one()
17
18 unless local_ap_id == nil do
19 # Hack to get instance base url because getting it from Phoenix
20 # would require starting the whole application
21 instance_uri =
22 local_ap_id
23 |> URI.parse()
24 |> Map.put(:query, nil)
25 |> Map.put(:path, nil)
26 |> URI.to_string()
27
28 {:ok, %{rows: ap_ids}} =
29 Ecto.Adapters.SQL.query(
30 Repo,
31 "select distinct unnest(nonexistent_locals.recipients) from activities, lateral (select array_agg(recipient) as recipients from unnest(activities.recipients) as recipient where recipient similar to '#{instance_uri}/users/[A-Za-z0-9]*' and not(recipient in (select ap_id from users))) nonexistent_locals;",
32 [],
33 timeout: :infinity
34 )
35
36 ap_ids
37 |> Enum.each(fn [ap_id] ->
38 Ecto.Changeset.change(%User{}, deactivated: true, ap_id: ap_id)
39 |> Repo.insert()
40 end)
41 end
42 end
43 end