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

supabase / cli / 7245044079

18 Dec 2023 07:42AM UTC coverage: 58.331% (+0.2%) from 58.141%
7245044079

push

github

sweatybridge
chore: move windows arm64 build to separate PR

5962 of 10221 relevant lines covered (58.33%)

710.01 hits per line

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

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

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

9
        "github.com/go-errors/errors"
10
        "github.com/jackc/pgconn"
11
        "github.com/spf13/afero"
12
        "github.com/supabase/cli/internal/db/start"
13
        "github.com/supabase/cli/internal/migration/new"
14
        "github.com/supabase/cli/internal/utils"
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 := afero.WriteFile(fsys, path, []byte(out), 0644); err != nil {
1✔
26
                        return errors.Errorf("failed to save diff: %w", err)
×
27
                }
×
28
                fmt.Fprintln(os.Stderr, warnDiff)
1✔
29
        } else {
×
30
                fmt.Println(out)
×
31
        }
×
32
        return nil
1✔
33
}
34

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

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

52
        return SaveDiff(output, file, fsys)
×
53
}
54

55
var output string
56

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

×
60
        // 1. Create shadow db and run migrations
×
61
        shadow, err := CreateShadowDatabase(ctx)
×
62
        if err != nil {
×
63
                return err
×
64
        }
×
65
        defer utils.DockerRemove(shadow)
×
66
        if !start.WaitForHealthyService(ctx, shadow, start.HealthTimeout) {
×
67
                return errors.New(start.ErrDatabase)
×
68
        }
×
69
        if err := MigrateShadowDatabase(ctx, shadow, fsys); err != nil {
×
70
                return err
×
71
        }
×
72

73
        p.Send(utils.StatusMsg("Diffing local database with current migrations..."))
×
74

×
75
        // 2. Diff local db (source) with shadow db (target), print it.
×
76
        source := utils.ToPostgresURL(config)
×
77
        target := fmt.Sprintf("postgresql://postgres:postgres@127.0.0.1:%d/postgres", utils.Config.Db.ShadowPort)
×
78
        output, err = DiffSchema(ctx, source, target, schema, p)
×
79
        return err
×
80
}
81

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