-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathdiff.go
More file actions
246 lines (230 loc) · 8.16 KB
/
diff.go
File metadata and controls
246 lines (230 loc) · 8.16 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
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
package diff
import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/db/start"
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/parser"
)
type DiffFunc func(context.Context, pgconn.Config, pgconn.Config, []string, ...func(*pgx.ConnConfig)) (string, error)
func Run(ctx context.Context, schema []string, file string, config pgconn.Config, differ DiffFunc, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (err error) {
out, err := DiffDatabase(ctx, schema, config, os.Stderr, fsys, differ, options...)
if err != nil {
return err
}
branch := utils.GetGitBranch(fsys)
fmt.Fprintln(os.Stderr, "Finished "+utils.Aqua("supabase db diff")+" on branch "+utils.Aqua(branch)+".\n")
if err := SaveDiff(out, file, fsys); err != nil {
return err
}
drops := findDropStatements(out)
if len(drops) > 0 {
fmt.Fprintln(os.Stderr, "Found drop statements in schema diff. Please double check if these are expected:")
fmt.Fprintln(os.Stderr, utils.Yellow(strings.Join(drops, "\n")))
}
return nil
}
var warnDiff = `WARNING: The diff tool is not foolproof, so you may need to manually rearrange and modify the generated migration.
Run ` + utils.Aqua("supabase db reset") + ` to verify that the new migration does not generate errors.`
func SaveDiff(out, file string, fsys afero.Fs) error {
if len(out) < 2 {
fmt.Fprintln(os.Stderr, "No schema changes found")
} else if len(file) > 0 {
path := new.GetMigrationPath(utils.GetCurrentTimestamp(), file)
if err := utils.WriteFile(path, []byte(out), fsys); err != nil {
return err
}
fmt.Fprintln(os.Stderr, warnDiff)
} else {
fmt.Println(out)
}
return nil
}
func loadDeclaredSchemas(fsys afero.Fs) ([]string, error) {
if schemas := utils.Config.Db.Migrations.SchemaPaths; len(schemas) > 0 {
return schemas.Files(afero.NewIOFS(fsys))
}
if exists, err := afero.DirExists(fsys, utils.SchemasDir); err != nil {
return nil, errors.Errorf("failed to check schemas: %w", err)
} else if !exists {
return nil, nil
}
var declared []string
if err := afero.Walk(fsys, utils.SchemasDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() && filepath.Ext(info.Name()) == ".sql" {
declared = append(declared, path)
}
return nil
}); err != nil {
return nil, errors.Errorf("failed to walk dir: %w", err)
}
return declared, nil
}
// https://github.com/djrobstep/migra/blob/master/migra/statements.py#L6
var dropStatementPattern = regexp.MustCompile(`(?i)drop\s+`)
func findDropStatements(out string) []string {
lines, err := parser.SplitAndTrim(strings.NewReader(out))
if err != nil {
return nil
}
var drops []string
for _, line := range lines {
if dropStatementPattern.MatchString(line) {
drops = append(drops, line)
}
}
return drops
}
func CreateShadowDatabase(ctx context.Context, port uint16) (string, error) {
// Disable background workers in shadow database
config := start.NewContainerConfig("-c", "max_worker_processes=0")
hostPort := strconv.FormatUint(uint64(port), 10)
hostConfig := container.HostConfig{
PortBindings: nat.PortMap{"5432/tcp": []nat.PortBinding{{HostPort: hostPort}}},
AutoRemove: true,
}
networkingConfig := network.NetworkingConfig{}
if utils.Config.Db.MajorVersion <= 14 {
hostConfig.Tmpfs = map[string]string{"/docker-entrypoint-initdb.d": ""}
}
return utils.DockerStart(ctx, config, hostConfig, networkingConfig, "")
}
func ConnectShadowDatabase(ctx context.Context, timeout time.Duration, options ...func(*pgx.ConnConfig)) (conn *pgx.Conn, err error) {
// Retry until connected, cancelled, or timeout
policy := start.NewBackoffPolicy(ctx, timeout)
config := pgconn.Config{Port: utils.Config.Db.ShadowPort}
connect := func() (*pgx.Conn, error) {
return utils.ConnectLocalPostgres(ctx, config, options...)
}
return backoff.RetryWithData(connect, policy)
}
// Required to bypass pg_cron check: https://github.com/citusdata/pg_cron/blob/main/pg_cron.sql#L3
const CREATE_TEMPLATE = "CREATE DATABASE contrib_regression TEMPLATE postgres"
func MigrateShadowDatabase(ctx context.Context, container string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
migrations, err := migration.ListLocalMigrations(utils.MigrationsDir, afero.NewIOFS(fsys))
if err != nil {
return err
}
conn, err := ConnectShadowDatabase(ctx, 10*time.Second, options...)
if err != nil {
return err
}
defer conn.Close(context.Background())
if err := start.SetupDatabase(ctx, conn, container[:12], os.Stderr, fsys); err != nil {
return err
}
if _, err := conn.Exec(ctx, CREATE_TEMPLATE); err != nil {
return errors.Errorf("failed to create template database: %w", err)
}
return migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys))
}
func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w io.Writer, fsys afero.Fs, differ DiffFunc, options ...func(*pgx.ConnConfig)) (string, error) {
fmt.Fprintln(w, "Creating shadow database...")
shadow, err := CreateShadowDatabase(ctx, utils.Config.Db.ShadowPort)
if err != nil {
return "", err
}
defer utils.DockerRemove(shadow)
if err := start.WaitForHealthyService(ctx, utils.Config.Db.HealthTimeout, shadow); err != nil {
return "", err
}
if err := MigrateShadowDatabase(ctx, shadow, fsys, options...); err != nil {
return "", err
}
shadowConfig := pgconn.Config{
Host: utils.Config.Hostname,
Port: utils.Config.Db.ShadowPort,
User: "postgres",
Password: utils.Config.Db.Password,
Database: "postgres",
}
if utils.IsLocalDatabase(config) {
if declared, err := loadDeclaredSchemas(fsys); len(declared) > 0 {
config = shadowConfig
config.Database = "contrib_regression"
if err := migrateBaseDatabase(ctx, config, declared, fsys, options...); err != nil {
return "", err
}
} else if err != nil {
return "", err
}
}
// Load all user defined schemas
if len(schema) > 0 {
fmt.Fprintln(w, "Diffing schemas:", strings.Join(schema, ","))
} else {
fmt.Fprintln(w, "Diffing schemas...")
}
return differ(ctx, shadowConfig, config, schema, options...)
}
func migrateBaseDatabase(ctx context.Context, config pgconn.Config, migrations []string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
fmt.Fprintln(os.Stderr, "Creating local database from declarative schemas:")
msg := make([]string, len(migrations))
for i, m := range migrations {
msg[i] = fmt.Sprintf(" • %s", utils.Bold(m))
}
fmt.Fprintln(os.Stderr, strings.Join(msg, "\n"))
conn, err := utils.ConnectLocalPostgres(ctx, config, options...)
if err != nil {
return err
}
defer conn.Close(context.Background())
return migration.SeedGlobals(ctx, migrations, conn, afero.NewIOFS(fsys))
}
func diffWithStream(ctx context.Context, env []string, script string, stdout io.Writer) error {
cmd := []string{"edge-runtime", "start", "--main-service=."}
if viper.GetBool("DEBUG") {
cmd = append(cmd, "--verbose")
}
cmdString := strings.Join(cmd, " ")
entrypoint := []string{"sh", "-c", `cat <<'EOF' > index.ts && ` + cmdString + `
` + script + `
EOF
`}
var stderr bytes.Buffer
if err := utils.DockerRunOnceWithConfig(
ctx,
container.Config{
Image: utils.Config.EdgeRuntime.Image,
Env: env,
Entrypoint: entrypoint,
},
container.HostConfig{
Binds: []string{utils.EdgeRuntimeId + ":/root/.cache/deno:rw"},
NetworkMode: network.NetworkHost,
},
network.NetworkingConfig{},
"",
stdout,
&stderr,
// The "main worker has been destroyed" message may not appear at the start of stderr
// (e.g. preceded by other Deno runtime output), so use Contains instead of HasPrefix.
); err != nil && !strings.Contains(stderr.String(), "main worker has been destroyed") {
return errors.Errorf("error diffing schema: %w:\n%s", err, stderr.String())
}
return nil
}