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

supabase / cli / 5319215965

20 Jun 2023 06:21AM UTC coverage: 61.791% (+0.3%) from 61.472%
5319215965

push

github

sweatybridge
chore: improve squash log messages

3 of 3 new or added lines in 1 file covered. (100.0%)

4740 of 7671 relevant lines covered (61.79%)

943.92 hits per line

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

8.54
/internal/db/diff/diff.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/migration/new"
12
        "github.com/supabase/cli/internal/utils"
13
)
14

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

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

33
func Run(ctx context.Context, schema []string, file string, config pgconn.Config, fsys afero.Fs) error {
×
34
        // Sanity checks.
×
35
        {
×
36
                if err := utils.LoadConfigFS(fsys); err != nil {
×
37
                        return err
×
38
                }
×
39
                if err := utils.AssertSupabaseDbIsRunning(); err != nil {
×
40
                        return err
×
41
                }
×
42
        }
43

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

50
        return SaveDiff(output, file, fsys)
×
51
}
52

53
var output string
54

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

×
58
        // 1. Create shadow db and run migrations
×
59
        shadow, err := CreateShadowDatabase(ctx)
×
60
        if err != nil {
×
61
                return err
×
62
        }
×
63
        defer utils.DockerRemove(shadow)
×
64
        if err := MigrateShadowDatabase(ctx, shadow, fsys); err != nil {
×
65
                return err
×
66
        }
×
67

68
        p.Send(utils.StatusMsg("Diffing local database with current migrations..."))
×
69

×
70
        // 2. Diff local db (source) with shadow db (target), print it.
×
71
        source := "postgresql://postgres:postgres@" + utils.DbId + ":5432/postgres"
×
72
        if len(config.Password) == 0 {
×
73
                config.Host = shadow[:12]
×
74
                config.Port = 5432
×
75
                config.User = "postgres"
×
76
                config.Password = "postgres"
×
77
                config.Database = "postgres"
×
78
        }
×
79
        target := utils.ToPostgresURL(config)
×
80
        output, err = DiffSchema(ctx, source, target, schema, p)
×
81
        return err
×
82
}
83

84
func DiffSchema(ctx context.Context, source, target string, schema []string, p utils.Program) (string, error) {
×
85
        stream := utils.NewDiffStream(p)
×
86
        args := []string{"--json-diff", source, target}
×
87
        if len(schema) == 0 {
×
88
                if err := utils.DockerRunOnceWithStream(
×
89
                        ctx,
×
90
                        utils.DifferImage,
×
91
                        nil,
×
92
                        args,
×
93
                        stream.Stdout(),
×
94
                        stream.Stderr(),
×
95
                ); err != nil {
×
96
                        return "", err
×
97
                }
×
98
        }
99
        for _, s := range schema {
×
100
                p.Send(utils.StatusMsg("Diffing schema: " + s))
×
101
                if err := utils.DockerRunOnceWithStream(
×
102
                        ctx,
×
103
                        utils.DifferImage,
×
104
                        nil,
×
105
                        append([]string{"--schema", s}, args...),
×
106
                        stream.Stdout(),
×
107
                        stream.Stderr(),
×
108
                ); err != nil {
×
109
                        return "", err
×
110
                }
×
111
        }
112
        diffBytes, err := stream.Collect()
×
113
        return string(diffBytes), err
×
114
}
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