Skip to content

Commit

Permalink
Allow importing extra config (#3906)
Browse files Browse the repository at this point in the history
* allow importing extra config

* changelog

* fix typo

* add test
  • Loading branch information
ruslandoga committed Mar 19, 2024
1 parent dfcc8d7 commit 4242b52
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
8 changes: 8 additions & 0 deletions rel/overlays/import_extra_config.exs
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions test/plausible/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4242b52

Please sign in to comment.