Add database migrations
[akkoma] / priv / repo / migrations / 20201005123100_simple_policy_string_to_tuple.exs
1 defmodule Pleroma.Repo.Migrations.SimplePolicyStringToTuple do
2 use Ecto.Migration
3
4 alias Pleroma.ConfigDB
5
6 def up, do: ConfigDB.get_by_params(%{group: :pleroma, key: :mrf_simple}) |> update_to_tuples
7 def down, do: ConfigDB.get_by_params(%{group: :pleroma, key: :mrf_simple}) |> update_to_strings
8
9 defp update_to_tuples(%{value: value}) do
10 new_value =
11 value
12 |> Enum.map(fn {k, v} ->
13 {k,
14 Enum.map(v, fn
15 {instance, reason} -> {instance, reason}
16 instance -> {instance, ""}
17 end)}
18 end)
19
20 ConfigDB.update_or_create(%{group: :pleroma, key: :mrf_simple, value: new_value})
21 end
22
23 defp update_to_tuples(nil), do: {:ok, nil}
24
25 defp update_to_strings(%{value: value}) do
26 new_value =
27 value
28 |> Enum.map(fn {k, v} ->
29 {k,
30 Enum.map(v, fn
31 {instance, _} -> instance
32 instance -> instance
33 end)}
34 end)
35
36 ConfigDB.update_or_create(%{group: :pleroma, key: :mrf_simple, value: new_value})
37 end
38
39 defp update_to_strings(nil), do: {:ok, nil}
40 end