-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathmisc.go
314 lines (283 loc) · 9.14 KB
/
misc.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package utils
import (
"context"
_ "embed"
"net"
"os"
"path/filepath"
"regexp"
"time"
"github.com/docker/docker/client"
"github.com/go-errors/errors"
"github.com/go-git/go-git/v5"
"github.com/spf13/afero"
)
// Assigned using `-ldflags` https://stackoverflow.com/q/11354518
var (
Version string
SentryDsn string
)
const (
Pg13Image = "supabase/postgres:13.3.0"
Pg14Image = "supabase/postgres:14.1.0.89"
Pg15Image = "supabase/postgres:15.1.0.147"
// Append to ServiceImages when adding new dependencies below
KongImage = "library/kong:2.8.1"
InbucketImage = "inbucket/inbucket:3.0.3"
PostgrestImage = "postgrest/postgrest:v12.0.1"
DifferImage = "supabase/pgadmin-schema-diff:cli-0.0.5"
MigraImage = "supabase/migra:3.0.1663481299"
PgmetaImage = "supabase/postgres-meta:v0.77.2"
StudioImage = "supabase/studio:20240205-b145c86"
ImageProxyImage = "darthsim/imgproxy:v3.8.0"
EdgeRuntimeImage = "supabase/edge-runtime:v1.36.1"
VectorImage = "timberio/vector:0.28.1-alpine"
PgbouncerImage = "bitnami/pgbouncer:1.20.1-debian-11-r39"
PgProveImage = "supabase/pg_prove:3.36"
GotrueImage = "supabase/gotrue:v2.132.3"
RealtimeImage = "supabase/realtime:v2.25.50"
StorageImage = "supabase/storage-api:v0.46.4"
LogflareImage = "supabase/logflare:1.4.0"
// Should be kept in-sync with EdgeRuntimeImage
DenoVersion = "1.30.3"
)
var ServiceImages = []string{
GotrueImage,
RealtimeImage,
StorageImage,
ImageProxyImage,
KongImage,
InbucketImage,
PostgrestImage,
DifferImage,
MigraImage,
PgmetaImage,
StudioImage,
EdgeRuntimeImage,
LogflareImage,
VectorImage,
PgbouncerImage,
PgProveImage,
}
func ShortContainerImageName(imageName string) string {
matches := ImageNamePattern.FindStringSubmatch(imageName)
if len(matches) < 2 {
return imageName
}
return matches[1]
}
const (
// https://dba.stackexchange.com/a/11895
// Args: dbname
TerminateDbSqlFmt = `
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '%[1]s';
-- Wait for WAL sender to drop replication slot.
DO 'BEGIN WHILE (
SELECT COUNT(*) FROM pg_replication_slots WHERE database = ''%[1]s''
) > 0 LOOP END LOOP; END';`
SuggestDebugFlag = "Try rerunning the command with --debug to troubleshoot the error."
)
var (
CmdSuggestion string
// pg_dumpall --globals-only --no-role-passwords --dbname $DB_URL \
// | sed '/^CREATE ROLE postgres;/d' \
// | sed '/^ALTER ROLE postgres WITH /d' \
// | sed "/^ALTER ROLE .* WITH .* LOGIN /s/;$/ PASSWORD 'postgres';/"
//go:embed templates/globals.sql
GlobalsSql string
ProjectRefPattern = regexp.MustCompile(`^[a-z]{20}$`)
UUIDPattern = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
ProjectHostPattern = regexp.MustCompile(`^(db\.)([a-z]{20})\.supabase\.(co|red)$`)
MigrateFilePattern = regexp.MustCompile(`^([0-9]+)_(.*)\.sql$`)
BranchNamePattern = regexp.MustCompile(`[[:word:]-]+`)
FuncSlugPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_-]*$`)
ImageNamePattern = regexp.MustCompile(`\/(.*):`)
// These schemas are ignored from db diff and db dump
SystemSchemas = []string{
"information_schema",
"pg_*", // Wildcard pattern follows pg_dump
// Owned by extensions
"cron",
"graphql",
"graphql_public",
"net",
"pgsodium",
"pgsodium_masks",
"pgtle",
"repack",
"tiger",
"tiger_data",
"timescaledb_*",
"_timescaledb_*",
"topology",
"vault",
}
InternalSchemas = append([]string{
"auth",
"extensions",
"pgbouncer",
"realtime",
"_realtime",
"storage",
"_analytics",
"supabase_functions",
"supabase_migrations",
}, SystemSchemas...)
ReservedRoles = []string{
"anon",
"authenticated",
"authenticator",
"dashboard_user",
"pgbouncer",
"postgres",
"service_role",
"supabase_admin",
"supabase_auth_admin",
"supabase_functions_admin",
"supabase_read_only_user",
"supabase_replication_admin",
"supabase_storage_admin",
// Managed by extensions
"pgsodium_keyholder",
"pgsodium_keyiduser",
"pgsodium_keymaker",
"pgtle_admin",
}
AllowedConfigs = []string{
// Ref: https://github.com/supabase/postgres/blob/develop/ansible/files/postgresql_config/supautils.conf.j2#L10
"pgaudit.*",
"pgrst.*",
"session_replication_role",
"statement_timeout",
"track_io_timing",
}
SupabaseDirPath = "supabase"
ConfigPath = filepath.Join(SupabaseDirPath, "config.toml")
GitIgnorePath = filepath.Join(SupabaseDirPath, ".gitignore")
TempDir = filepath.Join(SupabaseDirPath, ".temp")
ImportMapsDir = filepath.Join(TempDir, "import_maps")
ProjectRefPath = filepath.Join(TempDir, "project-ref")
PoolerUrlPath = filepath.Join(TempDir, "pooler-url")
PostgresVersionPath = filepath.Join(TempDir, "postgres-version")
GotrueVersionPath = filepath.Join(TempDir, "gotrue-version")
RestVersionPath = filepath.Join(TempDir, "rest-version")
StorageVersionPath = filepath.Join(TempDir, "storage-version")
CurrBranchPath = filepath.Join(SupabaseDirPath, ".branches", "_current_branch")
MigrationsDir = filepath.Join(SupabaseDirPath, "migrations")
FunctionsDir = filepath.Join(SupabaseDirPath, "functions")
FallbackImportMapPath = filepath.Join(FunctionsDir, "import_map.json")
FallbackEnvFilePath = filepath.Join(FunctionsDir, ".env")
DbTestsDir = filepath.Join(SupabaseDirPath, "tests")
SeedDataPath = filepath.Join(SupabaseDirPath, "seed.sql")
CustomRolesPath = filepath.Join(SupabaseDirPath, "roles.sql")
ErrNotLinked = errors.Errorf("Cannot find project ref. Have you run %s?", Aqua("supabase link"))
ErrInvalidRef = errors.New("Invalid project ref format. Must be like `abcdefghijklmnopqrst`.")
ErrInvalidSlug = errors.New("Invalid Function name. Must start with at least one letter, and only include alphanumeric characters, underscores, and hyphens. (^[A-Za-z][A-Za-z0-9_-]*$)")
ErrNotRunning = errors.Errorf("%s is not running.", Aqua("supabase start"))
)
func GetCurrentTimestamp() string {
// Magic number: https://stackoverflow.com/q/45160822.
return time.Now().UTC().Format("20060102150405")
}
func GetCurrentBranchFS(fsys afero.Fs) (string, error) {
branch, err := afero.ReadFile(fsys, CurrBranchPath)
if err != nil {
return "", errors.Errorf("failed to load current branch: %w", err)
}
return string(branch), nil
}
func AssertSupabaseDbIsRunning() error {
if _, err := Docker.ContainerInspect(context.Background(), DbId); err != nil {
if client.IsErrNotFound(err) {
return errors.New(ErrNotRunning)
}
if client.IsErrConnectionFailed(err) {
CmdSuggestion = suggestDockerInstall
}
return errors.Errorf("failed to inspect database container: %w", err)
}
return nil
}
func IsGitRepo() bool {
opts := &git.PlainOpenOptions{DetectDotGit: true}
_, err := git.PlainOpenWithOptions(".", opts)
return err == nil
}
// If the `os.Getwd()` is within a supabase project, this will return
// the root of the given project as the current working directory.
// Otherwise, the `os.Getwd()` is kept as is.
func GetProjectRoot(fsys afero.Fs) (string, error) {
origWd, err := os.Getwd()
for cwd := origWd; err == nil; cwd = filepath.Dir(cwd) {
path := filepath.Join(cwd, ConfigPath)
// Treat all errors as file not exists
if isSupaProj, _ := afero.Exists(fsys, path); isSupaProj {
return cwd, nil
}
if isRootDirectory(cwd) {
break
}
}
if err != nil {
return "", errors.Errorf("failed to find project root: %w", err)
}
return origWd, nil
}
func IsBranchNameReserved(branch string) bool {
switch branch {
case "_current_branch", "main", "postgres", "template0", "template1":
return true
default:
return false
}
}
func MkdirIfNotExist(path string) error {
return MkdirIfNotExistFS(afero.NewOsFs(), path)
}
func MkdirIfNotExistFS(fsys afero.Fs, path string) error {
if err := fsys.MkdirAll(path, 0755); err != nil && !errors.Is(err, os.ErrExist) {
return errors.Errorf("failed to mkdir: %w", err)
}
return nil
}
func WriteFile(path string, contents []byte, fsys afero.Fs) error {
if err := MkdirIfNotExistFS(fsys, filepath.Dir(path)); err != nil {
return err
}
if err := afero.WriteFile(fsys, path, contents, 0644); err != nil {
return errors.Errorf("failed to write file: %w", err)
}
return nil
}
func AssertSupabaseCliIsSetUpFS(fsys afero.Fs) error {
if _, err := fsys.Stat(ConfigPath); errors.Is(err, os.ErrNotExist) {
return errors.Errorf("Cannot find %s in the current directory. Have you set up the project with %s?", Bold(ConfigPath), Aqua("supabase init"))
} else if err != nil {
return errors.Errorf("failed to read config file: %w", err)
}
return nil
}
func AssertProjectRefIsValid(projectRef string) error {
if !ProjectRefPattern.MatchString(projectRef) {
return errors.New(ErrInvalidRef)
}
return nil
}
func ValidateFunctionSlug(slug string) error {
if !FuncSlugPattern.MatchString(slug) {
return errors.New(ErrInvalidSlug)
}
return nil
}
func Ptr[T any](v T) *T {
return &v
}
func GetHostname() string {
host := Docker.DaemonHost()
if parsed, err := client.ParseHostURL(host); err == nil && parsed.Scheme == "tcp" {
if host, _, err := net.SplitHostPort(parsed.Host); err == nil {
return host
}
}
return "127.0.0.1"
}