fix migration

pull/1370/head
Ayrat Badykov 6 years ago
parent 915d053dd5
commit b780876b3c
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 55
      apps/explorer/lib/explorer/chain/import/runner/transactions.ex
  2. 3
      apps/explorer/priv/repo/migrations/20190124082812_add_index_on_transaction_nonce_and_from_address_hash.exs

@ -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

@ -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

Loading…
Cancel
Save