Skip to content

feat(typegen): add postgrest-version params #3602

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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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: 6 additions & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var (
Value: types.LangTypescript,
}
postgrestV9Compat bool
postgrestVersion string
swiftAccessControl = utils.EnumFlag{
Allowed: []string{
types.SwiftInternalAccessControl,
Expand All @@ -70,6 +71,9 @@ var (
if postgrestV9Compat && !cmd.Flags().Changed("db-url") {
return errors.New("--postgrest-v9-compat must used together with --db-url")
}
if postgrestVersion != "" && !cmd.Flags().Changed("db-url") {
return errors.New("--postgrest-version must used together with --db-url")
}
// Legacy commands specify language using arg, eg. gen types typescript
if len(args) > 0 && args[0] != types.LangTypescript && !cmd.Flags().Changed("lang") {
return errors.New("use --lang flag to specify the typegen language")
Expand All @@ -86,7 +90,7 @@ var (
return err
}
}
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, afero.NewOsFs())
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, postgrestVersion, swiftAccessControl.Value, afero.NewOsFs())
},
Example: ` supabase gen types --local
supabase gen types --linked --lang=go
Expand All @@ -106,6 +110,7 @@ func init() {
typeFlags.StringSliceVarP(&schema, "schema", "s", []string{}, "Comma separated list of schema to include.")
typeFlags.Var(&swiftAccessControl, "swift-access-control", "Access control for Swift generated types.")
typeFlags.BoolVar(&postgrestV9Compat, "postgrest-v9-compat", false, "Generate types compatible with PostgREST v9 and below. Only use together with --db-url.")
typeFlags.StringVar(&postgrestVersion, "postgrest-version", "", "Generate types with __internal_supabase schema using the right version of postgrest. Only use together with --db-url.")
genCmd.AddCommand(genTypesCmd)
keyFlags := genKeysCmd.Flags()
keyFlags.StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
Expand Down
6 changes: 5 additions & 1 deletion internal/gen/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
SwiftInternalAccessControl = "internal"
)

func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang string, schemas []string, postgrestV9Compat bool, swiftAccessControl string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang string, schemas []string, postgrestV9Compat bool, postgrestVersion string, swiftAccessControl string, 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 @@ -60,6 +60,9 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang str
return err
}

// Extract version from image tag and trim 'v' prefix
postgrestVersion = strings.TrimPrefix(utils.Config.Api.Image, "v")

if strings.Contains(utils.Config.Api.Image, "v9") {
postgrestV9Compat = true
}
Expand Down Expand Up @@ -94,6 +97,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang str
"PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=" + included,
"PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL=" + swiftAccessControl,
fmt.Sprintf("PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS=%v", !postgrestV9Compat),
fmt.Sprintf("PG_META_POSTGREST_VERSION=%s", postgrestVersion),
},
Cmd: []string{"node", "dist/server/server.js"},
},
Expand Down
16 changes: 8 additions & 8 deletions internal/gen/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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, "", fsys, conn.Intercept))
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", "", fsys, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -63,7 +63,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, "", fsys))
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", "", fsys))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -83,7 +83,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, "", fsys))
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", "", fsys))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -106,7 +106,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, fsys, conn.Intercept))
assert.NoError(t, Run(context.Background(), "", dbConfig, LangSwift, []string{}, true, SwiftInternalAccessControl, "", fsys, conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -129,7 +129,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, "", fsys))
assert.NoError(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", "", fsys))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand All @@ -144,7 +144,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, "", fsys)
err := Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", "", fsys)
// Validate api
assert.ErrorIs(t, err, errNetwork)
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand All @@ -159,7 +159,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, "", fsys))
assert.Error(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", "", fsys))
})
}

Expand All @@ -184,7 +184,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, "", afero.NewMemMapFs(), conn.Intercept))
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{"public"}, true, "", "", afero.NewMemMapFs(), conn.Intercept))
// Validate api
assert.Empty(t, apitest.ListUnmatchedRequests())
})
Expand Down