summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20201005123100_simple_policy_string_to_tuple.exs
blob: 77a4a73116c325e4727aa1a440da285bf8926cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
defmodule Pleroma.Repo.Migrations.SimplePolicyStringToTuple do
  use Ecto.Migration

  alias Pleroma.ConfigDB

  def up, do: ConfigDB.get_by_params(%{group: :pleroma, key: :mrf_simple}) |> update_to_tuples
  def down, do: ConfigDB.get_by_params(%{group: :pleroma, key: :mrf_simple}) |> update_to_strings

  defp update_to_tuples(%{value: value}) do
    new_value =
      value
      |> Enum.map(fn {k, v} ->
        {k,
         Enum.map(v, fn
           {instance, reason} -> {instance, reason}
           instance -> {instance, ""}
         end)}
      end)

    ConfigDB.update_or_create(%{group: :pleroma, key: :mrf_simple, value: new_value})
  end

  defp update_to_tuples(nil), do: {:ok, nil}

  defp update_to_strings(%{value: value}) do
    new_value =
      value
      |> Enum.map(fn {k, v} ->
        {k,
         Enum.map(v, fn
           {instance, _} -> instance
           instance -> instance
         end)}
      end)

    ConfigDB.update_or_create(%{group: :pleroma, key: :mrf_simple, value: new_value})
  end

  defp update_to_strings(nil), do: {:ok, nil}
end