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 put_internal_transactions_indexed_at?(_, _), do: false
defp update_replaced_transactions(repo, transactions, %{timeout: timeout}) do defp update_replaced_transactions(repo, transactions, %{timeout: timeout}) do
transactions result =
|> Enum.filter(& &1.transaction.block_hash) transactions
|> Enum.map(fn transaction -> {transaction.nonce, transaction.from_address_hash} end) |> Enum.filter(& &1.block_hash)
|> Enum.uniq() |> Enum.map(fn transaction -> {transaction.nonce, transaction.from_address_hash} end)
|> Enum.map(fn {nonce, from_address_hash} -> |> Enum.uniq()
from(t in Transaction, |> Enum.map(fn {nonce, from_address_hash} ->
where: t.nonce == ^nonce and t.from_address_hash == ^from_address_hash and is_nil(t.block_hash), from(t in Transaction,
update: [ where: t.nonce == ^nonce and t.from_address_hash == ^from_address_hash and is_nil(t.block_hash),
set: [status: ^:error, error: "dropped/replaced"] update: [
] set: [status: ^:error, error: "dropped/replaced"]
) ]
end) )
|> Enum.map(fn query -> end)
try do |> Enum.map(fn query ->
{_, result} = repo.update(query, [], timeout: timeout) try do
{_, result} = repo.update_all(query, [], timeout: timeout)
{:ok, result}
rescue {:ok, result}
postgrex_error in Postgrex.Error -> rescue
{:error, %{exception: postgrex_error, query: query}} postgrex_error in Postgrex.Error ->
end {:error, %{exception: postgrex_error, query: query}}
end) 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
end end

@ -8,7 +8,7 @@ defmodule Explorer.Repo.Migrations.AddIndexOnTransactionNonceAndFromAddressHash
@timeout 60_000 * 30 @timeout 60_000 * 30
def change do def change do
create(index(:transactions, [:nonce, :from_address_hash])) create(index(:transactions, [:nonce, :from_address_hash, :block_hash]))
# for replaced/dropeed transactions # for replaced/dropeed transactions
create(index(:transactions, [:block_hash, :error])) create(index(:transactions, [:block_hash, :error]))
@ -20,6 +20,7 @@ defmodule Explorer.Repo.Migrations.AddIndexOnTransactionNonceAndFromAddressHash
SQL.query!( SQL.query!(
Repo, Repo,
query, query,
[],
timeout: @timeout timeout: @timeout
) )
end end

Loading…
Cancel
Save