From 84bf412ec7d0dcc092cffc1e0ce61d0d54f8d413 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Mon, 21 Aug 2023 09:29:39 +0200 Subject: [PATCH] Fix --- cmd/internal/database/postgres/postgres.go | 29 ++++++++-------------- cmd/internal/initializer/initializer.go | 10 ++++---- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/cmd/internal/database/postgres/postgres.go b/cmd/internal/database/postgres/postgres.go index ea67ac1..48d434f 100644 --- a/cmd/internal/database/postgres/postgres.go +++ b/cmd/internal/database/postgres/postgres.go @@ -220,7 +220,7 @@ func (db *Postgres) Upgrade() error { // Check if old pg binaries are present and match pgVersion oldPGConfigCmd := path.Join(oldPostgresBinDir, postgresConfigCmd) if _, err := os.Stat(oldPGConfigCmd); errors.Is(err, fs.ErrNotExist) { - db.log.Infow("pg_config of old version not present, skipping upgrade") + db.log.Infow("old pg binaries are not present, skipping upgrade") return nil } @@ -238,15 +238,6 @@ func (db *Postgres) Upgrade() error { // OK we need to upgrade the database in place, maybe taking a backup before is recommended db.log.Infow("start upgrading from", "old database", pgVersion, "old binary", oldBinaryVersionMajor, "new binary", binaryVersionMajor) - // Take a backup - // masterdata-db-0 backup-restore-sidecar {"level":"info","timestamp":"2023-08-20T12:10:44Z","logger":"postgres","caller":"postgres/postgres.go:240","msg":"creating a backup before upgrading failed, skipping upgrade","error":"error running backup command: pg_basebackup: error: connection to server at \"127.0.0.1\", port - // 5432 failed: Connection refused\n\tIs the server running on that host and accepting TCP/IP connections? exit status 1"} - // err = db.Backup() - // if err != nil { - // db.log.Infow("creating a backup before upgrading failed, skipping upgrade", "error", err) - // return nil - // } - // run the pg_upgrade command as postgres user pgUser, err := user.Lookup("postgres") if err != nil { @@ -256,10 +247,6 @@ func (db *Postgres) Upgrade() error { if err != nil { return err } - err = syscall.Setuid(uid) - if err != nil { - return err - } // remove /data/postgres-new if present newDataDirTemp := path.Join("/data", "postgres-new") @@ -273,9 +260,12 @@ func (db *Postgres) Upgrade() error { cmd := exec.Command(postgresInitDBCmd, "-D", newDataDirTemp) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + cmd.SysProcAttr = &syscall.SysProcAttr{ + Credential: &syscall.Credential{Uid: uint32(uid)}, + } err = cmd.Run() if err != nil { - db.log.Infow("unable to run initdb on new new datadir, skipping upgrade", "error", err) + db.log.Errorw("unable to run initdb on new new datadir, skipping upgrade", "error", err) return nil } db.log.Infow("new database directory initialized") @@ -307,11 +297,14 @@ func (db *Postgres) Upgrade() error { cmd = exec.Command(postgresUpgradeCmd, pgUpgradeArgs...) //nolint:gosec cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + cmd.SysProcAttr = &syscall.SysProcAttr{ + Credential: &syscall.Credential{Uid: uint32(uid)}, + } cmd.Dir = "/data" err = cmd.Run() if err != nil { - db.log.Infow("unable to run pg_upgrade on new new datadir, abort upgrade", "error", err) - return nil + db.log.Errorw("unable to run pg_upgrade on new new datadir, abort upgrade", "error", err) + return fmt.Errorf("unable to run pg_upgrade %w", err) } db.log.Infow("pg_upgrade done") @@ -323,7 +316,7 @@ func (db *Postgres) Upgrade() error { err = os.Rename(newDataDirTemp, db.datadir) if err != nil { - return fmt.Errorf("unable to rename upgraded datadir to destination error %w", err) + return fmt.Errorf("unable to rename upgraded datadir to destination, a full restore is required, error %w", err) } db.log.Infow("pg_upgrade done and new data in place", "took", time.Since(start)) diff --git a/cmd/internal/initializer/initializer.go b/cmd/internal/initializer/initializer.go index a5055b0..32a51b1 100644 --- a/cmd/internal/initializer/initializer.go +++ b/cmd/internal/initializer/initializer.go @@ -115,11 +115,6 @@ func (i *Initializer) initialize() error { return fmt.Errorf("unable to check data of database: %w", err) } - err = i.db.Upgrade() - if err != nil { - return err - } - if !needsBackup { i.log.Info("database does not need to be restored") return nil @@ -143,6 +138,11 @@ func (i *Initializer) initialize() error { return fmt.Errorf("unable to restore database: %w", err) } + err = i.db.Upgrade() + if err != nil { + return err + } + return nil }