From a62516913a0c3a3db3b5d9a823f0b44f0a9fd185 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 25 Feb 2020 19:33:04 +0300 Subject: [PATCH] Fix splitting setup --- CHANGELOG.md | 1 + apps/explorer/lib/explorer/chain.ex | 11 +++++++++++ .../lib/explorer/chain/events/listener.ex | 9 ++++++++- apps/explorer/test/explorer/chain_test.exs | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 411431fcc2..8fe447f507 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#2834](https://github.com/poanetwork/blockscout/pull/2834) - always redirect to checksummed hash ### Fixes +- [#3025](https://github.com/poanetwork/blockscout/pull/3025) - Fix splitting of indexer/web components setup - [#3021](https://github.com/poanetwork/blockscout/pull/3021), [#3022](https://github.com/poanetwork/blockscout/pull/3022) - Refine dev/test config - [#3016](https://github.com/poanetwork/blockscout/pull/3016), [#3017](https://github.com/poanetwork/blockscout/pull/3017) - Fix token instance QR code data - [#3014](https://github.com/poanetwork/blockscout/pull/3014) - Fix checksum address feature for tokens pages diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index e9b31519e7..6bd95e8811 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -4033,4 +4033,15 @@ defmodule Explorer.Chain do defp boolean_to_check_result(true), do: :ok defp boolean_to_check_result(false), do: :not_found + + def extract_db_name(db_url) do + if db_url == nil do + "" + else + db_url + |> String.split("/") + |> Enum.take(-1) + |> Enum.at(0) + end + end end diff --git a/apps/explorer/lib/explorer/chain/events/listener.ex b/apps/explorer/lib/explorer/chain/events/listener.ex index 2d93eeada0..ebb637c30e 100644 --- a/apps/explorer/lib/explorer/chain/events/listener.ex +++ b/apps/explorer/lib/explorer/chain/events/listener.ex @@ -6,15 +6,22 @@ defmodule Explorer.Chain.Events.Listener do use GenServer alias Postgrex.Notifications + import Explorer.Chain, only: [extract_db_name: 1] def start_link(_) do GenServer.start_link(__MODULE__, "chain_event", name: __MODULE__) end def init(channel) do - {:ok, pid} = + explorer_repo = :explorer |> Application.get_env(Explorer.Repo) + + db_url = explorer_repo[:url] + + {:ok, pid} = + explorer_repo + |> Keyword.put(:database, extract_db_name(db_url)) |> Notifications.start_link() ref = Notifications.listen!(pid, channel) diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index b37edbc36e..85bc03245f 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -4608,4 +4608,21 @@ defmodule Explorer.ChainTest do assert third.delta == Decimal.new(1) end end + + describe "extract_db_name/1" do + test "extracts correct db name" do + db_url = "postgresql://viktor:@localhost:5432/blockscout-dev-1" + assert Chain.extract_db_name(db_url) == "blockscout-dev-1" + end + + test "returns empty db name" do + db_url = "" + assert Chain.extract_db_name(db_url) == "" + end + + test "returns nil db name" do + db_url = nil + assert Chain.extract_db_name(db_url) == "" + end + end end