Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ All notable changes to this project will be documented in this file.

- "Previous period" comparison with "Match day of week" now shifts to the nearest matching weekday instead of always the next one, which could overlap the selected range
- Cache warmer retries a failed warm-up of critical caches
- Fixed `DATABASE_URL` ignoring the port when connecting to PostgreSQL over a Unix socket, which made it impossible to use a socket file other than `.s.PGSQL.5432`
- Stats API `/api/v1/stats/breakdown` now returns a `400` error for an invalid `page` parameter instead of crashing with a `500`
- Fixed "Create Team" going through when the team name was rejected, creating the team under a name the user never entered
- Improve team member removal/team role change
Expand Down
16 changes: 14 additions & 2 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,11 @@ db_socket_dir? = String.starts_with?(db_host, "%2F") or db_host == ""

if db_socket_dir? do
[database] = String.split(db_uri.path, "/", trim: true)
db_query = URI.decode_query(db_uri.query || "")

socket_dir =
if db_host == "" do
db_host = (db_uri.query || "") |> URI.decode_query() |> Map.get("host")
db_host || raise ArgumentError, "DATABASE_URL=#{db_url} doesn't include host info"
db_query["host"] || raise ArgumentError, "DATABASE_URL=#{db_url} doesn't include host info"
else
URI.decode_www_form(db_host)
end
Expand All @@ -552,6 +552,18 @@ if db_socket_dir? do
socket_dir: socket_dir,
database: database

# The port is not used for networking over a Unix socket, but PostgreSQL appends it
# to the name of the socket file: `.s.PGSQL.<port>`
if db_port = db_uri.port || db_query["port"] do
case Integer.parse("#{db_port}") do
{db_port, ""} ->
config :plausible, Plausible.Repo, port: db_port

_ ->
raise ArgumentError, "DATABASE_URL=#{db_url} includes an invalid port: #{db_port}"
end
end

if userinfo = db_uri.userinfo do
[username, password] = String.split(userinfo, ":")

Expand Down
35 changes: 35 additions & 0 deletions test/plausible/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,41 @@ defmodule Plausible.ConfigTest do
]
end

test "socket_dir in hostname with port" do
env = [
{"DATABASE_URL", "postgresql://postgres:postgres@%2Frun%2Fpostgresql:5433/plausible_db"}
]

config = runtime_config(env)

assert get_in(config, [:plausible, Plausible.Repo]) == [
socket_dir: "/run/postgresql",
database: "plausible_db",
port: 5433,
username: "postgres",
password: "postgres"
]
end

test "socket_dir in query with port" do
env = [{"DATABASE_URL", "postgresql:///plausible_db?host=/run/postgresql&port=5433"}]
config = runtime_config(env)

assert get_in(config, [:plausible, Plausible.Repo]) == [
socket_dir: "/run/postgresql",
database: "plausible_db",
port: 5433
]
end

test "socket_dir in query with invalid port" do
env = [{"DATABASE_URL", "postgresql:///plausible_db?host=/run/postgresql&port=socket"}]

assert_raise ArgumentError, ~r/includes an invalid port: socket/, fn ->
runtime_config(env)
end
end

test "socket_dir missing" do
env = [{"DATABASE_URL", "postgresql:///plausible_db"}]
assert_raise ArgumentError, ~r/doesn't include host info/, fn -> runtime_config(env) end
Expand Down