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

supabase / cli / 15489016285

06 Jun 2025 11:01AM UTC coverage: 55.018% (+0.2%) from 54.859%
15489016285

Pull #3673

github

web-flow
Merge dbb96ca7a into 865f304d5
Pull Request #3673: fix: move db dump to public pkg

15 of 43 new or added lines in 4 files covered. (34.88%)

5 existing lines in 1 file now uncovered.

5964 of 10840 relevant lines covered (55.02%)

6.11 hits per line

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

57.38
/internal/db/dump/dump.go
1
package dump
2

3
import (
4
        "context"
5
        _ "embed"
6
        "fmt"
7
        "io"
8
        "os"
9
        "strings"
10

11
        "github.com/docker/docker/api/types/container"
12
        "github.com/docker/docker/api/types/network"
13
        "github.com/go-errors/errors"
14
        "github.com/jackc/pgconn"
15
        "github.com/spf13/afero"
16
        "github.com/supabase/cli/internal/utils"
17
        "github.com/supabase/cli/pkg/migration"
18
)
19

20
func Run(ctx context.Context, path string, config pgconn.Config, dataOnly, roleOnly, dryRun bool, fsys afero.Fs, opts ...migration.DumpOptionFunc) error {
4✔
21
        // Initialize output stream
4✔
22
        outStream := (io.Writer)(os.Stdout)
4✔
23
        exec := DockerExec
4✔
24
        if dryRun {
4✔
NEW
25
                fmt.Fprintln(os.Stderr, "DRY RUN: *only* printing the pg_dump script to console.")
×
NEW
26
                exec = noExec
×
27
        } else if len(path) > 0 {
6✔
28
                f, err := fsys.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
2✔
29
                if err != nil {
3✔
30
                        return errors.Errorf("failed to open dump file: %w", err)
1✔
31
                }
1✔
32
                defer f.Close()
1✔
33
                outStream = f
1✔
34
        }
35
        db := "remote"
3✔
36
        if utils.IsLocalDatabase(config) {
3✔
37
                db = "local"
×
38
        }
×
39
        if dataOnly {
3✔
40
                fmt.Fprintf(os.Stderr, "Dumping data from %s database...\n", db)
×
NEW
41
                return migration.DumpData(ctx, config, outStream, exec, opts...)
×
42
        } else if roleOnly {
3✔
43
                fmt.Fprintf(os.Stderr, "Dumping roles from %s database...\n", db)
×
NEW
44
                return migration.DumpRole(ctx, config, outStream, exec, opts...)
×
45
        }
×
46
        fmt.Fprintf(os.Stderr, "Dumping schemas from %s database...\n", db)
3✔
47
        return migration.DumpSchema(ctx, config, outStream, exec, opts...)
3✔
48
}
49

NEW
50
func noExec(ctx context.Context, script string, env []string, w io.Writer) error {
×
NEW
51
        envMap := make(map[string]string, len(env))
×
NEW
52
        for _, e := range env {
×
NEW
53
                index := strings.IndexByte(e, '=')
×
NEW
54
                if index < 0 {
×
NEW
55
                        continue
×
56
                }
NEW
57
                envMap[e[:index]] = e[index+1:]
×
58
        }
NEW
59
        expanded := os.Expand(script, func(key string) string {
×
NEW
60
                // Bash variable expansion is unsupported:
×
NEW
61
                // https://github.com/golang/go/issues/47187
×
NEW
62
                parts := strings.Split(key, ":")
×
NEW
63
                value := envMap[parts[0]]
×
NEW
64
                // Escape double quotes in env vars
×
NEW
65
                return strings.ReplaceAll(value, `"`, `\"`)
×
NEW
66
        })
×
NEW
67
        fmt.Fprintln(w, expanded)
×
NEW
68
        return nil
×
69
}
70

71
func DockerExec(ctx context.Context, script string, env []string, w io.Writer) error {
9✔
72
        return utils.DockerRunOnceWithConfig(
9✔
73
                ctx,
9✔
74
                container.Config{
9✔
75
                        Image: utils.Config.Db.Image,
9✔
76
                        Env:   env,
9✔
77
                        Cmd:   []string{"bash", "-c", script, "--"},
9✔
78
                },
9✔
79
                container.HostConfig{
9✔
80
                        NetworkMode: network.NetworkHost,
9✔
81
                },
9✔
82
                network.NetworkingConfig{},
9✔
83
                "",
9✔
84
                w,
9✔
85
                os.Stderr,
9✔
86
        )
9✔
87
}
9✔
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