Skip to content

Commit bc2af4c

Browse files
authored
chore: Put backend versions into constants on launch (blockscout#14072)
1 parent 06e7f4e commit bc2af4c

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

apps/explorer/config/config.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ config :explorer, Explorer.Migrator.DeleteZeroValueInternalTransactions, enabled
115115

116116
config :explorer, Explorer.Chain.Mud, enabled: ConfigHelper.parse_bool_env_var("MUD_INDEXER_ENABLED")
117117

118+
config :explorer, Explorer.Utility.VersionConstantsUpdater, enabled: true
119+
118120
for migrator <- [
119121
# Background migrations
120122
Explorer.Migrator.TransactionsDenormalization,

apps/explorer/lib/explorer/application.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ defmodule Explorer.Application do
116116
defp configurable_children do
117117
configurable_children_set =
118118
[
119+
configure_mode_dependent_process(Explorer.Utility.VersionConstantsUpdater, :indexer),
119120
configure_mode_dependent_process(Explorer.Market.Fetcher.Coin, :api),
120121
configure_mode_dependent_process(Explorer.Market.Fetcher.Token, :indexer),
121122
configure_mode_dependent_process(Explorer.Market.Fetcher.History, :indexer),

apps/explorer/lib/explorer/application/constants.ex

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

1010
@keys_manager_contract_address_key "keys_manager_contract_address"
1111
@last_processed_erc_721_token "token_instance_sanitizer_last_processed_erc_721_token"
12+
@previous_backend_version_key "previous_backend_version"
13+
@current_backend_version_key "current_backend_version"
1214

1315
@primary_key false
1416
typed_schema "constants" do
@@ -102,4 +104,54 @@ defmodule Explorer.Application.Constants do
102104
def get_last_processed_token_address_hash(options \\ []) do
103105
@last_processed_erc_721_token |> get_constant_value(options) |> Chain.string_to_address_hash_or_nil()
104106
end
107+
108+
@doc """
109+
Inserts the previously running backend version into constants storage.
110+
111+
## Parameters
112+
- `version`: Backend version string to persist.
113+
114+
## Returns
115+
- The inserted or updated `%Explorer.Application.Constants{}` record.
116+
"""
117+
@spec insert_previous_backend_version(String.t()) :: Ecto.Schema.t()
118+
def insert_previous_backend_version(version) do
119+
set_constant_value(@previous_backend_version_key, version)
120+
end
121+
122+
@doc """
123+
Fetches the previously running backend version from constants storage.
124+
125+
## Returns
126+
- Previous backend version.
127+
"""
128+
@spec get_previous_backend_version() :: nil | String.t()
129+
def get_previous_backend_version do
130+
get_constant_value(@previous_backend_version_key)
131+
end
132+
133+
@doc """
134+
Inserts the current running backend version into constants storage.
135+
136+
## Parameters
137+
- `version`: Backend version string to persist.
138+
139+
## Returns
140+
- The inserted or updated `%Explorer.Application.Constants{}` record.
141+
"""
142+
@spec insert_current_backend_version(String.t()) :: Ecto.Schema.t()
143+
def insert_current_backend_version(version) do
144+
set_constant_value(@current_backend_version_key, version)
145+
end
146+
147+
@doc """
148+
Fetches the current running backend version from constants storage.
149+
150+
## Returns
151+
- Current backend version.
152+
"""
153+
@spec get_current_backend_version() :: nil | String.t()
154+
def get_current_backend_version do
155+
get_constant_value(@current_backend_version_key)
156+
end
105157
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule Explorer.Utility.VersionConstantsUpdater do
2+
@moduledoc """
3+
Module responsible for updating current and previous backend version in table `constants`.
4+
"""
5+
6+
use GenServer
7+
8+
alias Explorer.Application.Constants
9+
10+
@spec start_link(term()) :: GenServer.on_start()
11+
def start_link(_) do
12+
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
13+
end
14+
15+
def init(_) do
16+
set_versions()
17+
:ignore
18+
end
19+
20+
defp set_versions do
21+
stored_current_version = Constants.get_current_backend_version()
22+
current_version = to_string(Application.spec(:explorer, :vsn))
23+
24+
if not is_nil(stored_current_version) and stored_current_version != current_version do
25+
Constants.insert_previous_backend_version(stored_current_version)
26+
end
27+
28+
if stored_current_version != current_version do
29+
Constants.insert_current_backend_version(current_version)
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)