Merge pull request #295 from Icinga/feature/chk-db-schema

Assert the database schema of the expected version being present
This commit is contained in:
Eric Lippmann 2021-07-13 15:18:17 +02:00 committed by GitHub
commit cf3a13d3f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,8 +22,9 @@ import (
)
const (
ExitSuccess = 0
ExitFailure = 1
ExitSuccess = 0
ExitFailure = 1
expectedSchemaVersion = 2
)
func main() {
@ -48,6 +49,10 @@ func run() int {
}
}
if err := checkDbSchema(context.Background(), db); err != nil {
logger.Fatalf("%+v", err)
}
rc := cmd.Redis()
{
logger.Info("Connecting to Redis")
@ -241,3 +246,19 @@ func run() int {
cancelHactx()
}
}
// checkDbSchema asserts the database schema of the expected version being present.
func checkDbSchema(ctx context.Context, db *icingadb.DB) error {
var version uint16
err := db.QueryRowxContext(ctx, "SELECT version FROM icingadb_schema ORDER BY id DESC LIMIT 1").Scan(&version)
if err != nil {
return errors.Wrap(err, "can't check database schema version")
}
if version != expectedSchemaVersion {
return errors.Errorf("expected database schema v%d, got v%d", expectedSchemaVersion, version)
}
return nil
}