From b780876b3cd203c3d28e524629d6eaef6396f6ef Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 24 Jan 2019 16:24:29 +0300 Subject: [PATCH] fix migration --- .../chain/import/runner/transactions.ex | 55 +++++++++++-------- ...ransaction_nonce_and_from_address_hash.exs | 3 +- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/transactions.ex b/apps/explorer/lib/explorer/chain/import/runner/transactions.ex index 63b833e3f3..48fb872438 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/transactions.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/transactions.ex @@ -188,27 +188,38 @@ defmodule Explorer.Chain.Import.Runner.Transactions do defp put_internal_transactions_indexed_at?(_, _), do: false defp update_replaced_transactions(repo, transactions, %{timeout: timeout}) do - transactions - |> Enum.filter(& &1.transaction.block_hash) - |> Enum.map(fn transaction -> {transaction.nonce, transaction.from_address_hash} end) - |> Enum.uniq() - |> Enum.map(fn {nonce, from_address_hash} -> - from(t in Transaction, - where: t.nonce == ^nonce and t.from_address_hash == ^from_address_hash and is_nil(t.block_hash), - update: [ - set: [status: ^:error, error: "dropped/replaced"] - ] - ) - end) - |> Enum.map(fn query -> - try do - {_, result} = repo.update(query, [], timeout: timeout) - - {:ok, result} - rescue - postgrex_error in Postgrex.Error -> - {:error, %{exception: postgrex_error, query: query}} - end - end) + result = + transactions + |> Enum.filter(& &1.block_hash) + |> Enum.map(fn transaction -> {transaction.nonce, transaction.from_address_hash} end) + |> Enum.uniq() + |> Enum.map(fn {nonce, from_address_hash} -> + from(t in Transaction, + where: t.nonce == ^nonce and t.from_address_hash == ^from_address_hash and is_nil(t.block_hash), + update: [ + set: [status: ^:error, error: "dropped/replaced"] + ] + ) + end) + |> Enum.map(fn query -> + try do + {_, result} = repo.update_all(query, [], timeout: timeout) + + {:ok, result} + rescue + postgrex_error in Postgrex.Error -> + {:error, %{exception: postgrex_error, query: query}} + end + end) + + first_error = + Enum.find(result, fn result -> + case result do + {:ok, _} -> false + {:error, _} -> true + end + end) + + if first_error, do: first_error, else: {:ok, []} end end diff --git a/apps/explorer/priv/repo/migrations/20190124082812_add_index_on_transaction_nonce_and_from_address_hash.exs b/apps/explorer/priv/repo/migrations/20190124082812_add_index_on_transaction_nonce_and_from_address_hash.exs index fd53e4fcc0..b899304c3b 100644 --- a/apps/explorer/priv/repo/migrations/20190124082812_add_index_on_transaction_nonce_and_from_address_hash.exs +++ b/apps/explorer/priv/repo/migrations/20190124082812_add_index_on_transaction_nonce_and_from_address_hash.exs @@ -8,7 +8,7 @@ defmodule Explorer.Repo.Migrations.AddIndexOnTransactionNonceAndFromAddressHash @timeout 60_000 * 30 def change do - create(index(:transactions, [:nonce, :from_address_hash])) + create(index(:transactions, [:nonce, :from_address_hash, :block_hash])) # for replaced/dropeed transactions create(index(:transactions, [:block_hash, :error])) @@ -20,6 +20,7 @@ defmodule Explorer.Repo.Migrations.AddIndexOnTransactionNonceAndFromAddressHash SQL.query!( Repo, query, + [], timeout: @timeout ) end