Allow hyphen in database name

pull/5778/head
royki 2 years ago
parent 5cd6ae5df4
commit 04a0db83a3
  1. 2
      apps/explorer/lib/explorer/repo/config_helper.ex
  2. 12
      apps/explorer/test/explorer/repo/config_helper_test.exs

@ -29,7 +29,7 @@ defmodule Explorer.Repo.ConfigHelper do
# sobelow_skip ["DOS.StringToAtom"]
defp extract_parameters(database_url) do
~r/\w*:\/\/(?<username>\w+):(?<password>[a-zA-Z0-9-*#!%^&$_]*)?@(?<hostname>(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])):(?<port>\d+)\/(?<database>\w+)/
~r/\w*:\/\/(?<username>\w+):(?<password>[a-zA-Z0-9-*#!%^&$_]*)?@(?<hostname>(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])):(?<port>\d+)\/(?<database>[a-zA-Z0-9_-]*)/
|> Regex.named_captures(database_url)
|> Keyword.new(fn {k, v} -> {String.to_atom(k), v} end)
|> Keyword.put(:url, database_url)

@ -28,6 +28,18 @@ defmodule Explorer.Repo.ConfigHelperTest do
assert result[:database] == "test_database"
end
test "parse params from database url with hyphen in databasename" do
database_url = "postgresql://test_username:test_password@host-name.test.com:7777/test-database"
result = ConfigHelper.get_db_config(%{url: database_url, env_func: fn _ -> nil end})
assert result[:username] == "test_username"
assert result[:password] == "test_password"
assert result[:hostname] == "host-name.test.com"
assert result[:port] == "7777"
assert result[:database] == "test-database"
end
test "parse params from database url with special characters in password" do
database_url = "postgresql://test_username:awN!l#W*g$P%t-l^q&d@hostname.test.com:7777/test_database"

Loading…
Cancel
Save