-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathmigra.go
More file actions
126 lines (119 loc) · 3.29 KB
/
migra.go
File metadata and controls
126 lines (119 loc) · 3.29 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
122
123
124
125
126
package diff
import (
"bytes"
"context"
_ "embed"
"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/supabase/cli/internal/gen/types"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/config"
"github.com/supabase/cli/pkg/migration"
)
var (
//go:embed templates/migra.sh
diffSchemaScript string
//go:embed templates/migra.ts
diffSchemaTypeScript string
managedSchemas = []string{
// Local development
"_analytics",
"_realtime",
"_supavisor",
// Owned by extensions
"cron",
"graphql",
"graphql_public",
"net",
"pgroonga",
"pgtle",
"repack",
"tiger_data",
"vault",
// Deprecated extensions
"pgsodium",
"pgsodium_masks",
"timescaledb_experimental",
"timescaledb_information",
"_timescaledb_cache",
"_timescaledb_catalog",
"_timescaledb_config",
"_timescaledb_debug",
"_timescaledb_functions",
"_timescaledb_internal",
// Managed by Supabase
"pgbouncer",
"supabase_functions",
"supabase_migrations",
}
)
// Diffs local database schema against shadow, dumps output to stdout.
func DiffSchemaMigraBash(ctx context.Context, source, target pgconn.Config, schema []string, options ...func(*pgx.ConnConfig)) (string, error) {
// Load all user defined schemas
if len(schema) == 0 {
var err error
if schema, err = loadSchema(ctx, target, options...); err != nil {
return "", err
}
}
env := []string{
"SOURCE=" + utils.ToPostgresURL(source),
"TARGET=" + utils.ToPostgresURL(target),
}
// Passing in script string means command line args must be set manually, ie. "$@"
args := "set -- " + strings.Join(schema, " ") + ";"
cmd := []string{"/bin/sh", "-c", args + diffSchemaScript}
var out, stderr bytes.Buffer
if err := utils.DockerRunOnceWithConfig(
ctx,
container.Config{
Image: config.Images.Migra,
Env: env,
Cmd: cmd,
},
container.HostConfig{
NetworkMode: network.NetworkHost,
},
network.NetworkingConfig{},
"",
&out,
&stderr,
); err != nil {
return "", errors.Errorf("error diffing schema: %w:\n%s", err, stderr.String())
}
return out.String(), nil
}
func loadSchema(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) ([]string, error) {
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return nil, err
}
defer conn.Close(context.Background())
// RLS policies in auth and storage schemas can be included with -s flag
return migration.ListUserSchemas(ctx, conn)
}
func DiffSchemaMigra(ctx context.Context, source, target pgconn.Config, schema []string, options ...func(*pgx.ConnConfig)) (string, error) {
env := []string{
"SOURCE=" + utils.ToPostgresURL(source),
"TARGET=" + utils.ToPostgresURL(target),
}
if ca, err := types.GetRootCA(ctx, utils.ToPostgresURL(target), options...); err != nil {
return "", err
} else if len(ca) > 0 {
env = append(env, "SSL_CA="+ca)
}
if len(schema) > 0 {
env = append(env, "INCLUDED_SCHEMAS="+strings.Join(schema, ","))
} else {
env = append(env, "EXCLUDED_SCHEMAS="+strings.Join(managedSchemas, ","))
}
var out bytes.Buffer
if err := diffWithStream(ctx, env, diffSchemaTypeScript, &out); err != nil {
return "", err
}
return out.String(), nil
}