Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Allow dumping database without docker using pg_dump binary
  • Loading branch information
mafredri committed Nov 9, 2022
commit e8bded4e6abcd2b6e4c05a331f867dfe50486a89
40 changes: 33 additions & 7 deletions coderd/database/gen/dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/coder/coder/coderd/database/migrations"
"github.com/coder/coder/coderd/database/postgres"
)

const minimumPostgreSQLVersion = 13

func main() {
connection, closeFn, err := postgres.Open()
if err != nil {
Expand All @@ -30,12 +34,24 @@ func main() {
panic(err)
}

cmd := exec.Command(
"docker",
"run",
"--rm",
"--network=host",
"postgres:13",
hasPGDump := false
if _, err = exec.LookPath("pg_dump"); err == nil {
fmt.Fprintln(os.Stderr, "yahhoo")
out, err := exec.Command("pg_dump", "--version").Output()
if err == nil {
// Parse output:
// pg_dump (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
parts := strings.Split(string(out), " ")
if len(parts) > 2 {
version, err := strconv.Atoi(strings.Split(parts[2], ".")[0])
if err == nil && version >= minimumPostgreSQLVersion {
hasPGDump = true
}
}
}
}

cmdArgs := []string{
"pg_dump",
"--schema-only",
connection,
Expand All @@ -45,8 +61,18 @@ func main() {
// We never want to manually generate
// queries executing against this table.
"--exclude-table=schema_migrations",
)
}

if !hasPGDump {
cmdArgs = append([]string{
"docker",
"run",
"--rm",
"--network=host",
fmt.Sprintf("postgres:%d", minimumPostgreSQLVersion),
}, cmdArgs...)
}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec
cmd.Env = append(os.Environ(), []string{
"PGTZ=UTC",
"PGCLIENTENCODING=UTF8",
Expand Down