Skip to content

Commit

Permalink
remove maybe_migrate for now
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed Nov 14, 2024
1 parent 8e75e9a commit 2ad8f24
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 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,45 +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
Logger.metadata(external_id: tenant.external_id, project: tenant.external_id)
# Logger.metadata(external_id: tenant.external_id, project: tenant.external_id)

check_migrations_exist_query =
"select * from information_schema.tables where table_schema = 'realtime' and table_name = 'schema_migrations'"
# check_migrations_exist_query =
# "select * from information_schema.tables where table_schema = 'realtime' and table_name = 'schema_migrations'"

check_number_migrations_query = "select count(version) from realtime.schema_migrations"
# check_number_migrations_query = "select count(version) from realtime.schema_migrations"

%{extensions: [%{settings: settings} | _]} = tenant
# 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

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

: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
# {: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

0 comments on commit 2ad8f24

Please sign in to comment.