diff --git a/apps/explorer/lib/explorer/repo/config_helper.ex b/apps/explorer/lib/explorer/repo/config_helper.ex index 68b18e005a..bfed70d369 100644 --- a/apps/explorer/lib/explorer/repo/config_helper.ex +++ b/apps/explorer/lib/explorer/repo/config_helper.ex @@ -49,7 +49,7 @@ defmodule Explorer.Repo.ConfigHelper do Application.put_env(:explorer, module, merged) - {:ok, Keyword.put(opts, :url, db_url)} + {:ok, opts |> Keyword.put(:url, remove_search_path(db_url)) |> Keyword.merge(Keyword.take(merged, [:search_path]))} end def ssl_enabled?, do: String.equivalent?(System.get_env("ECTO_USE_SSL") || "true", "true") @@ -58,10 +58,44 @@ defmodule Explorer.Repo.ConfigHelper do # sobelow_skip ["DOS.StringToAtom"] def extract_parameters(database_url) do - ~r/\w*:\/\/(?[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][A-Za-z0-9\-]*[A-Za-z0-9])):(?\d+)\/(?[a-zA-Z0-9_-]*)/ + ~r/\w*:\/\/(?[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][A-Za-z0-9\-]*[A-Za-z0-9])):(?\d+)\/(?[a-zA-Z0-9_\-]*)(\?.*search_path=(?[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) + |> adjust_search_path() + end + + defp adjust_search_path(params) do + case params[:search_path] do + empty when empty in [nil, ""] -> Keyword.delete(params, :search_path) + [_search_path] -> params + search_path -> Keyword.put(params, :search_path, [search_path]) + end + end + + # Workaround for Ecto.Repo init. + # It takes parameters from the url in priority over provided options (as strings) + # while Postgrex expects search_path to be a list + # which means that it will always crash if there is a search_path parameter in DB url. + # That's why we need to remove this parameter from DB url before passing it to Ecto. + defp remove_search_path(nil), do: nil + + defp remove_search_path(db_url) do + case URI.parse(db_url) do + %{query: nil} -> + db_url + + %{query: query} = uri -> + query_without_search_path = + query + |> URI.decode_query() + |> Map.delete("search_path") + |> URI.encode_query() + + uri + |> Map.put(:query, query_without_search_path) + |> URI.to_string() + end end defp get_env_vars(vars, env_function) do diff --git a/apps/explorer/priv/account/migrations/20231207201701_add_watchlist_id_column.exs b/apps/explorer/priv/account/migrations/20231207201701_add_watchlist_id_column.exs index 346c9ec05e..176b65cd5a 100644 --- a/apps/explorer/priv/account/migrations/20231207201701_add_watchlist_id_column.exs +++ b/apps/explorer/priv/account/migrations/20231207201701_add_watchlist_id_column.exs @@ -3,7 +3,7 @@ defmodule Explorer.Repo.Account.Migrations.AddWatchlistIdColumn do def change do execute(""" - ALTER TABLE public.account_watchlist_notifications + ALTER TABLE account_watchlist_notifications DROP CONSTRAINT account_watchlist_notifications_watchlist_address_id_fkey; """) diff --git a/apps/explorer/priv/repo/migrations/20191203112646_internal_transactions_add_to_address_hash_index.exs b/apps/explorer/priv/repo/migrations/20191203112646_internal_transactions_add_to_address_hash_index.exs index f0ab878760..bf9bc5ce10 100644 --- a/apps/explorer/priv/repo/migrations/20191203112646_internal_transactions_add_to_address_hash_index.exs +++ b/apps/explorer/priv/repo/migrations/20191203112646_internal_transactions_add_to_address_hash_index.exs @@ -3,15 +3,15 @@ defmodule Explorer.Repo.Migrations.InternalTransactionsAddToAddressHashIndex do def change do execute( - "CREATE INDEX IF NOT EXISTS internal_transactions_from_address_hash_partial_index on public.internal_transactions(from_address_hash, block_number DESC, transaction_index DESC, index DESC) WHERE (((type = 'call') AND (index > 0)) OR (type != 'call'));" + "CREATE INDEX IF NOT EXISTS internal_transactions_from_address_hash_partial_index on internal_transactions(from_address_hash, block_number DESC, transaction_index DESC, index DESC) WHERE (((type = 'call') AND (index > 0)) OR (type != 'call'));" ) execute( - "CREATE INDEX IF NOT EXISTS internal_transactions_to_address_hash_partial_index on public.internal_transactions(to_address_hash, block_number DESC, transaction_index DESC, index DESC) WHERE (((type = 'call') AND (index > 0)) OR (type != 'call'));" + "CREATE INDEX IF NOT EXISTS internal_transactions_to_address_hash_partial_index on internal_transactions(to_address_hash, block_number DESC, transaction_index DESC, index DESC) WHERE (((type = 'call') AND (index > 0)) OR (type != 'call'));" ) execute( - "CREATE INDEX IF NOT EXISTS internal_transactions_created_contract_address_hash_partial_index on public.internal_transactions(created_contract_address_hash, block_number DESC, transaction_index DESC, index DESC) WHERE (((type = 'call') AND (index > 0)) OR (type != 'call'));" + "CREATE INDEX IF NOT EXISTS internal_transactions_created_contract_address_hash_partial_index on internal_transactions(created_contract_address_hash, block_number DESC, transaction_index DESC, index DESC) WHERE (((type = 'call') AND (index > 0)) OR (type != 'call'));" ) drop_if_exists( diff --git a/apps/explorer/priv/repo/migrations/20220919105140_add_method_id_index.exs b/apps/explorer/priv/repo/migrations/20220919105140_add_method_id_index.exs index 95ad38588c..daa96722b5 100644 --- a/apps/explorer/priv/repo/migrations/20220919105140_add_method_id_index.exs +++ b/apps/explorer/priv/repo/migrations/20220919105140_add_method_id_index.exs @@ -5,7 +5,7 @@ defmodule Explorer.Repo.Migrations.AddMethodIdIndex do def up do execute(""" - CREATE INDEX CONCURRENTLY IF NOT EXISTS method_id ON public.transactions USING btree (substring(input for 4)); + CREATE INDEX CONCURRENTLY IF NOT EXISTS method_id ON transactions USING btree (substring(input for 4)); """) end