From a02eb8839650ecbf8bcad9bd6d346fc280985cae Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 3 Dec 2020 19:34:23 +0300 Subject: [PATCH] config_db search methods --- lib/mix/tasks/pleroma/config.ex | 30 +++++++++++++----------------- lib/pleroma/config_db.ex | 12 +++++++++++- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 137aef038..63d8c46b5 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -62,9 +62,8 @@ def run(["dump", group, key]) do group = maybe_atomize(group) key = maybe_atomize(key) - %{group: group, key: key} - |> ConfigDB.get_by_params() - |> Repo.all() + group + |> ConfigDB.get_all_by_group_and_key(key) |> Enum.each(&dump/1) end) end @@ -290,17 +289,15 @@ defp dump(%Pleroma.ConfigDB{} = config) do end defp dump_group(group) when is_atom(group) do - %{group: group} - |> ConfigDB.get_by_params() - |> Repo.all() + group + |> ConfigDB.get_all_by_group() |> Enum.each(&dump/1) end defp group_exists?(group) do - %{group: group} - |> ConfigDB.get_by_params() - |> Repo.all() - |> Enum.empty?() + group + |> ConfigDB.get_all_by_group() + |> Enum.empty?() end defp maybe_atomize(arg) when is_atom(arg), do: arg @@ -310,7 +307,7 @@ defp maybe_atomize(arg) when is_binary(arg) do String.to_existing_atom("Elixir." <> arg) else String.to_atom(arg) - end + end end defp check_configdb(callback) do @@ -326,8 +323,8 @@ defp check_configdb(callback) do defp delete_key(group, key) do check_configdb(fn -> - ConfigDB.get_by_params(%{group: group, key: key}) - |> Repo.all() + group + |> ConfigDB.get_all_by_group_and_key(key) |> Enum.each(&delete(&1, true)) end) end @@ -338,10 +335,9 @@ defp delete_group(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)) - + group + |> ConfigDB.get_all_by_group() + |> Enum.each(&delete(&1, true)) else _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") end diff --git a/lib/pleroma/config_db.ex b/lib/pleroma/config_db.ex index e5b7811aa..2c3c0cb5c 100644 --- a/lib/pleroma/config_db.ex +++ b/lib/pleroma/config_db.ex @@ -6,7 +6,7 @@ defmodule Pleroma.ConfigDB do use Ecto.Schema import Ecto.Changeset - import Ecto.Query, only: [select: 3] + import Ecto.Query, only: [select: 3, from: 2] import Pleroma.Web.Gettext alias __MODULE__ @@ -41,6 +41,16 @@ def get_all_as_keyword do end) end + @spec get_all_by_group(atom() | String.t()) :: [t()] + def get_all_by_group(group) do + from(c in ConfigDB, where: c.group == ^group) |> Repo.all() + end + + @spec get_all_by_group_and_key(atom() | String.t(), atom() | String.t()) :: [t()] + def get_all_by_group_and_key(group, key) do + from(c in ConfigDB, where: c.group == ^group and c.key == ^key) |> Repo.all() + end + @spec get_by_params(map()) :: ConfigDB.t() | nil def get_by_params(params), do: Repo.get_by(ConfigDB, params)