move to 2.5.5
[anni] / priv / repo / migrations / 20201005124600_quarantained_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.QuarantainedStringToTuple do
6   use Ecto.Migration
7
8   alias Pleroma.ConfigDB
9
10   def up,
11     do:
12       ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
13       |> update_quarantined_instances_to_tuples
14
15   def down,
16     do:
17       ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
18       |> update_quarantined_instances_to_strings
19
20   defp update_quarantined_instances_to_tuples(%{value: settings}) do
21     settings |> List.keyfind(:quarantined_instances, 0) |> update_to_tuples
22   end
23
24   defp update_quarantined_instances_to_tuples(nil), do: {:ok, nil}
25
26   defp update_to_tuples({:quarantined_instances, instance_list}) do
27     new_value =
28       instance_list
29       |> Enum.map(fn
30         {v, r} -> {v, r}
31         v -> {v, ""}
32       end)
33
34     ConfigDB.update_or_create(%{
35       group: :pleroma,
36       key: :instance,
37       value: [quarantined_instances: new_value]
38     })
39   end
40
41   defp update_to_tuples(nil), do: {:ok, nil}
42
43   defp update_quarantined_instances_to_strings(%{value: settings}) do
44     settings |> List.keyfind(:quarantined_instances, 0) |> update_to_strings
45   end
46
47   defp update_quarantined_instances_to_strings(nil), do: {:ok, nil}
48
49   defp update_to_strings({:quarantined_instances, instance_list}) do
50     new_value =
51       instance_list
52       |> Enum.map(fn
53         {v, _} -> v
54         v -> v
55       end)
56
57     ConfigDB.update_or_create(%{
58       group: :pleroma,
59       key: :instance,
60       value: [quarantined_instances: new_value]
61     })
62   end
63
64   defp update_to_strings(nil), do: {:ok, nil}
65 end