Skip to content

Commit

Permalink
Complete upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Aug 20, 2023
1 parent 47f371b commit 3aa09bc
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion cmd/internal/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (

postgresConfigCmd = "pg_config"
postgresUpgradeCmd = "pg_upgrade"
postgresInitDBCmd = "initdb"
postgresVersionFile = "PG_VERSION"
oldPostgresBinDir = "/usr/local/bin/pg-old"
)
Expand Down Expand Up @@ -239,19 +240,46 @@ func (db *Postgres) Upgrade() error {
// run the pg_upgrade command

// mkdir /data/postgres-new
newDataDirTemp := path.Join("/data", "postgres-new")
err = os.MkdirAll(newDataDirTemp, os.ModeDir)
if err != nil {
db.log.Infow("unable to create new datadir, skipping upgrade", "error", err)
return nil
}
// initdb -D /data/postgres-new
cmd := exec.Command(postgresInitDBCmd, newDataDirTemp)
out, err := cmd.CombinedOutput()
if err != nil {
db.log.Infow("unable to run initdb on new new datadir, skipping upgrade", "error", err)
return nil
}
db.log.Infow("new database director initialized", "output", string(out))

// pg_upgrade \
// --old-datadir /data/postgres \
// --new-datadir /data/postgres \
// --old-bindir /usr/local/bin/pg-old \
// --new-bindir /usr/local/bin \
// --link
cmd = exec.Command(postgresUpgradeCmd, "--old-datadir", db.datadir, "--new-datadir", newDataDirTemp, "--old-bindir", oldPostgresBinDir, "--new-bindir", "/usr/local/bin", "--link") //nolint:gosec
out, err = cmd.CombinedOutput()
if err != nil {
db.log.Infow("unable to run pg_upgrade on new new datadir, abort upgrade", "error", err)
return nil
}
db.log.Infow("pg_ugrade done", "output", string(out))

// rm -rf /data/postgres
err = os.RemoveAll(db.datadir)
if err != nil {
return fmt.Errorf("unable to remove old datadir %w", err)
}
// mv /data/postgres-new /data/postgres
cmd = exec.Command("cp", "-a", path.Join(newDataDirTemp, "*", db.datadir)) //nolint:gosec
out, err = cmd.CombinedOutput()
db.log.Infow("pg_ugrade done and new data in place", "output", string(out))

return nil
return err
}

func (db *Postgres) getBinaryVersion(pgConfigCmd string) (int, error) {
Expand Down

0 comments on commit 3aa09bc

Please sign in to comment.