Skip to content

Commit 1a70d03

Browse files
authored
fix: Run background migrations immediately on green install (blockscout#14424)
1 parent 4a7e483 commit 1a70d03

11 files changed

Lines changed: 61 additions & 15 deletions

apps/explorer/lib/explorer/application.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ defmodule Explorer.Application do
223223
Explorer.Migrator.HeavyDbIndexOperation.CreateAddressesVerifiedIndex,
224224
:indexer
225225
),
226-
configure_mode_dependent_process(Explorer.Migrator.HeavyDbIndexOperation.CreateLogsBlockHashIndex, :indexer),
227226
configure_mode_dependent_process(
228227
Explorer.Migrator.HeavyDbIndexOperation.DropLogsBlockNumberAscIndexAscIndex,
229228
:indexer
230229
),
230+
configure_mode_dependent_process(Explorer.Migrator.HeavyDbIndexOperation.CreateLogsBlockHashIndex, :indexer),
231231
configure_mode_dependent_process(
232232
Explorer.Migrator.HeavyDbIndexOperation.CreateLogsAddressHashBlockNumberDescIndexDescIndex,
233233
:indexer
@@ -381,10 +381,6 @@ defmodule Explorer.Application do
381381
Explorer.Migrator.HeavyDbIndexOperation.DropTransactionsCreatedContractAddressHashWithPendingIndexA,
382382
:indexer
383383
),
384-
configure_mode_dependent_process(
385-
Explorer.Migrator.HeavyDbIndexOperation.RemoveInternalTransactionsBlockHashTransactionHashBlockIndexError,
386-
:indexer
387-
),
388384
configure_mode_dependent_process(
389385
Explorer.Migrator.HeavyDbIndexOperation.CreateInternalTransactionsFromAddressIdPartialIndex,
390386
:indexer
@@ -401,6 +397,10 @@ defmodule Explorer.Application do
401397
Explorer.Migrator.HeavyDbIndexOperation.CreateInternalTransactionsBlockNumberCreatedContractAddressIdPartialIndex,
402398
:indexer
403399
),
400+
configure_mode_dependent_process(
401+
Explorer.Migrator.HeavyDbIndexOperation.RemoveInternalTransactionsBlockHashTransactionHashBlockIndexError,
402+
:indexer
403+
),
404404
configure_mode_dependent_process(
405405
Explorer.Migrator.HeavyDbIndexOperation.DropInternalTransactionsBlockNumberCreatedContractAddressHashIndex,
406406
:indexer

apps/explorer/lib/explorer/migrator/filling_migration.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ defmodule Explorer.Migrator.FillingMigration do
171171

172172
import Ecto.Query
173173

174+
alias Explorer.Chain.Block
174175
alias Explorer.Migrator.HeavyDbIndexOperation.Helper, as: HeavyDbIndexOperationHelper
175176
alias Explorer.Migrator.MigrationStatus
176177
alias Explorer.Repo
@@ -195,7 +196,13 @@ defmodule Explorer.Migrator.FillingMigration do
195196

196197
@impl true
197198
def init(_) do
198-
{:ok, %{}, {:continue, :ok}}
199+
if Repo.exists?(Block) do
200+
{:ok, %{}, {:continue, :ok}}
201+
else
202+
MigrationStatus.set_status(migration_name(), "completed")
203+
update_cache()
204+
:ignore
205+
end
199206
end
200207

201208
# Called once when the GenServer starts to initialize the migration process by checking its

apps/explorer/lib/explorer/migrator/heavy_db_index_operation.ex

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ defmodule Explorer.Migrator.HeavyDbIndexOperation do
130130
import Ecto.Query
131131

132132
alias Ecto.Adapters.SQL
133+
alias Explorer.Chain.Block
133134
alias Explorer.Migrator.HeavyDbIndexOperation.Helper, as: HeavyDbIndexOperationHelper
134135
alias Explorer.Migrator.MigrationStatus
135136
alias Explorer.Repo
@@ -152,7 +153,22 @@ defmodule Explorer.Migrator.HeavyDbIndexOperation do
152153

153154
@impl true
154155
def init(_) do
155-
{:ok, %{}, {:continue, :ok}}
156+
with {:green_install?, true} <- {:green_install?, not Repo.exists?(Block)},
157+
{:migration_finished?, false} <- {:migration_finished?, migration_finished?()},
158+
{:dependent_from_migrations_completed?, true} <-
159+
{:dependent_from_migrations_completed?, dependent_from_migrations_completed?()},
160+
{:db_index_operation, :ok} <- {:db_index_operation, db_index_operation()} do
161+
MigrationStatus.set_status(migration_name(), "completed")
162+
update_cache()
163+
:ignore
164+
else
165+
{:migration_finished?, true} ->
166+
update_cache()
167+
:ignore
168+
169+
_ ->
170+
{:ok, %{}, {:continue, :ok}}
171+
end
156172
end
157173

158174
@impl true
@@ -226,16 +242,20 @@ defmodule Explorer.Migrator.HeavyDbIndexOperation do
226242
if running_other_heavy_migration_exists?(migration_name()) do
227243
false
228244
else
229-
if Enum.empty?(dependent_from_migrations()) do
230-
true
231-
else
232-
all_statuses =
233-
MigrationStatus.fetch_migration_statuses(dependent_from_migrations())
245+
dependent_from_migrations_completed?()
246+
end
247+
end
234248

235-
all_statuses_completed? = not Enum.empty?(all_statuses) && all_statuses |> Enum.all?(&(&1 == "completed"))
249+
defp dependent_from_migrations_completed? do
250+
if Enum.empty?(dependent_from_migrations()) do
251+
true
252+
else
253+
all_statuses =
254+
MigrationStatus.fetch_migration_statuses(dependent_from_migrations())
236255

237-
all_statuses_completed? && Enum.count(all_statuses) == Enum.count(dependent_from_migrations())
238-
end
256+
all_statuses_completed? = not Enum.empty?(all_statuses) && all_statuses |> Enum.all?(&(&1 == "completed"))
257+
258+
all_statuses_completed? && Enum.count(all_statuses) == Enum.count(dependent_from_migrations())
239259
end
240260
end
241261

apps/explorer/test/explorer/migrator/address_current_token_balance_token_type_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule Explorer.Migrator.AddressCurrentTokenBalanceTokenTypeTest do
99

1010
describe "Migrate current token balances" do
1111
test "Set token_type for not processed current token balances" do
12+
insert(:block)
13+
1214
Enum.each(0..10, fn _x ->
1315
current_token_balance = insert(:address_current_token_balance, token_type: nil)
1416
assert %{token_type: nil} = current_token_balance

apps/explorer/test/explorer/migrator/address_token_balance_token_type_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule Explorer.Migrator.AddressTokenBalanceTokenTypeTest do
99

1010
describe "Migrate token balances" do
1111
test "Set token_type for not processed token balances" do
12+
insert(:block)
13+
1214
Enum.each(0..10, fn _x ->
1315
token_balance = insert(:token_balance, token_type: nil)
1416
assert %{token_type: nil} = token_balance

apps/explorer/test/explorer/migrator/backfill_metadata_url_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ defmodule Explorer.Migrator.BackfillMetadataURLTest do
3131

3232
describe "BackfillMetadataURL" do
3333
test "complete migration" do
34+
insert(:block)
3435
token = insert(:token, type: "ERC-721")
3536

3637
insert(:token_instance,
@@ -127,6 +128,7 @@ defmodule Explorer.Migrator.BackfillMetadataURLTest do
127128
end
128129

129130
test "Resolve domain" do
131+
insert(:block)
130132
token = insert(:token, type: "ERC-721")
131133
env = Application.get_env(:indexer, Indexer.Fetcher.TokenInstance.Helper)
132134
Application.put_env(:explorer, Explorer.Migrator.BackfillMetadataURL, batch_size: 1, concurrency: 1)
@@ -279,6 +281,7 @@ defmodule Explorer.Migrator.BackfillMetadataURLTest do
279281
end
280282

281283
test "drop metadata on invalid token uri response" do
284+
insert(:block)
282285
token = insert(:token, type: "ERC-1155")
283286
env = Application.get_env(:indexer, Indexer.Fetcher.TokenInstance.Helper)
284287
Application.put_env(:explorer, Explorer.Migrator.BackfillMetadataURL, batch_size: 1, concurrency: 1)
@@ -349,6 +352,7 @@ defmodule Explorer.Migrator.BackfillMetadataURLTest do
349352
end
350353

351354
test "regression for https://github.com/blockscout/blockscout/issues/12389" do
355+
insert(:block)
352356
token = insert(:token, type: "ERC-721")
353357

354358
insert(:token_instance,

apps/explorer/test/explorer/migrator/merge_adjacent_missing_block_ranges_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ defmodule Explorer.Migrator.MergeAdjacentMissingBlockRangesTest do
77
alias Explorer.Utility.MissingBlockRange
88

99
test "Merges adjacent ranges" do
10+
insert(:block)
11+
1012
Repo.delete_all(MissingBlockRange)
1113

1214
insert(:missing_block_range, from_number: 100, to_number: 70, priority: nil)

apps/explorer/test/explorer/migrator/sanitize_duplicate_smart_contract_additional_sources_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ defmodule Explorer.Migrator.SanitizeDuplicateSmartContractAdditionalSourcesTest
1010

1111
describe "sanitize duplicates in smart_contracts_additional_sources" do
1212
test "removes duplicate rows keeping a single record per (address_hash, file_name)" do
13+
insert(:block)
14+
1315
sc1 = insert(:smart_contract)
1416
sc2 = insert(:smart_contract)
1517

@@ -53,6 +55,7 @@ defmodule Explorer.Migrator.SanitizeDuplicateSmartContractAdditionalSourcesTest
5355
end
5456

5557
test "completes gracefully when there are no duplicates" do
58+
insert(:block)
5659
sc = insert(:smart_contract)
5760

5861
%SmartContractAdditionalSource{}

apps/explorer/test/explorer/migrator/sanitize_missing_token_balances_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ defmodule Explorer.Migrator.SanitizeMissingTokenBalancesTest do
88

99
describe "Migrate token balances" do
1010
test "Unset value and value_fetched_at for token balances related to not processed current token balances" do
11+
insert(:block)
12+
1113
Enum.each(0..10, fn _x ->
1214
token_balance = insert(:token_balance)
1315

apps/explorer/test/explorer/migrator/unescape_ampersands_in_tokens_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ defmodule Explorer.Migrator.UnescapeAmpersandsInTokensTest do
66
alias Explorer.Repo
77

88
test "Unescapes ampersands in tokens" do
9+
insert(:block)
10+
911
escaped_name_token = insert(:token, name: "Rock &amp; Roll")
1012
escaped_symbol_token = insert(:token, symbol: "R&amp;R")
1113
escaped_both_token = insert(:token, name: "Tom &amp; Jerry", symbol: "T&amp;J")

0 commit comments

Comments
 (0)