Lint
[akkoma] / lib / mix / tasks / pleroma / config.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Config do
6 use Mix.Task
7
8 import Mix.Pleroma
9
10 alias Pleroma.ConfigDB
11 alias Pleroma.Repo
12
13 @shortdoc "Manages the location of the config"
14 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
15
16 def run(["migrate_to_db"]) do
17 start_pleroma()
18 migrate_to_db()
19 end
20
21 def run(["migrate_from_db" | options]) do
22 start_pleroma()
23
24 {opts, _} =
25 OptionParser.parse!(options,
26 strict: [env: :string, delete: :boolean],
27 aliases: [d: :delete]
28 )
29
30 migrate_from_db(opts)
31 end
32
33 @spec migrate_to_db(Path.t() | nil) :: any()
34 def migrate_to_db(file_path \\ nil) do
35 if Pleroma.Config.get([:configurable_from_database]) do
36 config_file =
37 if file_path do
38 file_path
39 else
40 if Pleroma.Config.get(:release) do
41 Pleroma.Config.get(:config_path)
42 else
43 "config/#{Pleroma.Config.get(:env)}.secret.exs"
44 end
45 end
46
47 do_migrate_to_db(config_file)
48 else
49 migration_error()
50 end
51 end
52
53 defp do_migrate_to_db(config_file) do
54 if File.exists?(config_file) do
55 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
56 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
57 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
58
59 custom_config =
60 config_file
61 |> read_file()
62 |> elem(0)
63
64 custom_config
65 |> Keyword.keys()
66 |> Enum.each(&create(&1, custom_config))
67 else
68 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
69 end
70 end
71
72 defp create(group, settings) do
73 group
74 |> Pleroma.Config.Loader.filter_group(settings)
75 |> Enum.each(fn {key, value} ->
76 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
77
78 shell_info("Settings for key #{key} migrated.")
79 end)
80
81 shell_info("Settings for group :#{group} migrated.")
82 end
83
84 defp migrate_from_db(opts) do
85 if Pleroma.Config.get([:configurable_from_database]) do
86 env = opts[:env] || "prod"
87
88 config_path =
89 if Pleroma.Config.get(:release) do
90 :config_path
91 |> Pleroma.Config.get()
92 |> Path.dirname()
93 else
94 "config"
95 end
96 |> Path.join("#{env}.exported_from_db.secret.exs")
97
98 file = File.open!(config_path, [:write, :utf8])
99
100 IO.write(file, config_header())
101
102 ConfigDB
103 |> Repo.all()
104 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
105
106 :ok = File.close(file)
107 System.cmd("mix", ["format", config_path])
108 else
109 migration_error()
110 end
111 end
112
113 defp migration_error do
114 shell_error(
115 "Migration is not allowed in config. You can change this behavior by setting `configurable_from_database` to true."
116 )
117 end
118
119 if Code.ensure_loaded?(Config.Reader) do
120 defp config_header, do: "import Config\r\n\r\n"
121 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
122 else
123 defp config_header, do: "use Mix.Config\r\n\r\n"
124 defp read_file(config_file), do: Mix.Config.eval!(config_file)
125 end
126
127 defp write_and_delete(config, file, delete?) do
128 config
129 |> write(file)
130 |> delete(delete?)
131 end
132
133 defp write(config, file) do
134 value = inspect(config.value, limit: :infinity)
135
136 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
137
138 config
139 end
140
141 defp delete(config, true) do
142 {:ok, _} = Repo.delete(config)
143 shell_info("#{config.key} deleted from DB.")
144 end
145
146 defp delete(_config, _), do: :ok
147 end