Skip to content

Commit fa67fe9

Browse files
committed
refactor pgDumpVersion to accept other tools
Rename it to pgToolVersion so that is can get the version of pg_dumpall as well. Fix the parsing so that the revision is not used as the min number with version greater that 10. Use the --version long option that is available at least from 9.0.
1 parent b42f06f commit fa67fe9

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

main.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (d *dump) dump() error {
450450
// It is recommended to use --create with the plain format
451451
// from PostgreSQL 11 to get the ACL and configuration of the
452452
// database
453-
if pgDumpVersion() >= 110000 && fileEnd == "sql" {
453+
if pgToolVersion("pg_dump") >= 110000 && fileEnd == "sql" {
454454
args = append(args, "--create")
455455
}
456456

@@ -576,17 +576,33 @@ func formatDumpPath(dir string, timeFormat string, suffix string, dbname string,
576576
return filepath.Join(d, f)
577577
}
578578

579-
func pgDumpVersion() int {
580-
vs, err := exec.Command(filepath.Join(binDir, "pg_dump"), "-V").Output()
579+
func pgToolVersion(tool string) int {
580+
vs, err := exec.Command(filepath.Join(binDir, tool), "--version").Output()
581581
if err != nil {
582-
l.Warnln("failed to retrieve version of pg_dump:", err)
582+
l.Warnf("failed to retrieve version of %s: %s", tool, err)
583583
return 0
584584
}
585585

586-
var maj, min, rev int
587-
fmt.Sscanf(string(vs), "pg_dump (PostgreSQL) %d.%d.%d", &maj, &min, &rev)
586+
var maj, min, rev, numver int
587+
n, _ := fmt.Sscanf(string(vs), tool+" (PostgreSQL) %d.%d.%d", &maj, &min, &rev)
588588

589-
return (maj*100+min)*100 + rev
589+
if n == 3 {
590+
// Before PostgreSQL 10, the format si MAJ.MIN.REV
591+
numver = (maj*100+min)*100 + rev
592+
} else if n == 2 {
593+
// From PostgreSQL 10, the format si MAJ.REV, so the rev ends
594+
// up in min with the scan
595+
numver = maj*10000 + min
596+
} else {
597+
// We have the special case of the development version, where the
598+
// format is MAJdevel
599+
fmt.Sscanf(string(vs), tool+" (PostgreSQL) %ddevel", &maj)
600+
numver = maj * 10000
601+
}
602+
603+
l.Verboseln(tool, "version is:", numver)
604+
605+
return numver
590606
}
591607

592608
func dumpGlobals(dir string, timeFormat string, conninfo *ConnInfo, fc chan<- string) error {

sql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func dumpCreateDBAndACL(db *pg, dbname string) (string, error) {
223223
// this is no longer necessary after 11. Dumping ACL is the
224224
// job of pg_dump so we have to check its version, not the
225225
// server
226-
if pgDumpVersion() >= 110000 {
226+
if pgToolVersion("pg_dump") >= 110000 {
227227
l.Verboseln("no need to dump create database query and database ACL with pg_dump from >=11")
228228
return "", nil
229229
}
@@ -400,7 +400,7 @@ func dumpDBConfig(db *pg, dbname string) (string, error) {
400400
// this is no longer necessary after 11. Dumping ACL is the
401401
// job of pg_dump so we have to check its version, not the
402402
// server
403-
if pgDumpVersion() >= 110000 {
403+
if pgToolVersion("pg_dump") >= 110000 {
404404
l.Verboseln("no need to dump database configuration with pg_dump from >=11")
405405
return "", nil
406406
}

sql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func needPgConn(t *testing.T) {
5757
}
5858

5959
func needPgDump(t *testing.T) {
60-
if pgDumpVersion() >= 110000 {
60+
if pgToolVersion("pg_dump") >= 110000 {
6161
t.Skip("testing with a pg_dump version > 11")
6262
}
6363
}

0 commit comments

Comments
 (0)