From 7ff327f826615d4aa77fb4fddf3f899a0eadd183 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Sat, 19 Aug 2023 22:13:11 +0200 Subject: [PATCH] More checks, take a backup before upgrade --- cmd/internal/database/postgres/postgres.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/internal/database/postgres/postgres.go b/cmd/internal/database/postgres/postgres.go index 5f80091..c07474a 100644 --- a/cmd/internal/database/postgres/postgres.go +++ b/cmd/internal/database/postgres/postgres.go @@ -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" ) @@ -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 @@ -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 }