Skip to content

Commit

Permalink
fix: Avoid migration when possible on healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed Apr 18, 2024
1 parent b4d8561 commit 7a42610
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
28 changes: 26 additions & 2 deletions lib/realtime/tenants.ex
Expand Up @@ -67,14 +67,38 @@ defmodule Realtime.Tenants do
{:ok, health_conn} ->
connected_cluster = UsersCounter.tenant_users(external_id)
%{extensions: [%{settings: settings} | _]} = Cache.get_tenant_by_external_id(external_id)
Helpers.transaction(health_conn, fn _ -> Migrations.run_migrations(settings) end)

Helpers.transaction(health_conn, fn transaction_conn ->
res =
Postgrex.query!(
transaction_conn,
"select * from pg_catalog.pg_tables where schemaname = 'realtime' and tablename = 'schema_migrations';",
[]
)

if res.rows == [] do
Migrations.run_migrations(settings)
end
end)

{:ok, %{healthy: true, db_connected: true, connected_cluster: connected_cluster}}

connected_cluster when is_integer(connected_cluster) ->
{:ok, db_conn} = Connect.lookup_or_start_connection(external_id)
%{extensions: [%{settings: settings} | _]} = Cache.get_tenant_by_external_id(external_id)
Helpers.transaction(db_conn, fn _ -> Migrations.run_migrations(settings) end)

Helpers.transaction(db_conn, fn transaction_conn ->
res =
Postgrex.query!(
transaction_conn,
"select * from pg_catalog.pg_tables where schemaname = 'realtime' and tablename = 'schema_migrations';",
[]
)

if res.rows == [] do
Migrations.run_migrations(settings)
end
end)

{:ok, %{healthy: true, db_connected: false, connected_cluster: connected_cluster}}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.28.25",
version: "2.28.26",
elixir: "~> 1.14.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit 7a42610

Please sign in to comment.