8000 Fixing HealthCheck for Supabase by shifluxxc · Pull Request #1848 · gofr-dev/gofr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
< 8000 diff-file-filter>

Fixing HealthCheck for Supabase #1848

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

Merged
Merged
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
39 changes: 27 additions & 12 deletions pkg/gofr/datasource/sql/sql.go
10000
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,43 @@ type DBConfig struct {
Charset string
}

func setupSupabaseDefaults(dbConfig *DBConfig, configs config.Config, logger datasource.Logger) {
if dbConfig.HostName == "" {
projectRef := configs.Get("SUPABASE_PROJECT_REF")
if projectRef != "" {
dbConfig.HostName = fmt.Sprintf("db.%s.supabase.co", projectRef)
}
}

if dbConfig.Database == "" {
dbConfig.Database = dialectPostgres
}

if dbConfig.SSLMode != requireSSLMode {
logger.Warnf("Supabase connections require SSL. Setting DB_SSL_MODE to 'require'")

dbConfig.SSLMode = requireSSLMode // Enforce SSL mode for Supabase
}

if dbConfig.Port == strconv.Itoa(defaultDBPort) {
dbConfig.Port = "5432"
}
}

func NewSQL(configs config.Config, logger datasource.Logger, metrics Metrics) *DB {
dbConfig := getDBConfig(configs)

if dbConfig.Dialect == "" {
return nil
if dbConfig.Dialect == supabaseDialect {
setupSupabaseDefaults(dbConfig, configs, logger)
}

// if Hostname is not provided, we won't try to connect to DB
if dbConfig.Dialect != sqlite && dbConfig.HostName == "" {
logger.Errorf("connection to %s failed: host name is empty.", dbConfig.Dialect)
return nil
}

// For Supabase, enforce SSL mode as "require"
if dbConfig.Dialect == supabaseDialect && dbConfig.SSLMode != requireSSLMode {
logger.Warnf("Supabase connections require SSL. Setting DB_SSL_MODE to 'require'")

dbConfig.SSLMode = requireSSLMode

if dbConfig.Port == strconv.Itoa(defaultDBPort) {
dbConfig.Port = "5432"
}
if dbConfig.Dialect == "" {
return nil
}

logger.Debugf("generating database connection string for '%s'", dbConfig.Dialect)
Expand Down
6 changes: 2 additions & 4 deletions pkg/gofr/datasource/sql/supabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetSupabaseConfig(configs config.Config) *SupabaseConfig {
return nil
}

dbConfig.SSLMode = configs.GetOrDefault("DB_SSL_MODE", "require")
dbConfig.SSLMode = requireSSLMode // Enforce SSL mode for Supabase

connectionType := strings.ToLower(configs.GetOrDefault("SUPABASE_CONNECTION_TYPE", "direct"))
projectRef := configs.Get("SUPABASE_PROJECT_REF")
Expand All @@ -45,9 +45,7 @@ func GetSupabaseConfig(configs config.Config) *SupabaseConfig {
// If a direct connection string is provided, we'll use that instead
connStr := configs.Get("DB_URL")
if connStr != "" {
if projectRef == "" {
projectRef = extractProjectRefFromConnStr(connStr)
}
projectRef = extractProjectRefFromConnStr(connStr)
}

return &SupabaseConfig{
Expand Down
6 changes: 3 additions & 3 deletions pkg/gofr/datasource/sql/supabase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func TestConfigureSupabaseConnection(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockLogger := logging.NewMockLogger(logging.DEBUG)

logs := testutil.StdoutOutputForFunc(func() {
mockLogger := logging.NewMockLogger(logging.DEBUG)

configureSupabaseConnection(tc.config, mockLogger)
})

Expand Down Expand Up @@ -495,13 +495,13 @@ func TestNewSupabaseSQL(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockConfig := config.NewMockConfig(tc.configs)
mockLogger := logging.NewMockLogger(logging.DEBUG)
mockMetrics := NewMockMetrics(ctrl)

// We expect metrics to be set regardless of the result
mockMetrics.EXPECT().SetGauge(gomock.Any(), gomock.Any()).AnyTimes()

logs := testutil.StdoutOutputForFunc(func() {
mockLogger := logging.NewMockLogger(logging.DEBUG)
result := NewSupabaseSQL(mockConfig, mockLogger, mockMetrics)

if tc.expectNil {
Expand Down
Loading
0