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

supabase / cli / 16535729838

26 Jul 2025 03:45AM UTC coverage: 55.377%. First build
16535729838

Pull #3920

github

web-flow
Merge dbf2d9d2d into b720b1240
Pull Request #3920: Feat/enhance drop warning

27 of 59 new or added lines in 3 files covered. (45.76%)

6220 of 11232 relevant lines covered (55.38%)

6.18 hits per line

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

8.14
/internal/db/diff/pgadmin.go
1
package diff
2

3
import (
4
        "context"
5
        _ "embed"
6
        "fmt"
7
        "os"
8

9
        "github.com/jackc/pgconn"
10
        "github.com/spf13/afero"
11
        "github.com/supabase/cli/internal/db/start"
12
        "github.com/supabase/cli/internal/migration/new"
13
        "github.com/supabase/cli/internal/utils"
14
        "github.com/supabase/cli/pkg/config"
15
)
16

17
var warnDiff = `WARNING: The diff tool is not foolproof, so you may need to manually rearrange and modify the generated migration.
18
Run ` + utils.Aqua("supabase db reset") + ` to verify that the new migration does not generate errors.`
19

20
func SaveDiff(out, file string, fsys afero.Fs) error {
1✔
21
        if len(out) < 2 {
1✔
22
                fmt.Fprintln(os.Stderr, "No schema changes found")
×
23
        } else if len(file) > 0 {
2✔
24
                path := new.GetMigrationPath(utils.GetCurrentTimestamp(), file)
1✔
25
                if err := utils.WriteFile(path, []byte(out), fsys); err != nil {
1✔
26
                        return err
×
27
                }
×
28
                fmt.Fprintln(os.Stderr, warnDiff)
1✔
29
        } else {
×
30
                fmt.Println(out)
×
31
        }
×
32
        return nil
1✔
33
}
34

NEW
35
func RunPgAdmin(ctx context.Context, schema []string, file string, config pgconn.Config, fsys afero.Fs, confirmDrops bool) error {
×
36
        // Sanity checks.
×
37
        if err := utils.AssertSupabaseDbIsRunning(); err != nil {
×
38
                return err
×
39
        }
×
40

41
        if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
×
42
                return run(p, ctx, schema, config, fsys)
×
43
        }); err != nil {
×
44
                return err
×
45
        }
×
46

NEW
47
        drops := findDropStatements(output)
×
NEW
48
        if len(drops) > 0 {
×
NEW
49
                if confirmDrops {
×
NEW
50
                        if err := showDropWarningAndConfirm(ctx, drops); err != nil {
×
NEW
51
                                return err
×
NEW
52
                        }
×
NEW
53
                } else {
×
NEW
54
                        fmt.Fprintln(os.Stderr, "Found drop statements in schema diff. Please double check if these are expected:")
×
NEW
55
                        for _, drop := range drops {
×
NEW
56
                                fmt.Fprintln(os.Stderr, "  "+drop)
×
NEW
57
                        }
×
NEW
58
                        fmt.Fprintln(os.Stderr, "")
×
59
                }
60
        }
61

62
        return SaveDiff(output, file, fsys)
×
63
}
64

65
var output string
66

67
func run(p utils.Program, ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
×
68
        p.Send(utils.StatusMsg("Creating shadow database..."))
×
69

×
70
        // 1. Create shadow db and run migrations
×
71
        shadow, err := CreateShadowDatabase(ctx, utils.Config.Db.ShadowPort)
×
72
        if err != nil {
×
73
                return err
×
74
        }
×
75
        defer utils.DockerRemove(shadow)
×
76
        if err := start.WaitForHealthyService(ctx, start.HealthTimeout, shadow); err != nil {
×
77
                return err
×
78
        }
×
79
        if err := MigrateShadowDatabase(ctx, shadow, fsys); err != nil {
×
80
                return err
×
81
        }
×
82

83
        p.Send(utils.StatusMsg("Diffing local database with current migrations..."))
×
84

×
85
        // 2. Diff local db (source) with shadow db (target), print it.
×
86
        source := utils.ToPostgresURL(config)
×
87
        target := fmt.Sprintf("postgresql://postgres:postgres@127.0.0.1:%d/postgres", utils.Config.Db.ShadowPort)
×
88
        output, err = DiffSchemaPgAdmin(ctx, source, target, schema, p)
×
89
        return err
×
90
}
91

92
func DiffSchemaPgAdmin(ctx context.Context, source, target string, schema []string, p utils.Program) (string, error) {
×
93
        stream := utils.NewDiffStream(p)
×
94
        args := []string{"--json-diff", source, target}
×
95
        if len(schema) == 0 {
×
96
                if err := utils.DockerRunOnceWithStream(
×
97
                        ctx,
×
98
                        config.Images.Differ,
×
99
                        nil,
×
100
                        args,
×
101
                        stream.Stdout(),
×
102
                        stream.Stderr(),
×
103
                ); err != nil {
×
104
                        return "", err
×
105
                }
×
106
        }
107
        for _, s := range schema {
×
108
                p.Send(utils.StatusMsg("Diffing schema: " + s))
×
109
                if err := utils.DockerRunOnceWithStream(
×
110
                        ctx,
×
111
                        config.Images.Differ,
×
112
                        nil,
×
113
                        append([]string{"--schema", s}, args...),
×
114
                        stream.Stdout(),
×
115
                        stream.Stderr(),
×
116
                ); err != nil {
×
117
                        return "", err
×
118
                }
×
119
        }
120
        diffBytes, err := stream.Collect()
×
121
        return string(diffBytes), err
×
122
}
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

© 2026 Coveralls, Inc