• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 15540005669

09 Jun 2025 04:58PM UTC coverage: 55.168% (+0.1%) from 55.066%
15540005669

Pull #3243

github

web-flow
Merge c0d923396 into 7280a4611
Pull Request #3243: fix: automatically set default schema env var for type generation

44 of 48 new or added lines in 3 files covered. (91.67%)

5 existing lines in 1 file now uncovered.

6005 of 10885 relevant lines covered (55.17%)

6.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cmd/gen.go
1
package cmd
2

3
import (
4
        "os"
5
        "os/signal"
6

7
        env "github.com/Netflix/go-env"
8
        "github.com/go-errors/errors"
9
        "github.com/spf13/afero"
10
        "github.com/spf13/cobra"
11
        "github.com/supabase/cli/internal/gen/keys"
12
        "github.com/supabase/cli/internal/gen/types"
13
        "github.com/supabase/cli/internal/utils"
14
        "github.com/supabase/cli/internal/utils/flags"
15
)
16

17
var (
18
        genCmd = &cobra.Command{
19
                GroupID: groupLocalDev,
20
                Use:     "gen",
21
                Short:   "Run code generation tools",
22
        }
23

24
        keyNames keys.CustomName
25

26
        genKeysCmd = &cobra.Command{
27
                Use:   "keys",
28
                Short: "Generate keys for preview branch",
29
                PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
×
30
                        es, err := env.EnvironToEnvSet(override)
×
31
                        if err != nil {
×
32
                                return err
×
33
                        }
×
34
                        if err := env.Unmarshal(es, &keyNames); err != nil {
×
35
                                return err
×
36
                        }
×
37
                        cmd.GroupID = groupManagementAPI
×
38
                        return cmd.Root().PersistentPreRunE(cmd, args)
×
39
                },
40
                RunE: func(cmd *cobra.Command, args []string) error {
×
41
                        format := utils.OutputFormat.Value
×
42
                        if format == utils.OutputPretty {
×
43
                                format = utils.OutputEnv
×
44
                        }
×
45
                        return keys.Run(cmd.Context(), flags.ProjectRef, format, keyNames, afero.NewOsFs())
×
46
                },
47
        }
48

49
        lang = utils.EnumFlag{
50
                Allowed: []string{
51
                        types.LangTypescript,
52
                        types.LangGo,
53
                        types.LangSwift,
54
                },
55
                Value: types.LangTypescript,
56
        }
57
        postgrestV9Compat  bool
58
        swiftAccessControl = utils.EnumFlag{
59
                Allowed: []string{
60
                        types.SwiftInternalAccessControl,
61
                        types.SwiftPublicAccessControl,
62
                },
63
                Value: types.SwiftInternalAccessControl,
64
        }
65

66
        genTypesCmd = &cobra.Command{
67
                Use:   "types",
68
                Short: "Generate types from Postgres schema",
69
                PreRunE: func(cmd *cobra.Command, args []string) error {
×
70
                        if postgrestV9Compat && !cmd.Flags().Changed("db-url") {
×
71
                                return errors.New("--postgrest-v9-compat must used together with --db-url")
×
72
                        }
×
73
                        // Legacy commands specify language using arg, eg. gen types typescript
74
                        if len(args) > 0 && args[0] != types.LangTypescript && !cmd.Flags().Changed("lang") {
×
75
                                return errors.New("use --lang flag to specify the typegen language")
×
76
                        }
×
77
                        return nil
×
78
                },
79
                RunE: func(cmd *cobra.Command, args []string) error {
×
80
                        ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
×
81
                        if flags.DbConfig.Host == "" {
×
82
                                // If no flag is specified, prompt for project id.
×
83
                                if err := flags.ParseProjectRef(ctx, afero.NewMemMapFs()); errors.Is(err, utils.ErrNotLinked) {
×
84
                                        return errors.New("Must specify one of --local, --linked, --project-id, or --db-url")
×
85
                                } else if err != nil {
×
86
                                        return err
×
87
                                }
×
88
                        }
NEW
89
                        return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, setDefault, postgrestV9Compat, swiftAccessControl.Value, afero.NewOsFs())
×
90
                },
91
                Example: `  supabase gen types --local
92
  supabase gen types --linked --lang=go
93
  supabase gen types --project-id abc-def-123 --schema public --schema private
94
  supabase gen types --db-url 'postgresql://...' --schema public --schema auth`,
95
        }
96

97
        setDefault bool
98
)
99

100
func init() {
×
101
        typeFlags := genTypesCmd.Flags()
×
102
        typeFlags.Bool("local", false, "Generate types from the local dev database.")
×
103
        typeFlags.Bool("linked", false, "Generate types from the linked project.")
×
104
        typeFlags.String("db-url", "", "Generate types from a database url.")
×
105
        typeFlags.StringVar(&flags.ProjectRef, "project-id", "", "Generate types from a project ID.")
×
106
        genTypesCmd.MarkFlagsMutuallyExclusive("local", "linked", "project-id", "db-url")
×
107
        typeFlags.Var(&lang, "lang", "Output language of the generated types.")
×
108
        typeFlags.StringSliceVarP(&schema, "schema", "s", []string{}, "Comma separated list of schema to include.")
×
109
        typeFlags.Var(&swiftAccessControl, "swift-access-control", "Access control for Swift generated types.")
×
110
        typeFlags.BoolVar(&postgrestV9Compat, "postgrest-v9-compat", false, "Generate types compatible with PostgREST v9 and below. Only use together with --db-url.")
×
NEW
111
        typeFlags.BoolVar(&setDefault, "set-default", false, "Set the specified schema as the default for helper types when using a single non-public schema")
×
112
        genCmd.AddCommand(genTypesCmd)
×
113
        keyFlags := genKeysCmd.Flags()
×
114
        keyFlags.StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
×
115
        keyFlags.StringSliceVar(&override, "override-name", []string{}, "Override specific variable names.")
×
116
        genCmd.AddCommand(genKeysCmd)
×
117
        rootCmd.AddCommand(genCmd)
×
118
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc