Skip to content

Commit 045334b

Browse files
committed
feat(typegen): add postgrest-version params
Should be merged once: https://github.com/supabase/postgres-meta/pull/948/files is merged
1 parent c603179 commit 045334b

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

cmd/gen.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var (
5555
Value: types.LangTypescript,
5656
}
5757
postgrestV9Compat bool
58+
postgrestVersion string
5859
swiftAccessControl = utils.EnumFlag{
5960
Allowed: []string{
6061
types.SwiftInternalAccessControl,
@@ -70,6 +71,9 @@ var (
7071
if postgrestV9Compat && !cmd.Flags().Changed("db-url") {
7172
return errors.New("--postgrest-v9-compat must used together with --db-url")
7273
}
74+
if postgrestVersion != "" && !cmd.Flags().Changed("db-url") {
75+
return errors.New("--postgrest-version must used together with --db-url")
76+
}
7377
// Legacy commands specify language using arg, eg. gen types typescript
7478
if len(args) > 0 && args[0] != types.LangTypescript && !cmd.Flags().Changed("lang") {
7579
return errors.New("use --lang flag to specify the typegen language")
@@ -86,7 +90,7 @@ var (
8690
return err
8791
}
8892
}
89-
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, afero.NewOsFs())
93+
return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, postgrestVersion, swiftAccessControl.Value, afero.NewOsFs())
9094
},
9195
Example: ` supabase gen types --local
9296
supabase gen types --linked --lang=go
@@ -106,6 +110,7 @@ func init() {
106110
typeFlags.StringSliceVarP(&schema, "schema", "s", []string{}, "Comma separated list of schema to include.")
107111
typeFlags.Var(&swiftAccessControl, "swift-access-control", "Access control for Swift generated types.")
108112
typeFlags.BoolVar(&postgrestV9Compat, "postgrest-v9-compat", false, "Generate types compatible with PostgREST v9 and below. Only use together with --db-url.")
113+
typeFlags.StringVar(&postgrestVersion, "postgrest-version", "", "Generate types with __internal_supabase schema using the right version of postgrest. Only use together with --db-url.")
109114
genCmd.AddCommand(genTypesCmd)
110115
keyFlags := genKeysCmd.Flags()
111116
keyFlags.StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")

internal/gen/types/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
SwiftInternalAccessControl = "internal"
2828
)
2929

30-
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 {
30+
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 {
3131
originalURL := utils.ToPostgresURL(dbConfig)
3232
// Add default schemas if --schema flag is not specified
3333
if len(schemas) == 0 {
@@ -60,6 +60,9 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang str
6060
return err
6161
}
6262

63+
// Extract version from image tag and trim 'v' prefix
64+
postgrestVersion = strings.TrimPrefix(utils.Config.Api.Image, "v")
65+
6366
if strings.Contains(utils.Config.Api.Image, "v9") {
6467
postgrestV9Compat = true
6568
}
@@ -94,6 +97,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang str
9497
"PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=" + included,
9598
"PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL=" + swiftAccessControl,
9699
fmt.Sprintf("PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS=%v", !postgrestV9Compat),
100+
fmt.Sprintf("PG_META_POSTGREST_VERSION=%s", postgrestVersion),
97101
},
98102
Cmd: []string{"node", "dist/server/server.js"},
99103
},

internal/gen/types/types_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestGenLocalCommand(t *testing.T) {
4848
conn := pgtest.NewConn()
4949
defer conn.Close(t)
5050
// Run test
51-
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", fsys, conn.Intercept))
51+
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", "", fsys, conn.Intercept))
5252
// Validate api
5353
assert.Empty(t, apitest.ListUnmatchedRequests())
5454
})
@@ -63,7 +63,7 @@ func TestGenLocalCommand(t *testing.T) {
6363
Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId).
6464
Reply(http.StatusServiceUnavailable)
6565
// Run test
66-
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", fsys))
66+
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", "", fsys))
6767
// Validate api
6868
assert.Empty(t, apitest.ListUnmatchedRequests())
6969
})
@@ -83,7 +83,7 @@ func TestGenLocalCommand(t *testing.T) {
8383
Get("/v" + utils.Docker.ClientVersion() + "/images").
8484
Reply(http.StatusServiceUnavailable)
8585
// Run test
86-
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", fsys))
86+
assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", "", fsys))
8787
// Validate api
8888
assert.Empty(t, apitest.ListUnmatchedRequests())
8989
})
@@ -106,7 +106,7 @@ func TestGenLocalCommand(t *testing.T) {
106106
conn := pgtest.NewConn()
107107
defer conn.Close(t)
108108
// Run test
109-
assert.NoError(t, Run(context.Background(), "", dbConfig, LangSwift, []string{}, true, SwiftInternalAccessControl, fsys, conn.Intercept))
109+
assert.NoError(t, Run(context.Background(), "", dbConfig, LangSwift, []string{}, true, SwiftInternalAccessControl, "", fsys, conn.Intercept))
110110
// Validate api
111111
assert.Empty(t, apitest.ListUnmatchedRequests())
112112
})
@@ -129,7 +129,7 @@ func TestGenLinkedCommand(t *testing.T) {
129129
Reply(200).
130130
JSON(api.TypescriptResponse{Types: ""})
131131
// Run test
132-
assert.NoError(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", fsys))
132+
assert.NoError(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", "", fsys))
133133
// Validate api
134134
assert.Empty(t, apitest.ListUnmatchedRequests())
135135
})
@@ -144,7 +144,7 @@ func TestGenLinkedCommand(t *testing.T) {
144144
Get("/v1/projects/" + projectId + "/types/typescript").
145145
ReplyError(errNetwork)
146146
// Run test
147-
err := Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", fsys)
147+
err := Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", "", fsys)
148148
// Validate api
149149
assert.ErrorIs(t, err, errNetwork)
150150
assert.Empty(t, apitest.ListUnmatchedRequests())
@@ -159,7 +159,7 @@ func TestGenLinkedCommand(t *testing.T) {
159159
Get("/v1/projects/" + projectId + "/types/typescript").
160160
Reply(http.StatusServiceUnavailable)
161161
// Run test
162-
assert.Error(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", fsys))
162+
assert.Error(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", "", fsys))
163163
})
164164
}
165165

@@ -184,7 +184,7 @@ func TestGenRemoteCommand(t *testing.T) {
184184
conn := pgtest.NewConn()
185185
defer conn.Close(t)
186186
// Run test
187-
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{"public"}, true, "", afero.NewMemMapFs(), conn.Intercept))
187+
assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{"public"}, true, "", "", afero.NewMemMapFs(), conn.Intercept))
188188
// Validate api
189189
assert.Empty(t, apitest.ListUnmatchedRequests())
190190
})

0 commit comments

Comments
 (0)