truncate config table on migrate to db task
[akkoma] / lib / mix / tasks / pleroma / config.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
56
57 custom_config =
58 config_file
59 |> read_file()
60 |> elem(0)
61
62 custom_config
63 |> Keyword.keys()
64 |> Enum.each(&create(&1, custom_config))
65 else
66 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
67 end
68 end
69
70 defp create(group, settings) do
71 group
72 |> Pleroma.Config.Loader.filter_group(settings)
73 |> Enum.each(fn {key, value} ->
74 key = inspect(key)
75 {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
76
77 shell_info("Settings for key #{key} migrated.")
78 end)
79
80 shell_info("Settings for group :#{group} migrated.")
81 end
82
83 defp migrate_from_db(opts) do
84 if Pleroma.Config.get([:configurable_from_database]) do
85 env = opts[:env] || "prod"
86
87 config_path =
88 if Pleroma.Config.get(:release) do
89 :config_path
90 |> Pleroma.Config.get()
91 |> Path.dirname()
92 else
93 "config"
94 end
95 |> Path.join("#{env}.exported_from_db.secret.exs")
96
97 file = File.open!(config_path, [:write, :utf8])
98
99 IO.write(file, config_header())
100
101 ConfigDB
102 |> Repo.all()
103 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
104
105 :ok = File.close(file)
106 System.cmd("mix", ["format", config_path])
107 else
108 migration_error()
109 end
110 end
111
112 defp migration_error do
113 shell_error(
114 "Migration is not allowed in config. You can change this behavior by setting `configurable_from_database` to true."
115 )
116 end
117
118 if Code.ensure_loaded?(Config.Reader) do
119 defp config_header, do: "import Config\r\n\r\n"
120 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
121 else
122 defp config_header, do: "use Mix.Config\r\n\r\n"
123 defp read_file(config_file), do: Mix.Config.eval!(config_file)
124 end
125
126 defp write_and_delete(config, file, delete?) do
127 config
128 |> write(file)
129 |> delete(delete?)
130 end
131
132 defp write(config, file) do
133 value =
134 config.value
135 |> ConfigDB.from_binary()
136 |> inspect(limit: :infinity)
137
138 IO.write(file, "config #{config.group}, #{config.key}, #{value}\r\n\r\n")
139
140 config
141 end
142
143 defp delete(config, true) do
144 {:ok, _} = Repo.delete(config)
145 shell_info("#{config.key} deleted from DB.")
146 end
147
148 defp delete(_config, _), do: :ok
149 end