diff --git a/README.md b/README.md index 01d16ec71..589660c04 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/lib/realtime/tenants/migrations.ex b/lib/realtime/tenants/migrations.ex index 315eab22d..22f4e324d 100644 --- a/lib/realtime/tenants/migrations.ex +++ b/lib/realtime/tenants/migrations.ex @@ -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 @@ -197,6 +195,8 @@ 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. @@ -204,19 +204,38 @@ defmodule Realtime.Tenants.Migrations do """ @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 """ diff --git a/mix.exs b/mix.exs index a083a3170..ef7238970 100644 --- a/mix.exs +++ b/mix.exs @@ -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, diff --git a/test/realtime/tenants/connect_test.exs b/test/realtime/tenants/connect_test.exs index fd3578099..16ad2fd55 100644 --- a/test/realtime/tenants/connect_test.exs +++ b/test/realtime/tenants/connect_test.exs @@ -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) diff --git a/test/realtime_web/controllers/tenant_controller_test.exs b/test/realtime_web/controllers/tenant_controller_test.exs index 3074c27af..aaef6ce6d 100644 --- a/test/realtime_web/controllers/tenant_controller_test.exs +++ b/test/realtime_web/controllers/tenant_controller_test.exs @@ -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