1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Config do
9 alias Pleroma.Web.AdminAPI.Config
10 @shortdoc "Manages the location of the config"
12 Manages the location of the config.
14 ## Transfers config from file to DB.
16 mix pleroma.config migrate_to_db
18 ## Transfers config from DB to file `config/env.exported_from_db.secret.exs`
20 mix pleroma.config migrate_from_db ENV
23 def run(["migrate_to_db"]) do
26 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
27 Application.get_all_env(:pleroma)
28 |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
29 |> Enum.each(fn {k, v} ->
30 key = to_string(k) |> String.replace("Elixir.", "")
33 if String.starts_with?(key, "Pleroma.") do
39 {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
40 Mix.shell().info("#{key} is migrated.")
43 Mix.shell().info("Settings migrated.")
46 "Migration is not allowed by config. You can change this behavior in instance settings."
51 def run(["migrate_from_db", env, delete?]) do
54 delete? = if delete? == "true", do: true, else: false
56 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
57 config_path = "config/#{env}.exported_from_db.secret.exs"
59 {:ok, file} = File.open(config_path, [:write])
60 IO.write(file, "use Mix.Config\r\n")
63 |> Enum.each(fn config ->
66 "config :#{config.group}, #{config.key}, #{inspect(Config.from_binary(config.value))}\r\n\r\n"
70 {:ok, _} = Repo.delete(config)
71 Mix.shell().info("#{config.key} deleted from DB.")
76 System.cmd("mix", ["format", config_path])
79 "Migration is not allowed by config. You can change this behavior in instance settings."