Allow importing extra config (#3906)

* allow importing extra config

* changelog

* fix typo

* add test
This commit is contained in:
ruslandoga 2024-03-19 19:02:52 +08:00 committed by GitHub
parent dfcc8d794a
commit 4242b52be4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 2 deletions

View File

@ -31,6 +31,7 @@ All notable changes to this project will be documented in this file.
- Add 'browser_versions.csv' to CSV export
- Add `CLICKHOUSE_MAX_BUFFER_SIZE_BYTES` env var which defaults to `100000` (100KB)
- Add alternative SMTP adapter plausible/analytics#3654
- Add `EXTRA_CONFIG_PATH` env var to specify extra Elixir config plausible/analytics#3906
### Removed
- Removed the nested custom event property breakdown UI when filtering by a goal in Goal Conversions

View File

@ -19,8 +19,10 @@ defmodule Plausible.MixProject do
releases: [
plausible: [
include_executables_for: [:unix],
applications: [plausible: :permanent],
steps: [:assemble, :tar]
config_providers: [
{Config.Reader,
path: {:system, "RELEASE_ROOT", "/import_extra_config.exs"}, imports: []}
]
]
],
dialyzer: [

View File

@ -0,0 +1,8 @@
import Config
import Plausible.ConfigHelpers
config_dir = System.get_env("CONFIG_DIR", "/run/secrets")
if extra_config_path = get_var_from_path_or_env(config_dir, "EXTRA_CONFIG_PATH") do
import_config extra_config_path
end

View File

@ -272,6 +272,44 @@ defmodule Plausible.ConfigTest do
end
end
describe "extra config" do
test "no-op when no extra path is set" do
put_system_env_undo({"EXTRA_CONFIG_PATH", nil})
assert Config.Reader.read!("rel/overlays/import_extra_config.exs") == []
end
test "raises if path is invalid" do
put_system_env_undo({"EXTRA_CONFIG_PATH", "no-such-file"})
assert_raise File.Error, ~r/could not read file/, fn ->
Config.Reader.read!("rel/overlays/import_extra_config.exs")
end
end
@tag :tmp_dir
test "reads extra config", %{tmp_dir: tmp_dir} do
extra_config_path = Path.join(tmp_dir, "config.exs")
File.write!(extra_config_path, """
import Config
config :plausible, Plausible.Repo,
after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}
""")
put_system_env_undo({"EXTRA_CONFIG_PATH", extra_config_path})
assert Config.Reader.read!("rel/overlays/import_extra_config.exs") == [
{:plausible,
[
{Plausible.Repo,
[after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}]}
]}
]
end
end
defp runtime_config(env) do
put_system_env_undo(env)
Config.Reader.read!("config/runtime.exs", env: :prod)