move to 2.5.5
[anni] / priv / repo / migrations / 20201005123100_simple_policy_string_to_tuple.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.SimplePolicyStringToTuple do
6   use Ecto.Migration
7
8   alias Pleroma.ConfigDB
9
10   def up, do: ConfigDB.get_by_params(%{group: :pleroma, key: :mrf_simple}) |> update_to_tuples
11   def down, do: ConfigDB.get_by_params(%{group: :pleroma, key: :mrf_simple}) |> update_to_strings
12
13   defp update_to_tuples(%{value: value}) do
14     new_value =
15       value
16       |> Enum.map(fn {k, v} ->
17         {k,
18          Enum.map(v, fn
19            {instance, reason} -> {instance, reason}
20            instance -> {instance, ""}
21          end)}
22       end)
23
24     ConfigDB.update_or_create(%{group: :pleroma, key: :mrf_simple, value: new_value})
25   end
26
27   defp update_to_tuples(nil), do: {:ok, nil}
28
29   defp update_to_strings(%{value: value}) do
30     new_value =
31       value
32       |> Enum.map(fn {k, v} ->
33         {k,
34          Enum.map(v, fn
35            {instance, _} -> instance
36            instance -> instance
37          end)}
38       end)
39
40     ConfigDB.update_or_create(%{group: :pleroma, key: :mrf_simple, value: new_value})
41   end
42
43   defp update_to_strings(nil), do: {:ok, nil}
44 end