Skip to content
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
7 changes: 5 additions & 2 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"errors"
"os"
"os/signal"

env "github.com/Netflix/go-env"
"github.com/spf13/afero"
Expand All @@ -14,7 +16,7 @@ import (

var (
genCmd = &cobra.Command{
GroupID: groupManagementAPI,
GroupID: groupLocalDev,
Use: "gen",
Short: "Run code generation tools",
}
Expand All @@ -41,6 +43,7 @@ var (
if err := env.Unmarshal(es, &keyNames); err != nil {
return err
}
cmd.GroupID = groupManagementAPI
return cmd.Root().PersistentPreRunE(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -65,7 +68,7 @@ var (
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
if flags.DbConfig.Host == "" {
// If no flag is specified, prompt for project id.
if err := flags.ParseProjectRef(ctx, afero.NewMemMapFs()); errors.Is(err, utils.ErrNotLinked) {
Expand Down
27 changes: 20 additions & 7 deletions internal/gen/types/typescript/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"github.com/docker/docker/api/types/network"
"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
)

func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas []string, postgrestV9Compat bool, fsys afero.Fs) error {
func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas []string, postgrestV9Compat bool, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
// Add default schemas if --schema flag is not specified
if len(schemas) == 0 {
schemas = utils.RemoveDuplicates(append([]string{"public"}, utils.Config.Api.Schemas...))
Expand Down Expand Up @@ -46,19 +47,20 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
if strings.Contains(utils.Config.Api.Image, "v9") {
postgrestV9Compat = true
}
} else {
// Additional configs for pg-meta with enforce ssl
if dbConfig.RuntimeParams == nil {
dbConfig.RuntimeParams = make(map[string]string, 1)
}
dbConfig.RuntimeParams["sslmode"] = "prefer"
}

fmt.Fprintln(os.Stderr, "Connecting to", dbConfig.Host, dbConfig.Port)
if len(dbConfig.Database) == 0 {
dbConfig.Database = "postgres"
}
escaped := utils.ToPostgresURL(dbConfig)
if require, err := isRequireSSL(ctx, escaped, options...); err != nil {
return err
} else if require {
// node-postgres does not support sslmode=prefer
escaped += "&sslmode=require"
}

return utils.DockerRunOnceWithConfig(
ctx,
container.Config{
Expand All @@ -80,3 +82,14 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
os.Stderr,
)
}

func isRequireSSL(ctx context.Context, dbUrl string, options ...func(*pgx.ConnConfig)) (bool, error) {
conn, err := utils.ConnectByUrl(ctx, dbUrl+"&sslmode=require", options...)
if err != nil {
if strings.HasSuffix(err.Error(), "(server refused TLS connection)") {
return false, nil
}
return false, err
}
return true, conn.Close(ctx)
}
11 changes: 9 additions & 2 deletions internal/gen/types/typescript/typescript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/internal/testing/pgtest"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
"gopkg.in/h2non/gock.v1"
Expand Down Expand Up @@ -43,8 +44,11 @@ func TestGenLocalCommand(t *testing.T) {
JSON(types.ContainerJSON{})
apitest.MockDockerStart(utils.Docker, imageUrl, containerId)
require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "hello world"))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
// Run test
assert.NoError(t, Run(context.Background(), "", dbConfig, []string{}, true, fsys))
assert.NoError(t, Run(context.Background(), "", dbConfig, []string{}, true, fsys, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand Down Expand Up @@ -153,8 +157,11 @@ func TestGenRemoteCommand(t *testing.T) {
defer gock.OffAll()
apitest.MockDockerStart(utils.Docker, imageUrl, containerId)
require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "hello world"))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
// Run test
assert.NoError(t, Run(context.Background(), "", dbConfig, []string{"public"}, true, afero.NewMemMapFs()))
assert.NoError(t, Run(context.Background(), "", dbConfig, []string{"public"}, true, afero.NewMemMapFs(), conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
PostgrestImage = "postgrest/postgrest:v12.0.1"
DifferImage = "supabase/pgadmin-schema-diff:cli-0.0.5"
MigraImage = "supabase/migra:3.0.1663481299"
PgmetaImage = "supabase/postgres-meta:v0.78.2"
PgmetaImage = "supabase/postgres-meta:v0.79.0"
StudioImage = "supabase/studio:20240205-b145c86"
ImageProxyImage = "darthsim/imgproxy:v3.8.0"
EdgeRuntimeImage = "supabase/edge-runtime:v1.36.7"
Expand Down