From 44ed1e69c2235a0b66ddca449973b04c7300e2b3 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Mon, 21 Aug 2023 15:32:28 +0200 Subject: [PATCH] copy over existing postgres binaries with major version specified --- cmd/internal/database/postgres/upgrade.go | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cmd/internal/database/postgres/upgrade.go b/cmd/internal/database/postgres/upgrade.go index 19c0fcf..61da67d 100644 --- a/cmd/internal/database/postgres/upgrade.go +++ b/cmd/internal/database/postgres/upgrade.go @@ -37,6 +37,12 @@ var ( // Once the upgrade was made, any error condition will require to recover the database from backup. func (db *Postgres) Upgrade() error { start := time.Now() + + err := db.copyPostgresBinaries() + if err != nil { + return err + } + // First check if there are data already present pgVersionFile := path.Join(db.datadir, postgresVersionFile) if _, err := os.Stat(pgVersionFile); errors.Is(err, fs.ErrNotExist) { @@ -236,3 +242,31 @@ func (db *Postgres) getBindir(pgConfigCmd string) (string, error) { } return strings.TrimSpace(string(out)), nil } + +func (db *Postgres) copyPostgresBinaries() error { + bindir, err := db.getBindir(postgresConfigCmd) + if err != nil { + return err + } + + version, err := db.getBinaryVersion(postgresConfigCmd) + if err != nil { + return err + } + + pgbindir := path.Join(db.datadir, fmt.Sprintf("pg-bin-v%d", version)) + + err = os.RemoveAll(pgbindir) + if err != nil { + return fmt.Errorf("unable to remove old pgbindir %w", err) + } + + copy := exec.Command("cp", "-a", bindir, pgbindir) + copy.Stdout = os.Stdout + copy.Stderr = os.Stderr + err = copy.Run() + if err != nil { + return fmt.Errorf("unable to copy pgbindir %w", err) + } + return nil +}