Skip to content

Commit e8bded4

Browse files
committed
fix: Allow dumping database without docker using pg_dump binary
1 parent d82364b commit e8bded4

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

coderd/database/gen/dump/main.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"runtime"
11+
"strconv"
12+
"strings"
1113

1214
"github.com/coder/coder/coderd/database/migrations"
1315
"github.com/coder/coder/coderd/database/postgres"
1416
)
1517

18+
const minimumPostgreSQLVersion = 13
19+
1620
func main() {
1721
connection, closeFn, err := postgres.Open()
1822
if err != nil {
@@ -30,12 +34,24 @@ func main() {
3034
panic(err)
3135
}
3236

33-
cmd := exec.Command(
34-
"docker",
35-
"run",
36-
"--rm",
37-
"--network=host",
38-
"postgres:13",
37+
hasPGDump := false
38+
if _, err = exec.LookPath("pg_dump"); err == nil {
39+
fmt.Fprintln(os.Stderr, "yahhoo")
40+
out, err := exec.Command("pg_dump", "--version").Output()
41+
if err == nil {
42+
// Parse output:
43+
// pg_dump (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
44+
parts := strings.Split(string(out), " ")
45+
if len(parts) > 2 {
46+
version, err := strconv.Atoi(strings.Split(parts[2], ".")[0])
47+
if err == nil && version >= minimumPostgreSQLVersion {
48+
hasPGDump = true
49+
}
50+
}
51+
}
52+
}
53+
54+
cmdArgs := []string{
3955
"pg_dump",
4056
"--schema-only",
4157
connection,
@@ -45,8 +61,18 @@ func main() {
4561
// We never want to manually generate
4662
// queries executing against this table.
4763
"--exclude-table=schema_migrations",
48-
)
64+
}
4965

66+
if !hasPGDump {
67+
cmdArgs = append([]string{
68+
"docker",
69+
"run",
70+
"--rm",
71+
"--network=host",
72+
fmt.Sprintf("postgres:%d", minimumPostgreSQLVersion),
73+
}, cmdArgs...)
74+
}
75+
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec
5076
cmd.Env = append(os.Environ(), []string{
5177
"PGTZ=UTC",
5278
"PGCLIENTENCODING=UTF8",

0 commit comments

Comments
 (0)