Skip to content

Commit 5eba693

Browse files
authored
fix: unblock tenants and make migrations safer (#1839)
1 parent bbd626e commit 5eba693

8 files changed

Lines changed: 30 additions & 24 deletions

compose.dbs.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
services:
22
db:
33
image: supabase/postgres:17.6.1.074
4-
container_name: realtime-db
54
ports:
65
- "5432:5432"
76
volumes:
@@ -18,7 +17,6 @@ services:
1817

1918
tenant_db:
2019
image: supabase/postgres:17.6.1.074
21-
container_name: tenant-db
2220
ports:
2321
- "5433:5432"
2422
command: postgres -c config_file=/etc/postgresql/postgresql.conf

lib/realtime/tenants/repo/migrations/20211116045059_create_realtime_check_filters_trigger.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ defmodule Realtime.Tenants.Migrations.CreateRealtimeCheckFiltersTrigger do
5757
end;
5858
$$;")
5959

60+
execute("drop trigger if exists tr_check_filters on realtime.subscription")
61+
6062
execute("create trigger tr_check_filters
6163
before insert or update on realtime.subscription
6264
for each row

lib/realtime/tenants/repo/migrations/20211210212804_enable_generic_subscription_claims.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ defmodule Realtime.Tenants.Migrations.EnableGenericSubscriptionClaims do
77
execute("truncate table realtime.subscription restart identity")
88

99
execute("alter table realtime.subscription
10-
drop constraint subscription_entity_user_id_filters_key cascade,
11-
drop column email cascade,
12-
drop column created_at cascade")
10+
drop constraint if exists subscription_entity_user_id_filters_key cascade,
11+
drop column if exists email cascade,
12+
drop column if exists created_at cascade")
1313

1414
execute("alter table realtime.subscription rename user_id to subscription_id")
1515

@@ -87,7 +87,7 @@ defmodule Realtime.Tenants.Migrations.EnableGenericSubscriptionClaims do
8787

8888
execute("alter type realtime.wal_rls rename attribute users to subscription_ids cascade;")
8989

90-
execute("drop function realtime.apply_rls(jsonb, integer);")
90+
execute("drop function if exists realtime.apply_rls(jsonb, integer);")
9191
execute("create or replace function realtime.apply_rls(wal jsonb, max_record_bytes int = 1024 * 1024)
9292
returns setof realtime.wal_rls
9393
language plpgsql

lib/realtime/tenants/repo/migrations/20240523004032_redefine_authorization_tables.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ defmodule Realtime.Tenants.Migrations.RedefineAuthorizationTables do
44
use Ecto.Migration
55

66
def change do
7-
drop table(:broadcasts), mode: :cascade
8-
drop table(:presences), mode: :cascade
9-
drop table(:channels), mode: :cascade
7+
drop_if_exists table(:broadcasts), mode: :cascade
8+
drop_if_exists table(:presences), mode: :cascade
9+
drop_if_exists table(:channels), mode: :cascade
1010

1111
create_if_not_exists table(:messages) do
1212
add :topic, :text, null: false
@@ -31,7 +31,7 @@ defmodule Realtime.Tenants.Migrations.RedefineAuthorizationTables do
3131
execute("ALTER table realtime.messages OWNER to supabase_realtime_admin")
3232

3333
execute("""
34-
DROP function realtime.channel_name
34+
DROP function IF EXISTS realtime.channel_name
3535
""")
3636

3737
execute("""

lib/realtime/tenants/repo/migrations/20241108114728_messages_using_uuid.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ defmodule Realtime.Tenants.Migrations.MessagesUsingUuid do
1010
end
1111

1212
execute("ALTER TABLE realtime.messages ADD PRIMARY KEY (id, inserted_at)")
13-
execute("DROP SEQUENCE realtime.messages_id_seq")
13+
execute("DROP SEQUENCE IF EXISTS realtime.messages_id_seq")
1414
end
1515
end

lib/realtime/tenants/repo/migrations/20251120212548_add_action_to_subscriptions.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Realtime.Tenants.Migrations.AddActionToSubscriptions do
1919

2020
def down do
2121
execute("""
22-
ALTER TABLE realtime.subscription DROP COLUMN action_filter;
22+
ALTER TABLE realtime.subscription DROP COLUMN IF EXISTS action_filter;
2323
""")
2424

2525
execute("""

lib/realtime_web/dashboard/sql_inspector.ex

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ defmodule RealtimeWeb.Dashboard.SqlInspector do
99
"""
1010
use Phoenix.LiveDashboard.PageBuilder
1111

12+
require Logger
13+
1214
alias Realtime.Repo.Replica
1315

14-
@query_timeout 10_000
16+
@query_timeout 30_000
1517
@max_rows 1_000
1618

1719
@sensitive_patterns ~w(password passwd secret token jwt key credential private salt hash)
@@ -52,6 +54,7 @@ defmodule RealtimeWeb.Dashboard.SqlInspector do
5254
)}
5355

5456
{:error, msg} ->
57+
Logger.warning("SqlInspector query error: #{msg}")
5558
{:noreply, assign(socket, error: msg, result: nil, sql: sql, display_rows: [], row_count: 0)}
5659
end
5760
end
@@ -74,7 +77,7 @@ defmodule RealtimeWeb.Dashboard.SqlInspector do
7477
@impl true
7578
def render(assigns) do
7679
~H"""
77-
<div style="background: #ffffff; border: 1px solid #dee2e6; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); padding: 20px; margin-bottom: 16px;">
80+
<div style="background: #ffffff; border: 1px solid #dee2e6; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); padding: 20px; margin-bottom: 16px; min-width: 0;">
7881
<h5 style="margin: 0 0 4px 0; font-weight: 600;">SQL Inspector</h5>
7982
<p style="color: #6c757d; font-size: 0.85rem; margin-bottom: 16px;">
8083
Read-only SELECT queries only. Sensitive column values are masked. Results capped at <%= @max_rows %> rows.
@@ -161,15 +164,18 @@ defmodule RealtimeWeb.Dashboard.SqlInspector do
161164
limited_sql = "SELECT * FROM (#{stripped}) AS _q LIMIT #{@max_rows}"
162165
repo = Replica.replica()
163166

164-
repo.transaction(fn ->
165-
Ecto.Adapters.SQL.query!(repo, "SET TRANSACTION READ ONLY", [])
166-
167-
case Ecto.Adapters.SQL.query(repo, limited_sql, [], timeout: @query_timeout) do
168-
{:ok, result} -> repo.rollback({:ok, mask_sensitive_columns(result)})
169-
{:error, %{postgres: %{message: message}}} -> repo.rollback({:error, message})
170-
{:error, reason} -> repo.rollback({:error, inspect(reason)})
171-
end
172-
end)
167+
repo.transaction(
168+
fn ->
169+
Ecto.Adapters.SQL.query!(repo, "SET TRANSACTION READ ONLY", [])
170+
171+
case Ecto.Adapters.SQL.query(repo, limited_sql, [], timeout: @query_timeout) do
172+
{:ok, result} -> repo.rollback({:ok, mask_sensitive_columns(result)})
173+
{:error, %{postgres: %{message: message}}} -> repo.rollback({:error, message})
174+
{:error, reason} -> repo.rollback({:error, inspect(reason)})
175+
end
176+
end,
177+
timeout: @query_timeout
178+
)
173179
|> case do
174180
{:error, {:ok, result}} -> {:ok, result}
175181
{:error, {:error, message}} -> {:error, message}

mise.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)