Packages
surgex
4.15.0
6.0.1
6.0.0
5.1.1
5.1.0
5.0.0
5.0.0-git-015aa39a
4.15.2
4.15.1
4.15.1-git-98b3
4.15.0
4.15.0-git-943e
4.15.0-git-5806
4.14.1
4.14.0
4.13.1
4.13.0
4.12.0
4.11.0
4.11.0-git-97d4
4.11.0-git-14c8
4.10.0
4.10.0-git-37f2
4.9.0
4.9.0-git-9d5f
4.8.1-git-80a7
4.8.0
4.8.0-git-fbda
4.8.0-git-e058
4.7.0
4.7.0-git-5414
4.6.1
4.6.0
4.5.0
4.4.0
4.3.0
4.2.1
4.2.0
4.1.1
4.1.1-git-9f6b
4.1.1-git-6f0cd
4.1.0
4.1.0-git-e934
4.1.0-git-8c28
4.1.0-git-56a9
4.1.0-git-55fd
4.0.0
3.2.8
3.2.7
3.2.6
3.2.5
3.2.4
3.2.3
3.2.2
3.2.1
3.2.0
retired
3.1.0
3.0.0
2.24.1
2.24.0
retired
2.23.0
2.22.0
2.21.0
2.20.1
2.20.0
2.19.0
2.18.1
2.18.0
2.17.0
2.16.0
2.15.0
2.14.0
2.13.0
2.12.1
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.0
2.6.0
2.5.1
2.5.0
2.4.0
retired
2.3.0
2.2.1
2.2.0
2.1.1
2.1.0
2.0.0
1.6.0
1.5.2
1.5.1
1.5.0
1.4.0
1.3.1
retired
1.3.0
retired
1.2.1
1.2.0
1.1.0
1.0.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
All Things Elixir @ Surge Ventures Inc, the creators of Fresha
Current section
Files
Jump to
Current section
Files
lib/surgex/repo_helpers/repo_helpers.ex
defmodule Surgex.RepoHelpers do
@moduledoc """
Tools for dynamic setup of Ecto repo opts.
**NOTE: Deprecated in favor of Elixir 1.9 runtime configuration.**
"""
@doc """
Sets repo options from env vars starting with specified prefix.
## Examples
iex> System.put_env("DATABASE_URL", "postgres://localhost")
iex> System.put_env("DATABASE_SERVER_POOL_SIZE", "30")
iex> System.put_env("DATABASE_SSL", "true")
iex> Application.put_env(:phoenix, :serve_endpoints, true)
iex>
iex> final_opts = Surgex.RepoHelpers.set_opts([])
iex>
iex> Keyword.get(final_opts, :url)
"postgres://localhost"
iex> Keyword.get(final_opts, :pool_size)
30
iex> Keyword.get(final_opts, :ssl)
true
"""
def set_opts(opts, env_prefix \\ :database) do
upcase_env_prefix =
env_prefix
|> to_string()
|> String.upcase()
opts
|> set_url("#{upcase_env_prefix}_URL")
|> set_pool_size("#{upcase_env_prefix}_POOL_SIZE")
|> set_server_pool_size("#{upcase_env_prefix}_SERVER_POOL_SIZE")
|> set_ssl("#{upcase_env_prefix}_SSL")
|> set_application_name()
end
@doc """
Sets repo database URL from specified env var.
"""
def set_url(opts, env) do
Keyword.put(opts, :url, System.get_env(env))
end
@doc """
Sets repo database pool size from specified env var.
"""
def set_pool_size(opts, env) do
with env_value when is_binary(env_value) <- System.get_env(env),
{server_pool_size, ""} <- Integer.parse(env_value) do
Keyword.put(opts, :pool_size, server_pool_size)
else
_ -> opts
end
end
@doc """
Sets repo database pool size from specified env var only if Phoenix server is configured to run.
"""
def set_server_pool_size(opts, env) do
if Application.get_env(:phoenix, :serve_endpoints) do
set_pool_size(opts, env)
else
opts
end
end
@doc """
Sets repo database ssl enable from specified env var.
"""
def set_ssl(opts, env) do
with env_value when is_binary(env_value) <- System.get_env(env),
ssl_value <- String.downcase(env_value) do
case ssl_value do
"true" -> Keyword.put(opts, :ssl, true)
"false" -> Keyword.put(opts, :ssl, false)
_ -> opts
end
else
_ -> opts
end
end
@doc """
Sets application_name to the value of APP_NAME env var.
"""
def set_application_name(opts) do
with env_value when is_binary(env_value) <- System.get_env("APP_NAME"),
application_name <- String.slice(env_value, 0, 63),
parameters <- Keyword.get(opts, :parameters, []) do
updated_parameters = Keyword.put(parameters, :application_name, application_name)
Keyword.put(opts, :parameters, updated_parameters)
else
_ -> opts
end
end
end