Apply 1 suggestion(s) to 1 file(s)
[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, where: fragment("? && ?", u.tags, ^legacy_tags))
22 |> Repo.all()
23 |> Enum.each(fn user ->
24 fix_tags_changeset(user)
25 |> Repo.update()
26 end)
27 end
28
29 defp fix_tags_changeset(%User{tags: tags} = user) do
30 new_tags =
31 Enum.map(tags, fn tag ->
32 Map.get(@old_new_map, tag, tag)
33 end)
34
35 Ecto.Changeset.change(user, tags: new_tags)
36 end
37 end