Skip to content
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
5 changes: 3 additions & 2 deletions apps/cli-go/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ var (
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if flags.DbConfig.Host == "" {
dbURLChanged := cmd.Flags().Changed("db-url")
if flags.DbConfig.Host == "" && !dbURLChanged {
// If no flag is specified, prompt for project id.
if err := flags.ParseProjectRef(ctx, afero.NewMemMapFs()); errors.Is(err, utils.ErrNotLinked) {
return errors.New("Must specify one of --local, --linked, --project-id, or --db-url")
} else if err != nil {
return err
}
}
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, queryTimeout, afero.NewOsFs())
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, queryTimeout, dbURLChanged, afero.NewOsFs())
},
Example: ` supabase gen types --local
supabase gen types --linked --lang=go
Expand Down
4 changes: 2 additions & 2 deletions apps/cli-go/internal/gen/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
SwiftInternalAccessControl = "internal"
)

func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang string, schemas []string, postgrestV9Compat bool, swiftAccessControl string, queryTimeout time.Duration, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang string, schemas []string, postgrestV9Compat bool, swiftAccessControl string, queryTimeout time.Duration, directDbURL bool, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
originalURL := utils.ToPostgresURL(dbConfig)
// Add default schemas if --schema flag is not specified
if len(schemas) == 0 {
Expand Down Expand Up @@ -61,7 +61,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang str
}

hostConfig := container.HostConfig{}
if utils.IsLocalDatabase(dbConfig) {
if utils.IsLocalDatabase(dbConfig) && !directDbURL {
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}
Expand Down
35 changes: 27 additions & 8 deletions apps/cli-go/internal/gen/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestGenLocalCommand(t *testing.T) {
conn := pgtest.NewConn()
defer conn.Close(t)
// Run test
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys, conn.Intercept))
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, false, fsys, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -64,7 +64,7 @@ func TestGenLocalCommand(t *testing.T) {
Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId).
Reply(http.StatusServiceUnavailable)
// Run test
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys))
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, false, fsys))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -84,7 +84,7 @@ func TestGenLocalCommand(t *testing.T) {
Get("/v" + utils.Docker.ClientVersion() + "/images").
Reply(http.StatusServiceUnavailable)
// Run test
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys))
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, false, fsys))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -107,7 +107,26 @@ func TestGenLocalCommand(t *testing.T) {
conn := pgtest.NewConn()
defer conn.Close(t)
// Run test
assert.NoError(t, Run(context.Background(), "", dbConfig, LangSwift, []string{}, true, SwiftInternalAccessControl, time.Second, fsys, conn.Intercept))
assert.NoError(t, Run(context.Background(), "", dbConfig, LangSwift, []string{}, true, SwiftInternalAccessControl, time.Second, false, fsys, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("skips local stack check for explicit db-url on localhost", func(t *testing.T) {
const containerId = "test-pgmeta"
imageUrl := utils.GetRegistryImageUrl(utils.Config.Studio.PgmetaImage)
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
apitest.MockDockerStart(utils.Docker, imageUrl, containerId)
require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "hello world\n"))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
// Run test
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, true, fsys, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -130,7 +149,7 @@ func TestGenLinkedCommand(t *testing.T) {
Reply(200).
JSON(api.TypescriptResponse{Types: ""})
// Run test
assert.NoError(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys))
assert.NoError(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, false, fsys))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -145,7 +164,7 @@ func TestGenLinkedCommand(t *testing.T) {
Get("/v1/projects/" + projectId + "/types/typescript").
ReplyError(errNetwork)
// Run test
err := Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys)
err := Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, false, fsys)
// Validate api
assert.ErrorIs(t, err, errNetwork)
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand All @@ -160,7 +179,7 @@ func TestGenLinkedCommand(t *testing.T) {
Get("/v1/projects/" + projectId + "/types/typescript").
Reply(http.StatusServiceUnavailable)
// Run test
assert.Error(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys))
assert.Error(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, false, fsys))
})
}

Expand All @@ -185,7 +204,7 @@ func TestGenRemoteCommand(t *testing.T) {
conn := pgtest.NewConn()
defer conn.Close(t)
// Run test
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{"public"}, true, "", time.Second, afero.NewMemMapFs(), conn.Intercept))
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{"public"}, true, "", time.Second, false, afero.NewMemMapFs(), conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand Down