Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add better logging around maybe_migrate #1224

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ This is the list of operational codes that can help you understand your deployme
| UnableToConnectToTenantDatabase | Realtime was not able to connect to the tenant's database |
| RealtimeNodeDisconnected | Realtime is a distributed application and this means that one the system is unable to communicate with one of the distributed nodes |
| MigrationsFailedToRun | Error when running the migrations against the Tenant database that are required by Realtime |
| MigrationCheckFailed | Check to see if we require to run migrations fails |
| PartitionCreationFailed | Error when creating partitions for realtime.messages |
| ErrorStartingPostgresCDCStream | Error when starting the Postgres CDC stream which is used for Postgres Changes |
| UnknownDataProcessed | An unknown data type was processed by the Realtime system |
Expand Down
41 changes: 30 additions & 11 deletions lib/realtime/tenants/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ defmodule Realtime.Tenants.Migrations do
{20_241_108_114_728, MessagesUsingUuid}
]

@expected_migration_count length(@migrations)

defstruct [:tenant_external_id, :settings]
@spec run_migrations(map()) :: :ok | {:error, any()}
def run_migrations(%__MODULE__{tenant_external_id: tenant_external_id} = attrs) do
Expand Down Expand Up @@ -197,26 +195,47 @@ defmodule Realtime.Tenants.Migrations do
end)
end

# @expected_migration_count length(@migrations)

@doc """
Checks if the number of migrations ran in the database is equal to the expected number of migrations.

If not all migrations have been run, it will run the missing migrations.
"""
@spec maybe_run_migrations(pid(), Tenant.t()) :: :ok
def maybe_run_migrations(db_conn, tenant) do
query =
"select * from pg_catalog.pg_tables where schemaname = 'realtime' and tablename = 'schema_migrations';"
# Logger.metadata(external_id: tenant.external_id, project: tenant.external_id)

%{extensions: [%{settings: settings} | _]} = tenant
# check_migrations_exist_query =
# "select * from information_schema.tables where table_schema = 'realtime' and table_name = 'schema_migrations'"

{:ok, %{num_rows: num_rows}} =
Database.transaction(db_conn, fn db_conn -> Postgrex.query!(db_conn, query, []) end)
# check_number_migrations_query = "select count(version) from realtime.schema_migrations"

if num_rows < @expected_migration_count do
run_migrations(%__MODULE__{tenant_external_id: tenant.external_id, settings: settings})
end
# with {:ok, %Postgrex.Result{num_rows: 1}} <-
# Database.transaction(db_conn, fn db_conn ->
# Postgrex.query!(db_conn, check_migrations_exist_query, [])
# end),
# {:ok, %Postgrex.Result{rows: [[count]]}} <-
# Database.transaction(db_conn, fn db_conn ->
# Postgrex.query!(db_conn, check_number_migrations_query, [])
# end) do
# if count < @expected_migration_count do
# Logger.error("Running missing migrations")
# run_migrations(%__MODULE__{tenant_external_id: tenant.external_id, settings: settings})
# end

:ok
# :ok
# else
# {:ok, %{num_rows: 0}} ->
# Logger.error("Running migrations")
# run_migrations(%__MODULE__{tenant_external_id: tenant.external_id, settings: settings})

# {:error, error} ->
# log_error("MigrationCheckFailed", error)
# {:error, :migration_check_failed}
# end
%{extensions: [%{settings: settings} | _]} = tenant
run_migrations(%__MODULE__{tenant_external_id: tenant.external_id, settings: settings})
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.33.46",
version: "2.33.47",
elixir: "~> 1.16.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
2 changes: 1 addition & 1 deletion test/realtime/tenants/connect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ defmodule Realtime.Tenants.ConnectTest do

assert {:ok, db_conn} = Connect.lookup_or_start_connection(tenant.external_id)
Sandbox.allow(Repo, self(), db_conn)
:timer.sleep(100)
:timer.sleep(500)
assert Process.alive?(db_conn)
assert {:ok, db_conn} = Connect.lookup_or_start_connection(tenant.external_id)
assert Process.alive?(db_conn)
Expand Down
2 changes: 1 addition & 1 deletion test/realtime_web/controllers/tenant_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ defmodule RealtimeWeb.TenantControllerTest do

conn = get(conn, Routes.tenant_path(conn, :health, ext_id))
data = json_response(conn, 200)["data"]

:timer.sleep(1000)
assert {:ok, %{rows: []}} = Postgrex.query(db_conn, "SELECT * FROM realtime.messages", [])

assert %{"healthy" => true, "db_connected" => true, "connected_cluster" => 0} = data
Expand Down
Loading