mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-25 04:53:06 +00:00
Migrate legacy tags set by AdminFE to match TagPolicy, #2010
This commit is contained in:
parent
70951d042b
commit
c2c3dd4613
37
priv/repo/migrations/20200802170532_fix_legacy_tags.exs
Normal file
37
priv/repo/migrations/20200802170532_fix_legacy_tags.exs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
# Fix legacy tags set by AdminFE that don't align with TagPolicy MRF
|
||||||
|
|
||||||
|
defmodule Pleroma.Repo.Migrations.FixLegacyTags do
|
||||||
|
use Ecto.Migration
|
||||||
|
alias Pleroma.Repo
|
||||||
|
alias Pleroma.User
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
@old_new_map %{
|
||||||
|
"force_nsfw" => "mrf_tag:media-force-nsfw",
|
||||||
|
"strip_media" => "mrf_tag:media-strip",
|
||||||
|
"force_unlisted" => "mrf_tag:force-unlisted",
|
||||||
|
"sandbox" => "mrf_tag:sandbox",
|
||||||
|
"disable_remote_subscription" => "mrf_tag:disable-remote-subscription",
|
||||||
|
"disable_any_subscription" => "mrf_tag:disable-any-subscription"
|
||||||
|
}
|
||||||
|
|
||||||
|
def change do
|
||||||
|
legacy_tags = Map.keys(@old_new_map)
|
||||||
|
|
||||||
|
from(u in User, where: fragment("? && ?", u.tags, ^legacy_tags))
|
||||||
|
|> Repo.all()
|
||||||
|
|> Enum.each(fn user ->
|
||||||
|
fix_tags_changeset(user)
|
||||||
|
|> Repo.update()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fix_tags_changeset(%User{tags: tags} = user) do
|
||||||
|
new_tags =
|
||||||
|
Enum.map(tags, fn tag ->
|
||||||
|
Map.get(@old_new_map, tag, tag)
|
||||||
|
end)
|
||||||
|
|
||||||
|
Ecto.Changeset.change(user, tags: new_tags)
|
||||||
|
end
|
||||||
|
end
|
24
test/migrations/20200802170532_fix_legacy_tags_test.exs
Normal file
24
test/migrations/20200802170532_fix_legacy_tags_test.exs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
defmodule Pleroma.Repo.Migrations.FixLegacyTagsTest do
|
||||||
|
alias Pleroma.User
|
||||||
|
use Pleroma.DataCase
|
||||||
|
import Pleroma.Factory
|
||||||
|
import Pleroma.Tests.Helpers
|
||||||
|
|
||||||
|
setup_all do: require_migration("20200802170532_fix_legacy_tags")
|
||||||
|
|
||||||
|
test "change/0 converts legacy user tags into correct values", %{migration: migration} do
|
||||||
|
user = insert(:user, tags: ["force_nsfw", "force_unlisted", "verified"])
|
||||||
|
user2 = insert(:user)
|
||||||
|
|
||||||
|
assert :ok == migration.change()
|
||||||
|
|
||||||
|
fixed_user = User.get_by_id(user.id)
|
||||||
|
fixed_user2 = User.get_by_id(user2.id)
|
||||||
|
|
||||||
|
assert fixed_user.tags == ["mrf_tag:media-force-nsfw", "mrf_tag:force-unlisted", "verified"]
|
||||||
|
assert fixed_user2.tags == []
|
||||||
|
|
||||||
|
# user2 should not have been updated
|
||||||
|
assert fixed_user2.updated_at == fixed_user2.inserted_at
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue