mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
fix: Allow dumping db with pg_dump, utilize make cache (#4964)
This commit is contained in:
committed by
GitHub
parent
18a97c6f59
commit
4885ecc3ad
@ -8,11 +8,15 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/coder/coder/coderd/database/migrations"
|
"github.com/coder/coder/coderd/database/migrations"
|
||||||
"github.com/coder/coder/coderd/database/postgres"
|
"github.com/coder/coder/coderd/database/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const minimumPostgreSQLVersion = 13
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
connection, closeFn, err := postgres.Open()
|
connection, closeFn, err := postgres.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -30,12 +34,23 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(
|
hasPGDump := false
|
||||||
"docker",
|
if _, err = exec.LookPath("pg_dump"); err == nil {
|
||||||
"run",
|
out, err := exec.Command("pg_dump", "--version").Output()
|
||||||
"--rm",
|
if err == nil {
|
||||||
"--network=host",
|
// Parse output:
|
||||||
"postgres:13",
|
// 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",
|
"pg_dump",
|
||||||
"--schema-only",
|
"--schema-only",
|
||||||
connection,
|
connection,
|
||||||
@ -45,8 +60,18 @@ func main() {
|
|||||||
// We never want to manually generate
|
// We never want to manually generate
|
||||||
// queries executing against this table.
|
// queries executing against this table.
|
||||||
"--exclude-table=schema_migrations",
|
"--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{
|
cmd.Env = append(os.Environ(), []string{
|
||||||
"PGTZ=UTC",
|
"PGTZ=UTC",
|
||||||
"PGCLIENTENCODING=UTF8",
|
"PGCLIENTENCODING=UTF8",
|
||||||
|
@ -13,8 +13,10 @@ SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
|
|||||||
(
|
(
|
||||||
cd "$SCRIPT_DIR"
|
cd "$SCRIPT_DIR"
|
||||||
|
|
||||||
# Dump the updated schema.
|
echo generate 1>&2
|
||||||
go run gen/dump/main.go
|
|
||||||
|
# Dump the updated schema (use make to utilize caching).
|
||||||
|
make -C ../.. --no-print-directory coderd/database/dump.sql
|
||||||
# The logic below depends on the exact version being correct :(
|
# The logic below depends on the exact version being correct :(
|
||||||
go run github.com/kyleconroy/sqlc/cmd/sqlc@v1.13.0 generate
|
go run github.com/kyleconroy/sqlc/cmd/sqlc@v1.13.0 generate
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user