+
+ 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 dump_key(group, key) when is_atom(group) and is_atom(key) do
+ ConfigDB
+ |> Repo.all()
+ |> Enum.filter(fn x ->
+ if x.group == group && x.key == key do
+ x |> dump
+ end
+ end)
+ end
+
+ defp dump_group(group) when is_atom(group) do
+ %{group: group}
+ |> ConfigDB.get_by_params()
+ |> Repo.all()
+ |> Enum.each(&dump/1)
+ end
+
+ defp group_exists?(group) do
+ %{group: group}
+ |> ConfigDB.get_by_params()
+ |> Repo.all()
+ |> Enum.empty?()
+ end
+
+ defp maybe_atomize(arg) when is_atom(arg), do: arg
+
+ defp maybe_atomize(arg) when is_binary(arg) do
+ if Pleroma.ConfigDB.module_name?(arg) do
+ String.to_existing_atom("Elixir." <> arg)
+ else
+ String.to_atom(arg)
+ end
+ end
+
+ defp check_configdb(callback) do
+ with true <- Pleroma.Config.get([:configurable_from_database]) do
+ callback.()
+ else
+ _ ->
+ shell_error(
+ "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
+ )
+ end
+ end
+
+ defp delete_key(group, key) do
+ check_configdb(fn ->
+ ConfigDB.get_by_params(%{group: group, key: key})
+ |> Repo.all()
+ |> Enum.each(&delete(&1, true))
+ end)
+ end
+
+ defp delete_group(group) do
+ check_configdb(fn ->
+ with true <- group_exists?(group) do
+ shell_info("The following settings will be removed from ConfigDB:\n")
+ dump_group(group)
+
+ ConfigDB.get_by_params(%{group: group})
+ |> Repo.all()
+ |> Enum.each(&delete(&1, true))
+
+ else
+ _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
+ end
+ end)
+ end
+
+ defp truncatedb do
+ Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
+ Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
+ end