Skip to content

Commit

Permalink
always create a createdb.sql file for plain format instead of dumping…
Browse files Browse the repository at this point in the history
… with --create
  • Loading branch information
orgrim committed Dec 24, 2021
1 parent a1187d8 commit 4a11694
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Upload files to Azure Blob Storage
* Add a systemd timer for Debian in the package generated by goreleaser
* Check the syntax of the configuration file

* Always create a createdb.sql file for plain format instead of dumping with --create

## pg_back 2.0.1

Expand Down
14 changes: 6 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,12 @@ func run() (retVal error) {
// if it fails once it fails all the time.
if canDumpACL {
l.Verboseln("dumping create database query and ACL of", dbname)
b, err = dumpCreateDBAndACL(db, dbname)
force := false
if d.Options.Format == 'p' {
force = true
}

b, err = dumpCreateDBAndACL(db, dbname, force)
var verr *pgVersionError
if err != nil {
if !errors.As(err, &verr) {
Expand Down Expand Up @@ -566,13 +571,6 @@ func (d *dump) dump(fc chan<- sumFileJob) error {
}
}

// It is recommended to use --create with the plain format
// from PostgreSQL 11 to get the ACL and configuration of the
// database
if d.PgDumpVersion >= 110000 && fileEnd == "sql" {
args = append(args, "--create")
}

// Included and excluded schemas and table
for _, obj := range d.Options.Schemas {
args = append(args, "-n", obj)
Expand Down
7 changes: 4 additions & 3 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,23 @@ func (e *pgVersionError) Error() string {
}

// pg_dumpacl stuff
func dumpCreateDBAndACL(db *pg, dbname string) (string, error) {
func dumpCreateDBAndACL(db *pg, dbname string, force bool) (string, error) {
var s string

if dbname == "" {
return "", fmt.Errorf("empty input dbname")
}

// this query only work from 9.0, where datcollate and datctype were added to pg_database
// this query only work from 9.0, where datcollate and datctype were
// added to pg_database
if db.version < 90000 {
return "", &pgVersionError{s: "cluster version is older than 9.0, not dumping ACL"}
}

// this is no longer necessary after 11. Dumping ACL is the
// job of pg_dump so we have to check its version, not the
// server
if pgToolVersion("pg_dump") >= 110000 {
if pgToolVersion("pg_dump") >= 110000 && !force {
l.Verboseln("no need to dump create database query and database ACL with pg_dump from >=11")
return "", nil
}
Expand Down
2 changes: 1 addition & 1 deletion sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func TestDumpCreateDBAndACL(t *testing.T) {

for _, st := range tests {
t.Run(fmt.Sprintf("%s", st.db), func(t *testing.T) {
got, err := dumpCreateDBAndACL(testdb, st.db)
got, err := dumpCreateDBAndACL(testdb, st.db, false)
if err != nil {
t.Errorf("expected non nil error, got %q", err)
}
Expand Down

0 comments on commit 4a11694

Please sign in to comment.