Merge branch 'fix-for-new-isntances' into 'develop'
[akkoma] / lib / pleroma / config / transfer_task.ex
1 defmodule Pleroma.Config.TransferTask do
2 use Task
3 alias Pleroma.Web.AdminAPI.Config
4
5 def start_link do
6 load_and_update_env()
7 if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Pleroma.Repo)
8 :ignore
9 end
10
11 def load_and_update_env do
12 if Pleroma.Config.get([:instance, :dynamic_configuration]) and
13 Ecto.Adapters.SQL.table_exists?(Pleroma.Repo, "config") do
14 Pleroma.Repo.all(Config)
15 |> Enum.each(&update_env(&1))
16 end
17 end
18
19 defp update_env(setting) do
20 try do
21 key =
22 if String.starts_with?(setting.key, "Pleroma.") do
23 "Elixir." <> setting.key
24 else
25 setting.key
26 end
27
28 Application.put_env(
29 :pleroma,
30 String.to_existing_atom(key),
31 Config.from_binary(setting.value)
32 )
33 rescue
34 e ->
35 require Logger
36
37 Logger.warn(
38 "updating env causes error, key: #{inspect(setting.key)}, error: #{inspect(e)}"
39 )
40 end
41 end
42 end