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

supabase / cli / 16802493826

07 Aug 2025 10:55AM UTC coverage: 54.817% (-0.1%) from 54.932%
16802493826

push

github

web-flow
fix: diff schemas using pgkit migra (#3994)

* fix: diff schemas using pgkit migra

* chore: update exclusion rules for pg schema diff

35 of 56 new or added lines in 4 files covered. (62.5%)

24 existing lines in 2 files now uncovered.

6196 of 11303 relevant lines covered (54.82%)

6.14 hits per line

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

69.34
/internal/db/diff/diff.go
1
package diff
2

3
import (
4
        "context"
5
        _ "embed"
6
        "fmt"
7
        "io"
8
        "io/fs"
9
        "os"
10
        "path/filepath"
11
        "regexp"
12
        "strconv"
13
        "strings"
14
        "time"
15

16
        "github.com/cenkalti/backoff/v4"
17
        "github.com/docker/docker/api/types/container"
18
        "github.com/docker/docker/api/types/network"
19
        "github.com/docker/go-connections/nat"
20
        "github.com/go-errors/errors"
21
        "github.com/jackc/pgconn"
22
        "github.com/jackc/pgx/v4"
23
        "github.com/spf13/afero"
24
        "github.com/supabase/cli/internal/db/start"
25
        "github.com/supabase/cli/internal/gen/keys"
26
        "github.com/supabase/cli/internal/utils"
27
        "github.com/supabase/cli/pkg/migration"
28
        "github.com/supabase/cli/pkg/parser"
29
)
30

31
type DiffFunc func(context.Context, string, string, []string) (string, error)
32

33
func Run(ctx context.Context, schema []string, file string, config pgconn.Config, differ DiffFunc, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (err error) {
2✔
34
        out, err := DiffDatabase(ctx, schema, config, os.Stderr, fsys, differ, options...)
2✔
35
        if err != nil {
3✔
36
                return err
1✔
37
        }
1✔
38
        branch := keys.GetGitBranch(fsys)
1✔
39
        fmt.Fprintln(os.Stderr, "Finished "+utils.Aqua("supabase db diff")+" on branch "+utils.Aqua(branch)+".\n")
1✔
40
        if err := SaveDiff(out, file, fsys); err != nil {
1✔
41
                return err
×
42
        }
×
43
        drops := findDropStatements(out)
1✔
44
        if len(drops) > 0 {
1✔
45
                fmt.Fprintln(os.Stderr, "Found drop statements in schema diff. Please double check if these are expected:")
×
46
                fmt.Fprintln(os.Stderr, utils.Yellow(strings.Join(drops, "\n")))
×
47
        }
×
48
        return nil
1✔
49
}
50

51
func loadDeclaredSchemas(fsys afero.Fs) ([]string, error) {
1✔
52
        if schemas := utils.Config.Db.Migrations.SchemaPaths; len(schemas) > 0 {
1✔
53
                return schemas.Files(afero.NewIOFS(fsys))
×
54
        }
×
55
        if exists, err := afero.DirExists(fsys, utils.SchemasDir); err != nil {
1✔
56
                return nil, errors.Errorf("failed to check schemas: %w", err)
×
57
        } else if !exists {
1✔
58
                return nil, nil
×
59
        }
×
60
        var declared []string
1✔
61
        if err := afero.Walk(fsys, utils.SchemasDir, func(path string, info fs.FileInfo, err error) error {
10✔
62
                if err != nil {
9✔
63
                        return err
×
64
                }
×
65
                if info.Mode().IsRegular() && filepath.Ext(info.Name()) == ".sql" {
13✔
66
                        declared = append(declared, path)
4✔
67
                }
4✔
68
                return nil
9✔
69
        }); err != nil {
×
70
                return nil, errors.Errorf("failed to walk dir: %w", err)
×
71
        }
×
72
        return declared, nil
1✔
73
}
74

75
// https://github.com/djrobstep/migra/blob/master/migra/statements.py#L6
76
var dropStatementPattern = regexp.MustCompile(`(?i)drop\s+`)
77

78
func findDropStatements(out string) []string {
2✔
79
        lines, err := parser.SplitAndTrim(strings.NewReader(out))
2✔
80
        if err != nil {
2✔
81
                return nil
×
82
        }
×
83
        var drops []string
2✔
84
        for _, line := range lines {
6✔
85
                if dropStatementPattern.MatchString(line) {
6✔
86
                        drops = append(drops, line)
2✔
87
                }
2✔
88
        }
89
        return drops
2✔
90
}
91

92
func CreateShadowDatabase(ctx context.Context, port uint16) (string, error) {
13✔
93
        // Disable background workers in shadow database
13✔
94
        config := start.NewContainerConfig("-c", "max_worker_processes=0")
13✔
95
        hostPort := strconv.FormatUint(uint64(port), 10)
13✔
96
        hostConfig := container.HostConfig{
13✔
97
                PortBindings: nat.PortMap{"5432/tcp": []nat.PortBinding{{HostPort: hostPort}}},
13✔
98
                AutoRemove:   true,
13✔
99
        }
13✔
100
        networkingConfig := network.NetworkingConfig{}
13✔
101
        if utils.Config.Db.MajorVersion <= 14 {
18✔
102
                hostConfig.Tmpfs = map[string]string{"/docker-entrypoint-initdb.d": ""}
5✔
103
        }
5✔
104
        return utils.DockerStart(ctx, config, hostConfig, networkingConfig, "")
13✔
105
}
106

107
func ConnectShadowDatabase(ctx context.Context, timeout time.Duration, options ...func(*pgx.ConnConfig)) (conn *pgx.Conn, err error) {
9✔
108
        // Retry until connected, cancelled, or timeout
9✔
109
        policy := start.NewBackoffPolicy(ctx, timeout)
9✔
110
        config := pgconn.Config{Port: utils.Config.Db.ShadowPort}
9✔
111
        connect := func() (*pgx.Conn, error) {
18✔
112
                return utils.ConnectLocalPostgres(ctx, config, options...)
9✔
113
        }
9✔
114
        return backoff.RetryWithData(connect, policy)
9✔
115
}
116

117
// Required to bypass pg_cron check: https://github.com/citusdata/pg_cron/blob/main/pg_cron.sql#L3
118
const CREATE_TEMPLATE = "CREATE DATABASE contrib_regression TEMPLATE postgres"
119

120
func MigrateShadowDatabase(ctx context.Context, container string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
7✔
121
        migrations, err := migration.ListLocalMigrations(utils.MigrationsDir, afero.NewIOFS(fsys))
7✔
122
        if err != nil {
8✔
123
                return err
1✔
124
        }
1✔
125
        conn, err := ConnectShadowDatabase(ctx, 10*time.Second, options...)
6✔
126
        if err != nil {
7✔
127
                return err
1✔
128
        }
1✔
129
        defer conn.Close(context.Background())
5✔
130
        if err := start.SetupDatabase(ctx, conn, container[:12], os.Stderr, fsys); err != nil {
7✔
131
                return err
2✔
132
        }
2✔
133
        if _, err := conn.Exec(ctx, CREATE_TEMPLATE); err != nil {
3✔
134
                return errors.Errorf("failed to create template database: %w", err)
×
135
        }
×
136
        return migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys))
3✔
137
}
138

139
func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w io.Writer, fsys afero.Fs, differ func(context.Context, string, string, []string) (string, error), options ...func(*pgx.ConnConfig)) (string, error) {
7✔
140
        fmt.Fprintln(w, "Creating shadow database...")
7✔
141
        shadow, err := CreateShadowDatabase(ctx, utils.Config.Db.ShadowPort)
7✔
142
        if err != nil {
10✔
143
                return "", err
3✔
144
        }
3✔
145
        defer utils.DockerRemove(shadow)
4✔
146
        if err := start.WaitForHealthyService(ctx, start.HealthTimeout, shadow); err != nil {
5✔
147
                return "", err
1✔
148
        }
1✔
149
        if err := MigrateShadowDatabase(ctx, shadow, fsys, options...); err != nil {
4✔
150
                return "", err
1✔
151
        }
1✔
152
        shadowConfig := pgconn.Config{
2✔
153
                Host:     utils.Config.Hostname,
2✔
154
                Port:     utils.Config.Db.ShadowPort,
2✔
155
                User:     "postgres",
2✔
156
                Password: utils.Config.Db.Password,
2✔
157
                Database: "postgres",
2✔
158
        }
2✔
159
        if utils.IsLocalDatabase(config) {
2✔
160
                if declared, err := loadDeclaredSchemas(fsys); err != nil {
×
161
                        return "", err
×
162
                } else if len(declared) > 0 {
×
163
                        config = shadowConfig
×
164
                        config.Database = "contrib_regression"
×
165
                        if err := migrateBaseDatabase(ctx, config, declared, fsys, options...); err != nil {
×
166
                                return "", err
×
167
                        }
×
168
                }
169
        }
170
        // Load all user defined schemas
171
        if len(schema) > 0 {
4✔
172
                fmt.Fprintln(w, "Diffing schemas:", strings.Join(schema, ","))
2✔
173
        } else {
2✔
NEW
174
                fmt.Fprintln(w, "Diffing schemas...")
×
UNCOV
175
        }
×
176
        source := utils.ToPostgresURL(shadowConfig)
2✔
177
        target := utils.ToPostgresURL(config)
2✔
178
        return differ(ctx, source, target, schema)
2✔
179
}
180

181
func migrateBaseDatabase(ctx context.Context, config pgconn.Config, migrations []string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
×
182
        fmt.Fprintln(os.Stderr, "Creating local database from declarative schemas:")
×
183
        msg := make([]string, len(migrations))
×
184
        for i, m := range migrations {
×
185
                msg[i] = fmt.Sprintf(" • %s", utils.Bold(m))
×
186
        }
×
187
        fmt.Fprintln(os.Stderr, strings.Join(msg, "\n"))
×
188
        conn, err := utils.ConnectLocalPostgres(ctx, config, options...)
×
189
        if err != nil {
×
190
                return err
×
191
        }
×
192
        defer conn.Close(context.Background())
×
193
        return migration.SeedGlobals(ctx, migrations, conn, afero.NewIOFS(fsys))
×
194
}
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