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.
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.", "")
31 {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
32 Mix.shell().info("#{key} is migrated.")
35 Mix.shell().info("Settings migrated.")
38 "Migration is not allowed by config. You can change this behavior in instance settings."
43 def run(["migrate_from_db", env, delete?]) do
46 delete? = if delete? == "true", do: true, else: false
48 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
49 config_path = "config/#{env}.exported_from_db.secret.exs"
51 {:ok, file} = File.open(config_path, [:write])
52 IO.write(file, "use Mix.Config\r\n")
55 |> Enum.each(fn config ->
57 if String.starts_with?(config.key, "Pleroma.") or
58 String.starts_with?(config.key, "Ueberauth"),
64 "config :#{config.group}, #{config.key}#{mark} #{
65 inspect(Config.from_binary(config.value))
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."