Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4885ecc

Browse files
authored
fix: Allow dumping db with pg_dump, utilize make cache (#4964)
1 parent 18a97c6 commit 4885ecc

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

coderd/database/gen/dump/main.go

Lines changed: 32 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,23 @@ 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+
out, err := exec.Command("pg_dump", "--version").Output()
40+
if err == nil {
41+
// Parse output:
42+
// pg_dump (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
43+
parts := strings.Split(string(out), " ")
44+
if len(parts) > 2 {
45+
version, err := strconv.Atoi(strings.Split(parts[2], ".")[0])
46+
if err == nil && version >= minimumPostgreSQLVersion {
47+
hasPGDump = true
48+
}
49+
}
50+
}
51+
}
52+
53+
cmdArgs := []string{
3954
"pg_dump",
4055
"--schema-only",
4156
connection,
@@ -45,8 +60,18 @@ func main() {
4560
// We never want to manually generate
4661
// queries executing against this table.
4762
"--exclude-table=schema_migrations",
48-
)
63+
}
4964

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

coderd/database/generate.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
1313
(
1414
cd "$SCRIPT_DIR"
1515

16-
# Dump the updated schema.
17-
go run gen/dump/main.go
16+
echo generate 1>&2
17+
18+
# Dump the updated schema (use make to utilize caching).
19+
make -C ../.. --no-print-directory coderd/database/dump.sql
1820
# The logic below depends on the exact version being correct :(
1921
go run github.com/kyleconroy/sqlc/cmd/[email protected] generate
2022

0 commit comments

Comments
 (0)