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
3 changes: 2 additions & 1 deletion apps/cli-go/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ var (
return err
}
}
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, queryTimeout, afero.NewOsFs())
useDirectDBURL := cmd.Flags().Changed("db-url")
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, queryTimeout, afero.NewOsFs(), useDirectDBURL)
},
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, fsys afero.Fs, useDirectDBURL bool, 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 !useDirectDBURL && utils.IsLocalDatabase(dbConfig) {
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}
Expand Down
37 changes: 28 additions & 9 deletions apps/cli-go/internal/gen/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestGenLocalCommand(t *testing.T) {
Password: "password",
}

t.Run("generates typescript types", func(t *testing.T) {
t.Run("generates typescript types", func(t *testing.T) {
const containerId = "test-pgmeta"
imageUrl := utils.GetRegistryImageUrl(utils.Config.Studio.PgmetaImage)
// Setup in-memory fs
Expand All @@ -49,11 +49,30 @@ 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, fsys, false, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("skips local db check when using direct db url", 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, fsys, true, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("throws error when db is not started", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
Expand All @@ -64,7 +83,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, fsys, false))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -84,7 +103,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, fsys, false))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -107,7 +126,7 @@ 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, fsys, false, 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, fsys, false))
// 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, fsys, false)
// 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, fsys, false))
})
}

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, afero.NewMemMapFs(), false, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand Down