+ alias Pleroma.Config
+
+ require Logger
+
+ defmacro clear_config(config_path) do
+ quote do
+ clear_config(unquote(config_path)) do
+ end
+ end
+ end
+
+ defmacro clear_config(config_path, do: yield) do
+ quote do
+ initial_setting = Config.fetch(unquote(config_path))
+
+ unquote(yield)
+
+ on_exit(fn ->
+ case initial_setting do
+ :error ->
+ Config.delete(unquote(config_path))
+
+ {:ok, value} ->
+ Config.put(unquote(config_path), value)
+ end
+ end)
+
+ :ok
+ end
+ end
+
+ defmacro clear_config(config_path, temp_setting) do
+ # NOTE: `clear_config([section, key], value)` != `clear_config([section], key: value)` (!)
+ # Displaying a warning to prevent unintentional clearing of all but one keys in section
+ if Keyword.keyword?(temp_setting) and length(temp_setting) == 1 do
+ Logger.warn(
+ "Please change `clear_config([section], key: value)` to `clear_config([section, key], value) (#{inspect(config_path)} = #{inspect(temp_setting)})`"
+ )
+ end
+
+ quote do
+ clear_config(unquote(config_path)) do
+ Config.put(unquote(config_path), unquote(temp_setting))
+ end
+ end
+ end
+
+ def require_migration(migration_name) do
+ [{module, _}] = Code.require_file("#{migration_name}.exs", "priv/repo/migrations")
+ {:ok, %{migration: module}}
+ end