From 045334bedde1a7924bef69646775c7e90f0740ba Mon Sep 17 00:00:00 2001 From: avallete Date: Thu, 22 May 2025 16:25:32 +0200 Subject: [PATCH] feat(typegen): add postgrest-version params Should be merged once: https://github.com/supabase/postgres-meta/pull/948/files is merged --- cmd/gen.go | 7 ++++++- internal/gen/types/types.go | 6 +++++- internal/gen/types/types_test.go | 16 ++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cmd/gen.go b/cmd/gen.go index 5849a5ce0..771dd688d 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -55,6 +55,7 @@ var ( Value: types.LangTypescript, } postgrestV9Compat bool + postgrestVersion string swiftAccessControl = utils.EnumFlag{ Allowed: []string{ types.SwiftInternalAccessControl, @@ -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") @@ -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 @@ -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.") diff --git a/internal/gen/types/types.go b/internal/gen/types/types.go index 9ffc7dc72..b4cb2438c 100644 --- a/internal/gen/types/types.go +++ b/internal/gen/types/types.go @@ -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 { @@ -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 } @@ -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"}, }, diff --git a/internal/gen/types/types_test.go b/internal/gen/types/types_test.go index bf7ee4067..ee703ee0e 100644 --- a/internal/gen/types/types_test.go +++ b/internal/gen/types/types_test.go @@ -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()) }) @@ -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()) }) @@ -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()) }) @@ -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()) }) @@ -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()) }) @@ -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()) @@ -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)) }) } @@ -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()) })