From 6dcc36baa9b19d18785d6f7ab8ceb7dd941c6180 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 25 Nov 2020 12:44:06 -0600 Subject: [PATCH] Add mix tasks to give additional recovery and debugging options - pleroma.config dump: prints the entire config as it would be exported to the filesystem - pleroma.config dump KEY: prints the configuration under a specific ConfigDB key in the database - pleroma.config keylist: lists the available keys in ConfigDB - pleroma.config keydel KEY: deletes ConfigDB entry stored under the key This should prevent the need for users to manually execute SQL queries. --- lib/mix/tasks/pleroma/config.ex | 89 +++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 18f99318d..b49854528 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -30,6 +30,83 @@ defmodule Mix.Tasks.Pleroma.Config do migrate_from_db(opts) end + def run(["dump"]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + header = config_header() + + shell_info("#{header}") + + ConfigDB + |> Repo.all() + |> Enum.each(&dump(&1)) + else + _ -> configdb_not_enabled() + end + end + + def run(["dump" | dbkey]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + dbkey = dbkey |> List.first() |> String.to_atom() + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.key == dbkey do + x |> dump + end + end) + else + _ -> configdb_not_enabled() + end + end + + def run(["keylist"]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + keys = + ConfigDB + |> Repo.all() + |> Enum.map(fn x -> x.key end) + + if length(keys) > 0 do + shell_info("The following configuration keys are set in ConfigDB:\r\n") + keys |> Enum.each(fn x -> shell_info("- #{x}") end) + shell_info("\r\n") + end + else + _ -> configdb_not_enabled() + end + end + + def run(["keydel" | dbkey]) do + unless [] == dbkey do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + dbkey = dbkey |> List.first() |> String.to_atom() + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.key == dbkey do + x |> delete(true) + end + end) + else + _ -> configdb_not_enabled() + end + else + shell_error( + "You must provide a key to delete. Use the keylist command to get a list of valid keys." + ) + end + end + @spec migrate_to_db(Path.t() | nil) :: any() def migrate_to_db(file_path \\ nil) do with true <- Pleroma.Config.get([:configurable_from_database]), @@ -154,4 +231,16 @@ defmodule Mix.Tasks.Pleroma.Config do end defp delete(_config, _), do: :ok + + defp dump(%Pleroma.ConfigDB{} = config) do + value = inspect(config.value, limit: :infinity) + + shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n") + end + + defp configdb_not_enabled do + shell_error( + "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." + ) + end end