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

supabase / cli / 20369862521

19 Dec 2025 12:21PM UTC coverage: 56.046% (-0.2%) from 56.237%
20369862521

Pull #4604

github

web-flow
Merge ddae3156d into 1876c6601
Pull Request #4604: feat: supabase public url

1 of 1 new or added line in 1 file covered. (100.0%)

68 existing lines in 6 files now uncovered.

6813 of 12156 relevant lines covered (56.05%)

6.29 hits per line

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

97.53
/internal/gen/types/types.go
1
package types
2

3
import (
4
        "context"
5
        _ "embed"
6
        "fmt"
7
        "os"
8
        "strings"
9
        "time"
10

11
        "github.com/docker/docker/api/types/container"
12
        "github.com/docker/docker/api/types/network"
13
        "github.com/go-errors/errors"
14
        "github.com/jackc/pgconn"
15
        "github.com/jackc/pgx/v4"
16
        "github.com/spf13/afero"
17
        "github.com/spf13/viper"
18
        "github.com/supabase/cli/internal/utils"
19
        "github.com/supabase/cli/pkg/api"
20
)
21

22
const (
23
        LangTypescript = "typescript"
24
        LangGo         = "go"
25
        LangSwift      = "swift"
26
        LangPython     = "python"
27
)
28

29
const (
30
        SwiftPublicAccessControl   = "public"
31
        SwiftInternalAccessControl = "internal"
32
)
33

34
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 {
8✔
35
        originalURL := utils.ToPostgresURL(dbConfig)
8✔
36
        // Add default schemas if --schema flag is not specified
8✔
37
        if len(schemas) == 0 {
15✔
38
                schemas = utils.RemoveDuplicates(append([]string{"public"}, utils.Config.Api.Schemas...))
7✔
39
        }
7✔
40
        included := strings.Join(schemas, ",")
8✔
41

8✔
42
        if projectId != "" {
11✔
43
                if lang != LangTypescript {
3✔
44
                        return errors.Errorf("Unable to generate %s types for selected project. Try using --db-url flag instead.", lang)
×
UNCOV
45
                }
×
46
                resp, err := utils.GetSupabase().V1GenerateTypescriptTypesWithResponse(ctx, projectId, &api.V1GenerateTypescriptTypesParams{
3✔
47
                        IncludedSchemas: &included,
3✔
48
                })
3✔
49
                if err != nil {
4✔
50
                        return errors.Errorf("failed to get typescript types: %w", err)
1✔
51
                }
1✔
52

53
                if resp.JSON200 == nil {
3✔
54
                        return errors.New("failed to retrieve generated types: " + string(resp.Body))
1✔
55
                }
1✔
56

57
                fmt.Print(resp.JSON200.Types)
1✔
58
                return nil
1✔
59
        }
60

61
        hostConfig := container.HostConfig{}
5✔
62
        if utils.IsLocalDatabase(dbConfig) {
9✔
63
                if err := utils.AssertSupabaseDbIsRunning(); err != nil {
5✔
64
                        return err
1✔
65
                }
1✔
66

67
                if strings.Contains(utils.Config.Api.Image, "v9") {
5✔
68
                        postgrestV9Compat = true
2✔
69
                }
2✔
70

71
                // Use custom network when connecting to local database
72
                dbConfig.Host = utils.DbAliases[0]
3✔
73
                dbConfig.Port = 5432
3✔
74
        } else {
1✔
75
                hostConfig.NetworkMode = network.NetworkHost
1✔
76
        }
1✔
77
        // pg-meta does not set username as the default database, ie. postgres
78
        if len(dbConfig.Database) == 0 {
7✔
79
                dbConfig.Database = "postgres"
3✔
80
        }
3✔
81

82
        fmt.Fprintln(os.Stderr, "Connecting to", dbConfig.Host, dbConfig.Port)
4✔
83
        env := []string{
4✔
84
                "PG_META_DB_URL=" + utils.ToPostgresURL(dbConfig),
4✔
85
                fmt.Sprintf("PG_CONN_TIMEOUT_SECS=%.0f", queryTimeout.Seconds()),
4✔
86
                fmt.Sprintf("PG_QUERY_TIMEOUT_SECS=%.0f", queryTimeout.Seconds()),
4✔
87
                "PG_META_GENERATE_TYPES=" + lang,
4✔
88
                "PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=" + included,
4✔
89
                "PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL=" + swiftAccessControl,
4✔
90
                fmt.Sprintf("PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS=%v", !postgrestV9Compat),
4✔
91
        }
4✔
92
        if ca, err := GetRootCA(ctx, originalURL, options...); err != nil {
5✔
93
                return err
1✔
94
        } else if len(ca) > 0 {
7✔
95
                env = append(env, "PG_META_DB_SSL_ROOT_CERT="+ca)
3✔
96
        }
3✔
97

98
        return utils.DockerRunOnceWithConfig(
3✔
99
                ctx,
3✔
100
                container.Config{
3✔
101
                        Image: utils.Config.Studio.PgmetaImage,
3✔
102
                        Env:   env,
3✔
103
                        Cmd:   []string{"node", "dist/server/server.js"},
3✔
104
                },
3✔
105
                hostConfig,
3✔
106
                network.NetworkingConfig{},
3✔
107
                "",
3✔
108
                os.Stdout,
3✔
109
                os.Stderr,
3✔
110
        )
3✔
111
}
112

113
var (
114
        //go:embed templates/staging-ca-2021.crt
115
        caStaging string
116
        //go:embed templates/prod-ca-2021.crt
117
        caProd string
118
        //go:embed templates/prod-ca-2025.crt
119
        caSnap string
120
)
121

122
func GetRootCA(ctx context.Context, dbURL string, options ...func(*pgx.ConnConfig)) (string, error) {
6✔
123
        // node-postgres does not support sslmode=prefer
6✔
124
        if require, err := isRequireSSL(ctx, dbURL, options...); !require {
9✔
125
                return "", err
3✔
126
        }
3✔
127
        // Merge all certs to support --db-url flag
128
        return caStaging + caProd + caSnap, nil
3✔
129
}
130

131
func isRequireSSL(ctx context.Context, dbUrl string, options ...func(*pgx.ConnConfig)) (bool, error) {
6✔
132
        conn, err := utils.ConnectByUrl(ctx, dbUrl+"&sslmode=require", options...)
6✔
133
        if err != nil {
9✔
134
                if strings.HasSuffix(err.Error(), "(server refused TLS connection)") {
5✔
135
                        return false, nil
2✔
136
                }
2✔
137
                return false, err
1✔
138
        }
139
        // SSL is not supported in debug mode
140
        return !viper.GetBool("DEBUG"), conn.Close(ctx)
3✔
141
}
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