refactor: test database config (#9662)

* refactor: test database config

* feat: configure test db with special envs

* fix: prefer `url` from config over raw env

* refactor: remove redundant fallback

* refactor: use `ConfigHelper.init_repo_module/2` to configure `ZkSync` repo
pull/10059/head
Fedor Ivanov 6 months ago committed by GitHub
parent 6fa60b4e4b
commit 6682065d3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 32
      apps/explorer/config/test.exs
  2. 32
      apps/explorer/lib/explorer/repo.ex
  3. 2
      apps/explorer/lib/explorer/repo/config_helper.ex
  4. 4
      docker-compose/envs/common-blockscout.env

@ -3,10 +3,15 @@ import Config
# Lower hashing rounds for faster tests
config :bcrypt_elixir, log_rounds: 4
database_url = System.get_env("TEST_DATABASE_URL")
database = if database_url, do: nil, else: "explorer_test"
hostname = if database_url, do: nil, else: "localhost"
# Configure your database
config :explorer, Explorer.Repo,
database: "explorer_test",
hostname: "localhost",
database: database,
hostname: hostname,
url: database_url,
pool: Ecto.Adapters.SQL.Sandbox,
# Default of `5_000` was too low for `BlockFetcher` test
ownership_timeout: :timer.minutes(7),
@ -17,8 +22,9 @@ config :explorer, Explorer.Repo,
# Configure API database
config :explorer, Explorer.Repo.Replica1,
database: "explorer_test",
hostname: "localhost",
database: database,
hostname: hostname,
url: database_url,
pool: Ecto.Adapters.SQL.Sandbox,
# Default of `5_000` was too low for `BlockFetcher` test
ownership_timeout: :timer.minutes(1),
@ -32,10 +38,14 @@ config :explorer, :proxy,
fallback_cached_implementation_data_ttl: :timer.seconds(20),
implementation_data_fetching_timeout: :timer.seconds(20)
account_database_url = System.get_env("TEST_DATABASE_READ_ONLY_API_URL") || database_url
account_database = if account_database_url, do: nil, else: "explorer_test_account"
# Configure API database
config :explorer, Explorer.Repo.Account,
database: "explorer_test_account",
hostname: "localhost",
database: account_database,
hostname: hostname,
url: account_database_url,
pool: Ecto.Adapters.SQL.Sandbox,
# Default of `5_000` was too low for `BlockFetcher` test
ownership_timeout: :timer.minutes(1),
@ -58,8 +68,9 @@ for repo <- [
Explorer.Repo.Mud
] do
config :explorer, repo,
database: "explorer_test",
hostname: "localhost",
database: database,
hostname: hostname,
url: database_url,
pool: Ecto.Adapters.SQL.Sandbox,
# Default of `5_000` was too low for `BlockFetcher` test
ownership_timeout: :timer.minutes(1),
@ -70,8 +81,9 @@ for repo <- [
end
config :explorer, Explorer.Repo.PolygonZkevm,
database: "explorer_test",
hostname: "localhost",
database: database,
hostname: hostname,
url: database_url,
pool: Ecto.Adapters.SQL.Sandbox,
# Default of `5_000` was too low for `BlockFetcher` test
ownership_timeout: :timer.minutes(1),

@ -12,21 +12,7 @@ defmodule Explorer.Repo do
DATABASE_URL environment variable.
"""
def init(_, opts) do
db_url = System.get_env("DATABASE_URL")
repo_conf = Application.get_env(:explorer, Explorer.Repo)
merged =
%{url: db_url}
|> ConfigHelper.get_db_config()
|> Keyword.merge(repo_conf, fn
_key, v1, nil -> v1
_key, nil, v2 -> v2
_, _, v2 -> v2
end)
Application.put_env(:explorer, Explorer.Repo, merged)
{:ok, Keyword.put(opts, :url, db_url)}
ConfigHelper.init_repo_module(__MODULE__, opts)
end
def logged_transaction(fun_or_multi, opts \\ []) do
@ -187,21 +173,7 @@ defmodule Explorer.Repo do
adapter: Ecto.Adapters.Postgres
def init(_, opts) do
db_url = Application.get_env(:explorer, __MODULE__)[:url]
repo_conf = Application.get_env(:explorer, __MODULE__)
merged =
%{url: db_url}
|> ConfigHelper.get_db_config()
|> Keyword.merge(repo_conf, fn
_key, v1, nil -> v1
_key, nil, v2 -> v2
_, _, v2 -> v2
end)
Application.put_env(:explorer, __MODULE__, merged)
{:ok, Keyword.put(opts, :url, db_url)}
ConfigHelper.init_repo_module(__MODULE__, opts)
end
end

@ -17,7 +17,7 @@ defmodule Explorer.Repo.ConfigHelper do
]
def get_db_config(opts) do
url_encoded = opts[:url] || System.get_env("DATABASE_URL")
url_encoded = opts[:url]
url = url_encoded && URI.decode(url_encoded)
env_function = opts[:env_func] || (&System.get_env/1)

@ -3,6 +3,8 @@ ETHEREUM_JSONRPC_HTTP_URL=http://host.docker.internal:8545/
# ETHEREUM_JSONRPC_FALLBACK_HTTP_URL=
DATABASE_URL=postgresql://blockscout:ceWb1MeLBEeOIfk65gU8EjF8@db:5432/blockscout
# DATABASE_QUEUE_TARGET
# TEST_DATABASE_URL=
# TEST_DATABASE_READ_ONLY_API_URL=
ETHEREUM_JSONRPC_TRACE_URL=http://host.docker.internal:8545/
# ETHEREUM_JSONRPC_FALLBACK_TRACE_URL=
# ETHEREUM_JSONRPC_FALLBACK_ETH_CALL_URL=
@ -373,4 +375,4 @@ TENDERLY_CHAIN_PATH=
# BRIDGED_TOKENS_FOREIGN_JSON_RPC
# MUD_INDEXER_ENABLED=
# MUD_DATABASE_URL=
# MUD_POOL_SIZE=50
# MUD_POOL_SIZE=50

Loading…
Cancel
Save