Skip to content

Create default _realtime.tenant on helm install. #91

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
14 changes: 11 additions & 3 deletions charts/supabase/templates/realtime/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ spec:
- name: DB_HOST
value: {{ include "supabase.db.fullname" . }}
{{- end }}
- name: TENANT_NAME
value: {{ include "supabase.realtime.fullname" . }}
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
Expand Down Expand Up @@ -132,14 +134,20 @@ spec:
resources:
{{- toYaml . | nindent 12 }}
{{- end }}
{{- with .Values.realtime.volumeMounts }}
volumeMounts:
{{- with .Values.realtime.volumeMounts }}
{{- toYaml . | nindent 12 }}
{{- end }}
{{- with .Values.realtime.volumes }}
- name: realtime-seed-volume
mountPath: /app/lib/realtime-{{ .Values.realtime.image.tag | trimPrefix "v" }}/priv/repo/seeds.exs
subPath: seeds.exs
volumes:
{{- with .Values.realtime.volumes }}
{{- toYaml . | nindent 8 }}
{{- end }}
- name: realtime-seed-volume
configMap:
name: {{ printf "%s-seeds" (include "supabase.realtime.fullname" .) }}
{{- with .Values.realtime.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand All @@ -152,4 +160,4 @@ spec:
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- end }}
{{- end }}
66 changes: 66 additions & 0 deletions charts/supabase/templates/realtime/seeds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ printf "%s-seeds" (include "supabase.realtime.fullname" .) }}
data:
seeds.exs: |
require Logger
alias Realtime.{Api.Tenant, Repo}
import Ecto.Adapters.SQL, only: [query: 3]

tenant_name = System.get_env("TENANT_NAME", "realtime-dev")

env = if :ets.whereis(Mix.State) != :undefined, do: Mix.env(), else: :prod
default_db_host = if env in [:dev, :test], do: "localhost", else: "host.docker.internal"

Repo.transaction(fn ->
case Repo.get_by(Tenant, external_id: tenant_name) do
%Tenant{} = tenant -> Repo.delete!(tenant)
nil -> {:ok, nil}
end

%Tenant{}
|> Tenant.changeset(%{
"name" => tenant_name,
"external_id" => tenant_name,
"jwt_secret" =>
System.get_env("API_JWT_SECRET", "super-secret-jwt-token-with-at-least-32-characters-long"),
"jwt_jwks" => System.get_env("API_JWT_JWKS") |> then(fn v -> if v, do: Jason.decode!(v) end),
"extensions" => [
%{
"type" => "postgres_cdc_rls",
"settings" => %{
"db_name" => System.get_env("DB_NAME", "postgres"),
"db_host" => System.get_env("DB_HOST", default_db_host),
"db_user" => System.get_env("DB_USER", "supabase_admin"),
"db_password" => System.get_env("DB_PASSWORD", "postgres"),
"db_port" => System.get_env("DB_PORT", "5433"),
"region" => "us-east-1",
"poll_interval_ms" => 100,
"poll_max_record_bytes" => 1_048_576,
"ssl_enforced" => false
}
}
],
"notify_private_alpha" => true
})
|> Repo.insert!()
end)

if env in [:dev, :test] do
publication = "supabase_realtime"

{:ok, _} =
Repo.transaction(fn ->
[
"drop publication if exists #{publication}",
"drop table if exists public.test_tenant;",
"create table public.test_tenant ( id SERIAL PRIMARY KEY, details text );",
"grant all on table public.test_tenant to anon;",
"grant all on table public.test_tenant to postgres;",
"grant all on table public.test_tenant to authenticated;",
"create publication #{publication} for table public.test_tenant"
]
|> Enum.each(&query(Repo, &1, []))
end)
end