-
Notifications
You must be signed in to change notification settings - Fork 568
/
Copy pathpgutil.go
146 lines (132 loc) · 3.16 KB
/
pgutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package pgutil
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
)
func DumpSchema(options ConnectionOptions, clean bool) (string, error) {
var b bytes.Buffer
if err := dump(&b, options); err != nil {
return "", err
}
if clean {
return cleanup(&b)
}
return b.String(), nil
}
var matchIgnorePrefix = []string{
"--",
"COMMENT ON",
"REVOKE",
"GRANT",
"SET",
"ALTER DEFAULT PRIVILEGES",
}
var matchIgnoreContains = []string{
"ALTER DEFAULT PRIVILEGES",
"OWNER TO",
}
func ignore(input string) bool {
for _, v := range matchIgnorePrefix {
if strings.HasPrefix(input, v) {
return true
}
}
for _, v := range matchIgnoreContains {
if strings.Contains(input, v) {
return true
}
}
return false
}
var reAdjacentEmptyLines = regexp.MustCompile(`(?m)(\n){3,}`)
func cleanup(r io.Reader) (string, error) {
var b strings.Builder
sc := bufio.NewScanner(r)
for sc.Scan() {
line := sc.Text()
if ignore(line) {
continue
}
if _, err := b.WriteString(line + "\n"); err != nil {
return "", fmt.Errorf("failed to write output: %w", err)
}
}
if err := sc.Err(); err != nil {
return "", err
}
result := reAdjacentEmptyLines.ReplaceAllString(b.String(), "\n\n")
return "\n" + strings.TrimSpace(result) + "\n", nil
}
func dump(w io.Writer, options ConnectionOptions) error {
pgDumpPath, err := exec.LookPath("pg_dump")
if err != nil && !errors.Is(err, exec.ErrNotFound) {
return err
}
if pgDumpPath != "" {
return runPgDump(w, pgDumpPath, options)
}
dockerPath, err := exec.LookPath("docker")
if err != nil && !errors.Is(err, exec.ErrNotFound) {
return err
}
if dockerPath != "" {
return runDockerPgDump(dockerPath)
}
return fmt.Errorf("failed to find at least one exectuable: pg_dump, docker")
}
type ConnectionOptions struct {
DBname string
Username string
Host string
Port string
Password string
}
func NewConnectionOptions(raw string) (ConnectionOptions, error) {
var opt ConnectionOptions
u, err := url.Parse(raw)
if err != nil {
return opt, err
}
if u.User == nil {
return opt, fmt.Errorf("invalid postgres connection string: missing username and password information")
}
// TODO(mf): we could ask the user for password with a prompt here.
pass, ok := u.User.Password()
if !ok {
return opt, fmt.Errorf("invalid postgres connection string: missing password information")
}
opt.Username = u.User.Username()
opt.Password = pass
opt.Host = u.Hostname()
opt.Port = u.Port()
opt.DBname = strings.TrimPrefix(u.Path, "/")
// TODO(mf): What about u.RawQuery to get at options after the ? .."sslmode=disable"
return opt, nil
}
func (c ConnectionOptions) Args() []string {
return []string{
"--dbname=" + c.DBname,
"--host=" + c.Host,
"--port=" + c.Port,
"--username=" + c.Username,
}
}
func runPgDump(w io.Writer, pgDumpPath string, connOptions ConnectionOptions) error {
args := append(connOptions.Args(), "--schema-only")
cmd := exec.Command(pgDumpPath, args...)
cmd.Env = append(cmd.Env, "PGPASSWORD="+connOptions.Password)
cmd.Stdout = w
cmd.Stderr = os.Stderr
return cmd.Run()
}
func runDockerPgDump(dockerPath string) error {
return errors.New("unimplemented")
}