Skip to content
Open
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
4 changes: 3 additions & 1 deletion lib/mix/tasks/realtime.export_tenant_db_baseline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ defmodule Mix.Tasks.Realtime.ExportTenantDbBaseline do
pgdelta = pgdelta_bin!(opts[:pgdelta_path])
Mix.shell().info("[export_tenant_db_baseline] pgdelta: #{pgdelta}")

pgdelta_filter = RealtimeWeb.Dashboard.TenantMigrations.pgdelta_filter()

output = Path.expand(@baseline_path, File.cwd!())
args = ["catalog-export", "--target", url, "--output", output]
args = ["catalog-export", "--target", url, "--output", output, "--filter", pgdelta_filter]

case System.cmd(pgdelta, args, stderr_to_stdout: true) do
{output_str, 0} ->
Expand Down
35 changes: 20 additions & 15 deletions lib/realtime_web/dashboard/tenant_migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
alias Realtime.Api.Tenant
alias Realtime.Database

@pg_delta_filter ~s"""
@pgdelta_filter ~s"""
{
"and": [
{"*/schema": "realtime"},
Expand All @@ -36,7 +36,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
external_id: "",
tenant: nil,
schema_migrations: nil,
pg_delta: nil,
pgdelta: nil,
error: nil
)}
end
Expand All @@ -53,7 +53,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
external_id: ref,
tenant: tenant,
schema_migrations: fetch_schema_migrations(db_conn),
pg_delta: run_pg_delta(settings),
pgdelta: run_pgdelta(settings),
error: nil
)}
else
Expand All @@ -72,7 +72,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
external_id: "",
tenant: nil,
schema_migrations: nil,
pg_delta: nil,
pgdelta: nil,
error: nil
)}
end
Expand All @@ -91,11 +91,11 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
assigns: %{
tenant: %Tenant{} = tenant,
external_id: ref,
pg_delta: {:ok, %{status: :changes, sql: sql}}
pgdelta: {:ok, %{status: :changes, sql: sql}}
}
} = socket
) do
case apply_pg_delta(tenant, sql) do
case apply_pgdelta(tenant, sql) do
:ok ->
{:noreply, push_patch(socket, to: "/admin/dashboard/tenant_migrations?external_id=#{URI.encode(ref)}")}

Expand Down Expand Up @@ -135,7 +135,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
<%= schema_migrations(@schema_migrations) %>

<h6 class="mt-4">pg-delta plan vs baseline</h6>
<%= pg_delta_plan(@pg_delta) %>
<%= pgdelta_plan(@pgdelta) %>
<% end %>
</div>
"""
Expand Down Expand Up @@ -188,12 +188,12 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
"""
end

defp pg_delta_plan(nil) do
defp pgdelta_plan(nil) do
assigns = %{}
~H""
end

defp pg_delta_plan({:error, msg}) do
defp pgdelta_plan({:error, msg}) do
assigns = %{msg: msg}

~H"""
Expand All @@ -204,7 +204,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
"""
end

defp pg_delta_plan({:ok, %{status: :no_changes}}) do
defp pgdelta_plan({:ok, %{status: :no_changes}}) do
assigns = %{}

~H"""
Expand All @@ -214,7 +214,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
"""
end

defp pg_delta_plan({:ok, %{status: :changes, sql: sql}}) do
defp pgdelta_plan({:ok, %{status: :changes, sql: sql}}) do
assigns = %{sql: sql}

~H"""
Expand Down Expand Up @@ -261,7 +261,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
external_id: ref,
tenant: nil,
schema_migrations: nil,
pg_delta: nil,
pgdelta: nil,
error: msg
)
end
Expand Down Expand Up @@ -306,7 +306,12 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
])
end

defp run_pg_delta(%Database{} = settings) do
@doc """
Filter used to generate baselines and plans.
"""
def pgdelta_filter, do: @pgdelta_filter

defp run_pgdelta(%Database{} = settings) do
case System.find_executable("pgdelta") do
nil ->
log_warning("TenantMigrationsPgDeltaMissing", "pgdelta not found on PATH")
Expand All @@ -322,7 +327,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
"--target",
baseline,
"--filter",
@pg_delta_filter,
pgdelta_filter(),
"--format",
"sql"
]
Expand All @@ -341,7 +346,7 @@ defmodule RealtimeWeb.Dashboard.TenantMigrations do
end
end

defp apply_pg_delta(%Tenant{} = tenant, sql) do
defp apply_pgdelta(%Tenant{} = tenant, sql) do
opts = [query_type: :text, timeout: @query_timeout]

with {:ok, settings} <- Database.from_tenant(tenant, @application_name, :stop),
Expand Down
Loading