Merge branch '204-fix' into 'develop'
[akkoma] / priv / repo / migrations / 20200802170532_fix_legacy_tags.exs
1 # Fix legacy tags set by AdminFE that don't align with TagPolicy MRF
2
3 defmodule Pleroma.Repo.Migrations.FixLegacyTags do
4 use Ecto.Migration
5 alias Pleroma.Repo
6 alias Pleroma.User
7 import Ecto.Query
8
9 @old_new_map %{
10 "force_nsfw" => "mrf_tag:media-force-nsfw",
11 "strip_media" => "mrf_tag:media-strip",
12 "force_unlisted" => "mrf_tag:force-unlisted",
13 "sandbox" => "mrf_tag:sandbox",
14 "disable_remote_subscription" => "mrf_tag:disable-remote-subscription",
15 "disable_any_subscription" => "mrf_tag:disable-any-subscription"
16 }
17
18 def change do
19 legacy_tags = Map.keys(@old_new_map)
20
21 from(u in User,
22 where: fragment("? && ?", u.tags, ^legacy_tags),
23 select: struct(u, [:tags, :id])
24 )
25 |> Repo.chunk_stream(100)
26 |> Enum.each(fn user ->
27 fix_tags_changeset(user)
28 |> Repo.update()
29 end)
30 end
31
32 defp fix_tags_changeset(%User{tags: tags} = user) do
33 new_tags =
34 Enum.map(tags, fn tag ->
35 Map.get(@old_new_map, tag, tag)
36 end)
37
38 Ecto.Changeset.change(user, tags: new_tags)
39 end
40 end