diff --git a/CHANGELOG.md b/CHANGELOG.md index 3599cf86a5..49f87e0e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ - [#6449](https://github.com/blockscout/blockscout/pull/6449) - Search min_missing_block_number from zero - [#6492](https://github.com/blockscout/blockscout/pull/6492) - Remove token instance owner fetching - [#6536](https://github.com/blockscout/blockscout/pull/6536) - Fix internal transactions query +- [#6550](https://github.com/blockscout/blockscout/pull/6550) - Query token transfers before updating ### Chore diff --git a/apps/explorer/lib/explorer/token_transfer_token_id_migration/worker.ex b/apps/explorer/lib/explorer/token_transfer_token_id_migration/worker.ex index 24a8379951..f7916ed058 100644 --- a/apps/explorer/lib/explorer/token_transfer_token_id_migration/worker.ex +++ b/apps/explorer/lib/explorer/token_transfer_token_id_migration/worker.ex @@ -36,7 +36,7 @@ defmodule Explorer.TokenTransferTokenIdMigration.Worker do @impl true def handle_info(:update, %{current_range: {lower_bound, upper_bound}} = state) do case do_update(lower_bound, upper_bound) do - {_total, _result} -> + true -> LowestBlockNumberUpdater.add_range(upper_bound, lower_bound) new_range = calculate_new_range(lower_bound, state.bottom_block, state.batch_size, state.step) schedule_next_update() @@ -60,21 +60,22 @@ defmodule Explorer.TokenTransferTokenIdMigration.Worker do end defp do_update(lower_bound, upper_bound) do - query = + token_transfers_batch_query = from( tt in TokenTransfer, where: tt.block_number >= ^lower_bound, - where: tt.block_number <= ^upper_bound, - where: not is_nil(tt.token_id), - update: [ - set: [ - token_ids: fragment("ARRAY_APPEND(ARRAY[]::decimal[], ?)", tt.token_id), - token_id: nil - ] - ] + where: tt.block_number <= ^upper_bound ) - Repo.update_all(query, [], timeout: :infinity) + token_transfers_batch_query + |> Repo.all() + |> Enum.filter(fn %{token_id: token_id} -> not is_nil(token_id) end) + |> Enum.map(fn token_transfer -> + token_transfer + |> TokenTransfer.changeset(%{token_ids: [token_transfer.token_id], token_id: nil}) + |> Repo.update() + end) + |> Enum.all?(&match?({:ok, _}, &1)) end defp schedule_next_update do