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
10 alias Pleroma.ConfigDB
13 @shortdoc "Manages the location of the config"
14 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
16 def run(["migrate_to_db"]) do
21 def run(["migrate_from_db" | options]) do
22 # TODO: add support for releases
26 OptionParser.parse!(options,
27 strict: [env: :string, delete_from_db: :boolean],
28 aliases: [d: :delete_from_db]
31 with {:active?, true} <-
32 {:active?, Pleroma.Config.get([:configurable_from_database])},
33 env when is_binary(env) <- opts[:env] || "prod",
34 config_path <- config_path(env),
35 {:ok, file} <- File.open(config_path, [:write, :utf8]) do
36 IO.write(file, config_header())
40 |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db]))
43 System.cmd("mix", ["format", config_path])
47 "Migration is not allowed by config. You can change this behavior in instance settings."
51 shell_info("Error occuried while opening file. #{inspect(error)}")
55 defp config_path(env) do
57 if Pleroma.Config.get(:release) do
59 |> Pleroma.Config.get()
65 Path.join(path, "#{env}.exported_from_db.secret.exs")
68 @spec migrate_to_db(Path.t() | nil) :: any()
69 def migrate_to_db(file_path \\ nil) do
70 if Pleroma.Config.get([:configurable_from_database]) do
72 if Pleroma.Config.get(:release),
73 do: Pleroma.Config.get(:config_path),
74 else: "config/#{Pleroma.Config.get(:env)}.secret.exs"
76 config_file = file_path || user_config_file
77 do_migrate_to_db(config_file)
80 "Migration is not allowed by config. You can change this behavior in instance settings."
85 if Code.ensure_loaded?(Config.Reader) do
86 defp config_header, do: "import Config\r\n\r\n"
87 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
89 defp config_header, do: "use Mix.Config\r\n\r\n"
90 defp read_file(config_file), do: Mix.Config.eval!(config_file)
93 defp do_migrate_to_db(config_file) do
94 if File.exists?(config_file) do
95 {custom_config, _paths} = read_file(config_file)
99 |> Enum.each(&create(&1, custom_config[&1]))
101 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
105 defp create(group, settings) do
106 Enum.reject(settings, fn {k, _v} ->
107 k in [Pleroma.Repo, Pleroma.Web.Endpoint, :env, :configurable_from_database] or
108 (group == :phoenix and k == :serve_endpoints)
110 |> Enum.each(fn {key, value} ->
112 {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
114 shell_info("Settings for key #{key} migrated.")
117 shell_info("Settings for group :#{group} migrated.")
120 defp write_to_file_with_deletion(config, file, with_deletion) do
123 "config #{config.group}, #{config.key}, #{
124 inspect(ConfigDB.from_binary(config.value), limit: :infinity)
129 {:ok, _} = Repo.delete(config)
130 shell_info("#{config.key} deleted from DB.")