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

supabase / cli / 3572813994

04 Dec 2022 06:36PM UTC coverage: 54.923% (-2.8%) from 57.76%
3572813994

Pull #648

github

Kevin Saliou
chore: remove all tabs & trailing spaces from SQL files
Pull Request #648: chore: remove all tabs & trailing spaces from SQL files

3057 of 5566 relevant lines covered (54.92%)

498.14 hits per line

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

64.62
/internal/db/start/start.go
1
package start
2

3
import (
4
        "context"
5
        "errors"
6
        "fmt"
7
        "io"
8
        "os"
9
        "path/filepath"
10
        "strconv"
11
        "strings"
12
        "time"
13

14
        "github.com/docker/docker/api/types/container"
15
        "github.com/docker/go-connections/nat"
16
        "github.com/jackc/pgx/v4"
17
        "github.com/spf13/afero"
18
        "github.com/supabase/cli/internal/db/diff"
19
        "github.com/supabase/cli/internal/db/lint"
20
        "github.com/supabase/cli/internal/db/reset"
21
        "github.com/supabase/cli/internal/utils"
22
)
23

24
func Run(ctx context.Context, fsys afero.Fs) error {
×
25
        if err := utils.AssertSupabaseCliIsSetUpFS(fsys); err != nil {
×
26
                return err
×
27
        }
×
28
        if err := utils.LoadConfigFS(fsys); err != nil {
×
29
                return err
×
30
        }
×
31
        if err := utils.AssertDockerIsRunning(); err != nil {
×
32
                return err
×
33
        }
×
34
        if _, err := utils.Docker.ContainerInspect(ctx, utils.DbId); err == nil {
×
35
                fmt.Fprintln(os.Stderr, "Postgres database is already running.")
×
36
                return nil
×
37
        }
×
38
        return StartDatabase(ctx, fsys, os.Stderr)
×
39
}
40

41
func StartDatabase(ctx context.Context, fsys afero.Fs, w io.Writer, options ...func(*pgx.ConnConfig)) error {
1✔
42
        config := container.Config{
1✔
43
                Image: utils.DbImage,
1✔
44
                Env:   []string{"POSTGRES_PASSWORD=postgres"},
1✔
45
                Healthcheck: &container.HealthConfig{
1✔
46
                        Test:     []string{"CMD", "pg_isready", "-U", "postgres", "-h", "localhost", "-p", "5432"},
1✔
47
                        Interval: 2 * time.Second,
1✔
48
                        Timeout:  2 * time.Second,
1✔
49
                        Retries:  10,
1✔
50
                },
1✔
51
        }
1✔
52
        if utils.Config.Db.MajorVersion >= 14 {
2✔
53
                config.Cmd = []string{"postgres",
1✔
54
                        "-c", "config_file=/etc/postgresql/postgresql.conf",
1✔
55
                        // One log file per hour, 24 hours retention
1✔
56
                        "-c", "log_destination=csvlog",
1✔
57
                        "-c", "logging_collector=on",
1✔
58
                        "-c", "log_directory=/var/log/postgresql",
1✔
59
                        "-c", "log_filename=server_%H00_UTC.log",
1✔
60
                        "-c", "log_file_mode=0640",
1✔
61
                        "-c", "log_rotation_age=60",
1✔
62
                        "-c", "log_rotation_size=0",
1✔
63
                        "-c", "log_truncate_on_rotation=on",
1✔
64
                        // Ref: https://postgrespro.com/list/thread-id/2448092
1✔
65
                        "-c", `search_path="$user",public,extensions`,
1✔
66
                }
1✔
67
        }
1✔
68
        hostPort := strconv.FormatUint(uint64(utils.Config.Db.Port), 10)
1✔
69
        hostConfig := container.HostConfig{
1✔
70
                PortBindings:  nat.PortMap{"5432/tcp": []nat.PortBinding{{HostPort: hostPort}}},
1✔
71
                RestartPolicy: container.RestartPolicy{Name: "always"},
1✔
72
                Binds:         []string{"/dev/null:/docker-entrypoint-initdb.d/migrate.sh:ro"},
1✔
73
        }
1✔
74
        fmt.Fprintln(w, "Starting database...")
1✔
75
        if _, err := utils.DockerStart(ctx, config, hostConfig, utils.DbId); err != nil {
1✔
76
                return err
×
77
        }
×
78
        return initDatabase(ctx, fsys, w, options...)
1✔
79
}
80

81
func initDatabase(ctx context.Context, fsys afero.Fs, w io.Writer, options ...func(*pgx.ConnConfig)) error {
6✔
82
        if !reset.WaitForHealthyDatabase(ctx, 20*time.Second) {
6✔
83
                fmt.Fprintln(os.Stderr, "Database is not healthy.")
×
84
        }
×
85
        // Initialise globals
86
        conn, err := lint.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.Port, "postgres", options...)
6✔
87
        if err != nil {
7✔
88
                return err
1✔
89
        }
1✔
90
        defer conn.Close(context.Background())
5✔
91
        if err := diff.BatchExecDDL(ctx, conn, strings.NewReader(utils.GlobalsSql)); err != nil {
6✔
92
                return err
1✔
93
        }
1✔
94

95
        fmt.Fprintln(w, "Restoring branches...")
4✔
96

4✔
97
        // Create branch dir if missing
4✔
98
        branchDir := filepath.Dir(utils.CurrBranchPath)
4✔
99
        if err := utils.MkdirIfNotExistFS(fsys, branchDir); err != nil {
5✔
100
                return err
1✔
101
        }
1✔
102
        branches, err := afero.ReadDir(fsys, branchDir)
3✔
103
        if err != nil {
3✔
104
                return err
×
105
        }
×
106
        // Ensure `_current_branch` file exists.
107
        currBranch, err := utils.GetCurrentBranchFS(fsys)
3✔
108
        if errors.Is(err, os.ErrNotExist) {
5✔
109
                currBranch = "main"
2✔
110
                if err := afero.WriteFile(fsys, utils.CurrBranchPath, []byte(currBranch), 0644); err != nil {
2✔
111
                        return err
×
112
                }
×
113
        } else if err != nil {
1✔
114
                return err
×
115
        }
×
116
        // Restore every branch dump
117
        for _, branch := range branches {
7✔
118
                if !branch.IsDir() {
5✔
119
                        continue
1✔
120
                }
121
                dumpPath := filepath.Join(branchDir, branch.Name(), "dump.sql")
3✔
122
                content, err := fsys.Open(dumpPath)
3✔
123
                if errors.Is(err, os.ErrNotExist) {
4✔
124
                        fmt.Fprintln(os.Stderr, "Error restoring "+utils.Aqua(branch.Name())+": branch was not dumped.")
1✔
125
                        if err := fsys.RemoveAll(filepath.Dir(dumpPath)); err != nil {
1✔
126
                                return err
×
127
                        }
×
128
                        continue
1✔
129
                } else if err != nil {
2✔
130
                        return err
×
131
                }
×
132
                defer content.Close()
2✔
133
                // Restore current branch to postgres directly
2✔
134
                if branch.Name() == currBranch {
3✔
135
                        if err := diff.BatchExecDDL(ctx, conn, content); err != nil {
1✔
136
                                return err
×
137
                        }
×
138
                        continue
1✔
139
                }
140
                // TODO: restoring non-main branch may break extensions that require postgres
141
                createDb := `CREATE DATABASE "` + branch.Name() + `";`
1✔
142
                if _, err := conn.Exec(ctx, createDb); err != nil {
2✔
143
                        return err
1✔
144
                }
1✔
145
                // Connect to branch database
146
                branchConn, err := lint.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.Port, branch.Name())
×
147
                if err != nil {
×
148
                        return err
×
149
                }
×
150
                defer branchConn.Close(context.Background())
×
151
                // Restore dump, reporting any error
×
152
                if err := diff.BatchExecDDL(ctx, branchConn, content); err != nil {
×
153
                        return err
×
154
                }
×
155
        }
156
        // Branch is already initialised
157
        if _, err = fsys.Stat(filepath.Join(branchDir, currBranch)); !errors.Is(err, os.ErrNotExist) {
2✔
158
                return err
×
159
        }
×
160

161
        fmt.Fprintln(w, "Setting up initial schema...")
2✔
162
        if err := reset.InitialiseDatabase(ctx, conn, fsys); err != nil {
2✔
163
                return err
×
164
        }
×
165

166
        // Ensure `main` branch exists.
167
        if err := utils.MkdirIfNotExistFS(fsys, utils.MigrationsDir); err != nil {
2✔
168
                return err
×
169
        }
×
170
        return fsys.Mkdir(filepath.Join(branchDir, currBranch), 0755)
2✔
171
}
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