feat: add support for different postgres versions
This commit is contained in:
parent
80d1b33d00
commit
c5f0b46e77
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/docker/docker/api/types"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PostgresDumper struct {
|
||||
|
@ -43,10 +44,23 @@ func (d *PostgresDumper) buildConnStr() string {
|
|||
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db)
|
||||
}
|
||||
|
||||
func findPgVersion(env []string) string {
|
||||
for _, s := range env {
|
||||
if strings.HasPrefix(s, "PG_MAJOR=") {
|
||||
return strings.TrimPrefix(s, "PG_MAJOR=")
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d *PostgresDumper) Dump() error {
|
||||
fmt.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
|
||||
|
||||
connStr := d.buildConnStr()
|
||||
|
||||
return runAndSaveCommand(getDumpFilename(d.Container.Name), "pg_dump", "--dbname", connStr)
|
||||
// The postgres version must match the one the db server is running
|
||||
pgVersion := findPgVersion(d.Container.Config.Env)
|
||||
|
||||
return runAndSaveCommand(getDumpFilename(d.Container.Name), "pg_dump"+pgVersion, "--dbname", connStr)
|
||||
}
|
||||
|
|
|
@ -115,3 +115,28 @@ func TestPostgresDumper_buildConnStr(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindPGVersionFromEnv(t *testing.T) {
|
||||
t.Run("no PG_MAJOR", func(t *testing.T) {
|
||||
pgVersion := findPgVersion([]string{})
|
||||
if pgVersion != "" {
|
||||
t.Errorf("Version is not empty")
|
||||
}
|
||||
})
|
||||
t.Run("pg 14", func(t *testing.T) {
|
||||
pgVersion := findPgVersion([]string{
|
||||
"POSTGRES_PASSWORD=test",
|
||||
"POSTGRES_USER=user",
|
||||
"POSTGRES_DB=test",
|
||||
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/14/bin",
|
||||
"GOSU_VERSION=1.14",
|
||||
"LANG=en_US.utf8",
|
||||
"PG_MAJOR=14",
|
||||
"PG_VERSION=14.1-1.pgdg110+1",
|
||||
"PGDATA=/var/lib/postgresql/data",
|
||||
})
|
||||
if pgVersion != "14" {
|
||||
t.Errorf("Version is not 14")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue