{
configs: [
{
+ "group": string,
"key": string,
- "value": string or {} or []
+ "value": string or {} or [] or {"tuple": []}
}
]
}
- Method `POST`
- Params:
- `configs` => [
+ - `group` (string)
- `key` (string)
- - `value` (string, [], {})
+ - `value` (string, [], {} or {"tuple": []})
- `delete` = true (optional, if parameter must be deleted)
]
{
configs: [
{
+ "group": "pleroma",
"key": "Pleroma.Upload",
"value": {
"uploader": "Pleroma.Uploaders.Local",
{
configs: [
{
+ "group": string,
"key": string,
"value": string or {} or [] or {"tuple": []}
}
|> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
|> Enum.each(fn {k, v} ->
key = to_string(k) |> String.replace("Elixir.", "")
- {:ok, _} = Config.update_or_create(%{key: key, value: v})
+ {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
Mix.shell().info("#{key} is migrated.")
end)
IO.write(
file,
- "config :pleroma, #{config.key}#{mark} #{inspect(Config.from_binary(config.value))}\r\n"
+ "config :#{config.group}, #{config.key}#{mark} #{
+ inspect(Config.from_binary(config.value))
+ }\r\n"
)
{:ok, _} = Repo.delete(config)
def load_and_update_env do
if Pleroma.Config.get([:instance, :dynamic_configuration]) and
Ecto.Adapters.SQL.table_exists?(Pleroma.Repo, "config") do
- Pleroma.Repo.all(Config)
- |> Enum.each(&update_env(&1))
+ for_restart =
+ Pleroma.Repo.all(Config)
+ |> Enum.map(&update_env(&1))
+
+ # We need to restart applications for loaded settings take effect
+ for_restart
+ |> Enum.reject(&(&1 in [:pleroma, :ok]))
+ |> Enum.each(fn app ->
+ Application.stop(app)
+ :ok = Application.start(app)
+ end)
end
end
setting.key
end
+ group = String.to_existing_atom(setting.group)
+
Application.put_env(
- :pleroma,
+ group,
String.to_existing_atom(key),
Config.from_binary(setting.value)
)
+
+ group
rescue
e ->
require Logger
if Pleroma.Config.get([:instance, :dynamic_configuration]) do
updated =
Enum.map(configs, fn
- %{"key" => key, "value" => value} ->
- {:ok, config} = Config.update_or_create(%{key: key, value: value})
+ %{"group" => group, "key" => key, "value" => value} ->
+ {:ok, config} = Config.update_or_create(%{group: group, key: key, value: value})
config
- %{"key" => key, "delete" => "true"} ->
- {:ok, _} = Config.delete(key)
+ %{"group" => group, "key" => key, "delete" => "true"} ->
+ {:ok, _} = Config.delete(%{group: group, key: key})
nil
end)
|> Enum.reject(&is_nil(&1))
schema "config" do
field(:key, :string)
+ field(:group, :string)
field(:value, :binary)
timestamps()
end
- @spec get_by_key(String.t()) :: Config.t() | nil
- def get_by_key(key), do: Repo.get_by(Config, key: key)
+ @spec get_by_params(map()) :: Config.t() | nil
+ def get_by_params(params), do: Repo.get_by(Config, params)
@spec changeset(Config.t(), map()) :: Changeset.t()
def changeset(config, params \\ %{}) do
config
- |> cast(params, [:key, :value])
- |> validate_required([:key, :value])
- |> unique_constraint(:key)
+ |> cast(params, [:key, :group, :value])
+ |> validate_required([:key, :group, :value])
+ |> unique_constraint(:key, name: :config_group_key_index)
end
@spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
- def create(%{key: key, value: value}) do
+ def create(params) do
%Config{}
- |> changeset(%{key: key, value: transform(value)})
+ |> changeset(Map.put(params, :value, transform(params[:value])))
|> Repo.insert()
end
end
@spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
- def update_or_create(%{key: key} = params) do
- with %Config{} = config <- Config.get_by_key(key) do
+ def update_or_create(params) do
+ with %Config{} = config <- Config.get_by_params(Map.take(params, [:group, :key])) do
Config.update(config, params)
else
nil -> Config.create(params)
end
end
- @spec delete(String.t()) :: {:ok, Config.t()} | {:error, Changeset.t()}
- def delete(key) do
- with %Config{} = config <- Config.get_by_key(key) do
+ @spec delete(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
+ def delete(params) do
+ with %Config{} = config <- Config.get_by_params(params) do
Repo.delete(config)
else
- nil -> {:error, "Config with key #{key} not found"}
+ nil -> {:error, "Config with params #{inspect(params)} not found"}
end
end
end
@spec transform(any()) :: binary()
+ def transform(%{"tuple" => _} = entity), do: :erlang.term_to_binary(do_transform(entity))
+
def transform(entity) when is_map(entity) do
tuples =
for {k, v} <- entity,
def render("show.json", %{config: config}) do
%{
key: config.key,
+ group: config.group,
value: Pleroma.Web.AdminAPI.Config.from_binary_to_map(config.value)
}
end
--- /dev/null
+defmodule Pleroma.Repo.Migrations.AddGroupKeyToConfig do
+ use Ecto.Migration
+
+ def change do
+ alter table("config") do
+ add(:group, :string)
+ end
+
+ drop(unique_index("config", :key))
+ create(unique_index("config", [:group, :key]))
+ end
+end
test "transfer config values from db to env" do
refute Application.get_env(:pleroma, :test_key)
- Pleroma.Web.AdminAPI.Config.create(%{key: "test_key", value: [live: 2, com: 3]})
+ refute Application.get_env(:idna, :test_key)
+
+ Pleroma.Web.AdminAPI.Config.create(%{
+ group: "pleroma",
+ key: "test_key",
+ value: [live: 2, com: 3]
+ })
+
+ Pleroma.Web.AdminAPI.Config.create(%{
+ group: "idna",
+ key: "test_key",
+ value: [live: 15, com: 35]
+ })
Pleroma.Config.TransferTask.start_link()
assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
+ assert Application.get_env(:idna, :test_key) == [live: 15, com: 35]
on_exit(fn ->
Application.delete_env(:pleroma, :test_key)
+ Application.delete_env(:idna, :test_key)
end)
end
test "non existing atom" do
- Pleroma.Web.AdminAPI.Config.create(%{key: "undefined_atom_key", value: [live: 2, com: 3]})
+ Pleroma.Web.AdminAPI.Config.create(%{
+ group: "pleroma",
+ key: "undefined_atom_key",
+ value: [live: 2, com: 3]
+ })
assert ExUnit.CaptureLog.capture_log(fn ->
Pleroma.Config.TransferTask.start_link()
def config_factory do
%Pleroma.Web.AdminAPI.Config{
key: sequence(:key, &"some_key_#{&1}"),
+ group: "pleroma",
value:
sequence(
:value,
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
- first_db = Config.get_by_key("first_setting")
- second_db = Config.get_by_key("second_setting")
- refute Config.get_by_key("Pleroma.Repo")
+ first_db = Config.get_by_params(%{group: "pleroma", key: "first_setting"})
+ second_db = Config.get_by_params(%{group: "pleroma", key: "second_setting"})
+ refute Config.get_by_params(%{group: "pleroma", key: "Pleroma.Repo"})
assert Config.from_binary(first_db.value) == [key: "value", key2: [Pleroma.Repo]]
assert Config.from_binary(second_db.value) == [key: "value2", key2: [Pleroma.Activity]]
end
test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
- Config.create(%{key: "setting_first", value: [key: "value", key2: [Pleroma.Activity]]})
- Config.create(%{key: "setting_second", value: [key: "valu2", key2: [Pleroma.Repo]]})
+ Config.create(%{
+ group: "pleroma",
+ key: "setting_first",
+ value: [key: "value", key2: [Pleroma.Activity]]
+ })
+
+ Config.create(%{
+ group: "pleroma",
+ key: "setting_second",
+ value: [key: "valu2", key2: [Pleroma.Repo]]
+ })
Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "temp"])
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
- %{key: "key1", value: "value1"},
+ %{group: "pleroma", key: "key1", value: "value1"},
%{
+ group: "pleroma",
key: "key2",
value: %{
"nested_1" => "nested_value1",
}
},
%{
+ group: "pleroma",
key: "key3",
value: [
%{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
]
},
%{
+ group: "pleroma",
key: "key4",
value: %{"nested_5" => ":upload", "endpoint" => "https://example.com"}
+ },
+ %{
+ group: "idna",
+ key: "key5",
+ value: %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
}
]
})
assert json_response(conn, 200) == %{
"configs" => [
%{
+ "group" => "pleroma",
"key" => "key1",
"value" => "value1"
},
%{
+ "group" => "pleroma",
"key" => "key2",
"value" => [
%{"nested_1" => "nested_value1"},
]
},
%{
+ "group" => "pleroma",
"key" => "key3",
"value" => [
[%{"nested_3" => "nested_3"}, %{"nested_33" => "nested_33"}],
]
},
%{
+ "group" => "pleroma",
"key" => "key4",
"value" => [%{"endpoint" => "https://example.com"}, %{"nested_5" => "upload"}]
+ },
+ %{
+ "group" => "idna",
+ "key" => "key5",
+ "value" => %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
}
]
}
endpoint: "https://example.com",
nested_5: :upload
]
+
+ assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []}
end
test "update config setting & delete", %{conn: conn} do
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
- %{key: config1.key, value: "another_value"},
- %{key: config2.key, delete: "true"}
+ %{group: config1.group, key: config1.key, value: "another_value"},
+ %{group: config2.group, key: config2.key, delete: "true"}
]
})
assert json_response(conn, 200) == %{
"configs" => [
%{
+ "group" => "pleroma",
"key" => config1.key,
"value" => "another_value"
}
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
+ "group" => "pleroma",
"key" => "Pleroma.Captcha.NotReal",
"value" => %{
"enabled" => ":false",
assert json_response(conn, 200) == %{
"configs" => [
%{
+ "group" => "pleroma",
"key" => "Pleroma.Captcha.NotReal",
"value" => [
%{"enabled" => false},
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
+ "group" => "pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
assert json_response(conn, 200) == %{
"configs" => [
%{
+ "group" => "pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
config = insert(:config)
insert(:config)
- assert config == Config.get_by_key(config.key)
+ assert config == Config.get_by_params(%{group: config.group, key: config.key})
end
test "create/1" do
- {:ok, config} = Config.create(%{key: "some_key", value: "some_value"})
- assert config == Config.get_by_key("some_key")
+ {:ok, config} = Config.create(%{group: "pleroma", key: "some_key", value: "some_value"})
+ assert config == Config.get_by_params(%{group: "pleroma", key: "some_key"})
end
test "update/1" do
config = insert(:config)
{:ok, updated} = Config.update(config, %{value: "some_value"})
- loaded = Config.get_by_key(config.key)
+ loaded = Config.get_by_params(%{group: config.group, key: config.key})
assert loaded == updated
end
key2 = "another_key"
params = [
- %{key: key2, value: "another_value"},
- %{key: config.key, value: "new_value"}
+ %{group: "pleroma", key: key2, value: "another_value"},
+ %{group: config.group, key: config.key, value: "new_value"}
]
assert Repo.all(Config) |> length() == 1
assert Repo.all(Config) |> length() == 2
- config1 = Config.get_by_key(config.key)
- config2 = Config.get_by_key(key2)
+ config1 = Config.get_by_params(%{group: config.group, key: config.key})
+ config2 = Config.get_by_params(%{group: "pleroma", key: key2})
assert config1.value == Config.transform("new_value")
assert config2.value == Config.transform("another_value")
test "delete/1" do
config = insert(:config)
- {:ok, _} = Config.delete(config.key)
- refute Config.get_by_key(config.key)
+ {:ok, _} = Config.delete(%{key: config.key, group: config.group})
+ refute Config.get_by_params(%{key: config.key, group: config.group})
end
describe "transform/1" do