-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathtypes.go
More file actions
121 lines (106 loc) · 3.54 KB
/
Copy pathtypes.go
File metadata and controls
121 lines (106 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package types
import (
"context"
"fmt"
"os"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
)
const (
LangTypescript = "typescript"
LangGo = "go"
LangSwift = "swift"
)
const (
SwiftPublicAccessControl = "public"
SwiftInternalAccessControl = "internal"
)
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 {
schemas = utils.RemoveDuplicates(append([]string{"public"}, utils.Config.Api.Schemas...))
}
included := strings.Join(schemas, ",")
if projectId != "" {
if lang != LangTypescript {
return errors.Errorf("Unable to generate %s types for selected project. Try using --db-url flag instead.", lang)
}
resp, err := utils.GetSupabase().V1GenerateTypescriptTypesWithResponse(ctx, projectId, &api.V1GenerateTypescriptTypesParams{
IncludedSchemas: &included,
})
if err != nil {
return errors.Errorf("failed to get typescript types: %w", err)
}
if resp.JSON200 == nil {
return errors.New("failed to retrieve generated types: " + string(resp.Body))
}
fmt.Print(resp.JSON200.Types)
return nil
}
hostConfig := container.HostConfig{}
if utils.IsLocalDatabase(dbConfig) {
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}
// Extract version from image tag and trim 'v' prefix
postgrestVersion = strings.TrimPrefix(utils.Config.Api.Image, "postgrest/postgrest:v")
if strings.Contains(utils.Config.Api.Image, "v9") {
postgrestV9Compat = true
}
// Use custom network when connecting to local database
dbConfig.Host = utils.DbAliases[0]
dbConfig.Port = 5432
} else {
hostConfig.NetworkMode = network.NetworkHost
}
// pg-meta does not set username as the default database, ie. postgres
if len(dbConfig.Database) == 0 {
dbConfig.Database = "postgres"
}
fmt.Fprintln(os.Stderr, "Connecting to", dbConfig.Host, dbConfig.Port)
escaped := utils.ToPostgresURL(dbConfig)
if require, err := isRequireSSL(ctx, originalURL, options...); err != nil {
return err
} else if require {
// node-postgres does not support sslmode=prefer
escaped += "&sslmode=require"
}
return utils.DockerRunOnceWithConfig(
ctx,
container.Config{
Image: utils.Config.Studio.PgmetaImage,
Env: []string{
"PG_META_DB_URL=" + escaped,
"PG_META_GENERATE_TYPES=" + lang,
"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"},
},
hostConfig,
network.NetworkingConfig{},
"",
os.Stdout,
os.Stderr,
)
}
func isRequireSSL(ctx context.Context, dbUrl string, options ...func(*pgx.ConnConfig)) (bool, error) {
conn, err := utils.ConnectByUrl(ctx, dbUrl+"&sslmode=require", options...)
if err != nil {
if strings.HasSuffix(err.Error(), "(server refused TLS connection)") {
return false, nil
}
return false, err
}
return true, conn.Close(ctx)
}