Add --force flag for delete and reset commands

Bunch of reorganization and consolidation
This commit is contained in:
Mark Felder 2020-12-02 16:24:32 -06:00
parent 20a911f9f7
commit e379ab8277
3 changed files with 170 additions and 47 deletions

View File

@ -111,13 +111,13 @@ e.g., this deletes all the settings under `config :tesla`
=== "OTP" === "OTP"
```sh ```sh
./bin/pleroma_ctl config delete tesla ./bin/pleroma_ctl config delete [--force] tesla
``` ```
=== "From Source" === "From Source"
```sh ```sh
mix pleroma.config delete tesla mix pleroma.config delete [--force] tesla
``` ```
To delete values under a specific key: To delete values under a specific key:
@ -127,13 +127,13 @@ e.g., this deletes all the settings under `config :phoenix, :stacktrace_depth`
=== "OTP" === "OTP"
```sh ```sh
./bin/pleroma_ctl config delete phoenix stacktrace_depth ./bin/pleroma_ctl config delete [--force] phoenix stacktrace_depth
``` ```
=== "From Source" === "From Source"
```sh ```sh
mix pleroma.config delete phoenix stacktrace_depth mix pleroma.config delete [--force] phoenix stacktrace_depth
``` ```
## Remove all settings from the database ## Remove all settings from the database
@ -143,11 +143,11 @@ This forcibly removes all saved values in the database.
=== "OTP" === "OTP"
```sh ```sh
./bin/pleroma_ctl config reset ./bin/pleroma_ctl config [--force] reset
``` ```
=== "From Source" === "From Source"
```sh ```sh
mix pleroma.config reset mix pleroma.config [--force] reset
``` ```

View File

@ -95,7 +95,7 @@ def run(["groups"]) do
end) end)
end end
def run(["reset"]) do def run(["reset" | options]) do
check_configdb(fn -> check_configdb(fn ->
start_pleroma() start_pleroma()
@ -108,7 +108,11 @@ def run(["reset"]) do
shell_error("\nTHIS CANNOT BE UNDONE!") shell_error("\nTHIS CANNOT BE UNDONE!")
if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do proceed? =
"--force" in options or
shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y)
if proceed? do
Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
@ -119,53 +123,46 @@ def run(["reset"]) do
end) end)
end end
def run(["delete", group]) do def run(["delete", "--force", group, key]) do
check_configdb(fn -> start_pleroma()
start_pleroma()
group = maybe_atomize(group) group = maybe_atomize(group)
key = maybe_atomize(key)
if group_exists?(group) do delete_key(group, key)
shell_info("The following settings will be removed from ConfigDB:\n") end
dump_group(group) def run(["delete", "--force", group]) do
start_pleroma()
if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do group = maybe_atomize(group)
ConfigDB
|> Repo.all() delete_group(group)
|> Enum.filter(fn x ->
if x.group == group do
x |> delete(true)
end
end)
else
shell_error("No changes made.")
end
else
shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
end
end)
end end
def run(["delete", group, key]) do def run(["delete", group, key]) do
check_configdb(fn -> start_pleroma()
start_pleroma()
group = maybe_atomize(group) group = maybe_atomize(group)
key = maybe_atomize(key) key = maybe_atomize(key)
if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
ConfigDB delete_key(group, key)
|> Repo.all() else
|> Enum.filter(fn x -> shell_error("No changes made.")
if x.group == group and x.key == key do end
x |> delete(true) end
end
end) def run(["delete", group]) do
else start_pleroma()
shell_error("No changes made.")
end group = maybe_atomize(group)
end)
if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
delete_group(group)
else
shell_error("No changes made.")
end
end end
@spec migrate_to_db(Path.t() | nil) :: any() @spec migrate_to_db(Path.t() | nil) :: any()
@ -275,7 +272,7 @@ defp delete(config, true) do
{:ok, _} = Repo.delete(config) {:ok, _} = Repo.delete(config)
shell_info( shell_info(
"config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB." "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
) )
end end
@ -348,4 +345,35 @@ defp check_configdb(callback) do
) )
end end
end end
defp delete_key(group, key) do
check_configdb(fn ->
ConfigDB
|> Repo.all()
|> Enum.filter(fn x ->
if x.group == group and x.key == key do
x |> delete(true)
end
end)
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
|> Repo.all()
|> Enum.filter(fn x ->
if x.group == group do
x |> delete(true)
end
end)
else
_ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
end
end)
end
end end

View File

@ -297,4 +297,99 @@ test "refuses to dump" do
]} ]}
end end
end end
describe "destructive operations" do
setup do: clear_config(:configurable_from_database, true)
test "deletes group of settings" do
insert(:config,
group: :pleroma,
key: :instance,
value: [
name: "Pleroma Test"
]
)
_config_before = Repo.all(ConfigDB)
assert config_before = [
%Pleroma.ConfigDB{
group: :pleroma,
key: :instance,
value: [name: "Pleroma Test"]
}
]
Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma"])
config_after = Repo.all(ConfigDB)
refute config_after == config_before
end
test "deletes specified key" do
insert(:config,
group: :pleroma,
key: :instance,
value: [
name: "Pleroma Test"
]
)
insert(:config,
group: :pleroma,
key: Pleroma.Captcha,
value: [
enabled: false
]
)
_config_before = Repo.all(ConfigDB)
assert config_before = [
%Pleroma.ConfigDB{
group: :pleroma,
key: :instance,
value: [name: "Pleroma Test"]
},
%Pleroma.ConfigDB{
group: :pleroma,
key: Pleroma.Captcha,
value: [enabled: false]
}
]
Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma", "Pleroma.Captcha"])
config_after = Repo.all(ConfigDB)
refute config_after == config_before
end
test "resets entire config" do
insert(:config,
group: :pleroma,
key: :instance,
value: [
name: "Pleroma Test"
]
)
_config_before = Repo.all(ConfigDB)
assert config_before = [
%Pleroma.ConfigDB{
group: :pleroma,
key: :instance,
value: [name: "Pleroma Test"]
}
]
Mix.Tasks.Pleroma.Config.run(["reset", "--force"])
config_after = Repo.all(ConfigDB)
assert config_after == []
end
end
end end