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"
11 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
30 def run(["migrate_to_db"]) do
33 if Pleroma.Config.get([:configurable_from_database]) do
34 Enum.each(@groups, &load_and_create(&1))
37 "Migration is not allowed by config. You can change this behavior in instance settings."
42 def run(["migrate_from_db" | options]) do
46 OptionParser.parse!(options,
47 strict: [env: :string, delete_from_db: :boolean],
48 aliases: [d: :delete_from_db]
51 with {:active?, true} <-
52 {:active?, Pleroma.Config.get([:configurable_from_database])},
53 env_path when is_binary(env_path) <- opts[:env],
54 config_path <- "config/#{env_path}.exported_from_db.secret.exs",
55 {:ok, file} <- File.open(config_path, [:write, :utf8]) do
56 IO.write(file, "use Mix.Config\r\n")
60 |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db]))
63 System.cmd("mix", ["format", config_path])
67 "migration is not allowed by config. You can change this behavior in instance settings."
71 Mix.shell().info("error occuried while opening file. #{inspect(error)}")
75 defp load_and_create(group) do
77 |> Application.get_all_env()
78 |> Enum.reject(fn {k, _v} ->
79 k in [Pleroma.Repo, :env] or (group == :phoenix and k == :serve_endpoints)
81 |> Enum.each(fn {key, value} ->
83 {:ok, _} = Config.update_or_create(%{group: inspect(group), key: key, value: value})
85 Mix.shell().info("settings for key #{key} migrated.")
88 Mix.shell().info("settings for group :#{group} migrated.")
91 defp write_to_file_with_deletion(config, file, with_deletion) do
94 "config #{config.group}, #{config.key}, #{
95 inspect(Config.from_binary(config.value), limit: :infinity)
100 {:ok, _} = Repo.delete(config)
101 Mix.shell().info("#{config.key} deleted from DB.")