From 29b826ffe196b85658a38d3ca85e217e9d71a9cc Mon Sep 17 00:00:00 2001
From: Remi  PLANEL <rplanel@pasteur.fr>
Date: Fri, 11 Apr 2025 14:48:45 +0200
Subject: [PATCH] try fix error database creation if exists

---
 charts/supabase/templates/db/deployment.yaml    |  1 +
 charts/supabase/templates/db/initdb.config.yaml | 13 +++++++++++--
 charts/supabase/templates/vector/config.yaml    |  1 -
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/charts/supabase/templates/db/deployment.yaml b/charts/supabase/templates/db/deployment.yaml
index e75aadf..e62694c 100644
--- a/charts/supabase/templates/db/deployment.yaml
+++ b/charts/supabase/templates/db/deployment.yaml
@@ -46,6 +46,7 @@ spec:
               cp /custom-init-scripts/99-roles.sql /initdb.d/init-scripts/99-roles.sql
               # Initialize the database settings with JWT_SECRET and JWT_EXP
               cp /custom-init-scripts/99-jwt.sql /initdb.d/init-scripts/99-jwt.sql
+              # should not execute this script since it is already in the image
               cp /custom-init-scripts/97-_supabase.sql  /initdb.d/migrations/97-_supabase.sql
               cp /custom-init-scripts/99-logs.sql /initdb.d/migrations/99-logs.sql
               cp /custom-init-scripts/99-pooler.sql /initdb.d/migrations/99-pooler.sql
diff --git a/charts/supabase/templates/db/initdb.config.yaml b/charts/supabase/templates/db/initdb.config.yaml
index ce9b89b..3790209 100644
--- a/charts/supabase/templates/db/initdb.config.yaml
+++ b/charts/supabase/templates/db/initdb.config.yaml
@@ -257,8 +257,17 @@ data:
 
   97-_supabase.sql: |
     \set pguser `echo "$POSTGRES_USER"`
-
-    CREATE DATABASE _supabase WITH OWNER :pguser;
+    DO $$
+    BEGIN
+      IF NOT EXISTS (
+        SELECT FROM pg_database
+        WHERE datname = '_supabase'
+      ) THEN
+        EXECUTE 'CREATE DATABASE _supabase';
+      END IF;
+    END
+    $$;
+    ALTER DATABASE _supabase WITH OWNER :pguser;
 
 
 
diff --git a/charts/supabase/templates/vector/config.yaml b/charts/supabase/templates/vector/config.yaml
index 9103217..df2e897 100644
--- a/charts/supabase/templates/vector/config.yaml
+++ b/charts/supabase/templates/vector/config.yaml
@@ -186,7 +186,6 @@ data:
         method: 'post'
         request:
           retry_max_duration_secs: 10
-        encoding:
         uri: 'http://{{ include "supabase.analytics.fullname" . }}:{{ .Values.analytics.service.port }}/api/logs?source_name=gotrue.logs.prod&api_key=SECRET[credentials.logflare_api_key]'
       logflare_realtime:
         type: 'http'
-- 
GitLab