mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
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:
commit
cf3a13d3f5
1 changed files with 23 additions and 2 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue