Skip to content

Commit

Permalink
More checks, take a backup before upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Aug 19, 2023
1 parent ece7e93 commit 7ff327f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/internal/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
postgresWalTar = "pg_wal.tar.gz"

postgresConfigCmd = "pg_config"
postgresUpgradeCmd = "pg_upgrade"
postgresVersionFile = "PG_VERSION"
oldPostgresBinDir = "/usr/local/bin/pg-old"
)
Expand Down Expand Up @@ -169,6 +170,12 @@ func (db *Postgres) Upgrade() error {
return nil
}

// Check if pg_upgrade is present
if _, err := os.Stat(postgresUpgradeCmd); errors.Is(err, fs.ErrNotExist) {
db.log.Infow("pg_upgrade is not present, skipping upgrade")
return nil
}

// Then check the version of the existing database
// cat PG_VERSION
// 12
Expand Down Expand Up @@ -214,9 +221,23 @@ func (db *Postgres) Upgrade() error {
return nil
}

if oldBinaryVersionMajor != pgVersion {
db.log.Infow("database version and old binary version do not match, skipping upgrade", "old database", pgVersion, "old binary", oldBinaryVersionMajor)
return nil
}

// 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
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

return nil
}

Expand Down

0 comments on commit 7ff327f

Please sign in to comment.