Skip to content

Commit cf3a13d

Browse files
authored
Merge pull request #295 from Icinga/feature/chk-db-schema
Assert the database schema of the expected version being present
2 parents 320fcd8 + 6a17911 commit cf3a13d

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

cmd/icingadb/main.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222
)
2323

2424
const (
25-
ExitSuccess = 0
26-
ExitFailure = 1
25+
ExitSuccess = 0
26+
ExitFailure = 1
27+
expectedSchemaVersion = 2
2728
)
2829

2930
func main() {
@@ -48,6 +49,10 @@ func run() int {
4849
}
4950
}
5051

52+
if err := checkDbSchema(context.Background(), db); err != nil {
53+
logger.Fatalf("%+v", err)
54+
}
55+
5156
rc := cmd.Redis()
5257
{
5358
logger.Info("Connecting to Redis")
@@ -241,3 +246,19 @@ func run() int {
241246
cancelHactx()
242247
}
243248
}
249+
250+
// checkDbSchema asserts the database schema of the expected version being present.
251+
func checkDbSchema(ctx context.Context, db *icingadb.DB) error {
252+
var version uint16
253+
254+
err := db.QueryRowxContext(ctx, "SELECT version FROM icingadb_schema ORDER BY id DESC LIMIT 1").Scan(&version)
255+
if err != nil {
256+
return errors.Wrap(err, "can't check database schema version")
257+
}
258+
259+
if version != expectedSchemaVersion {
260+
return errors.Errorf("expected database schema v%d, got v%d", expectedSchemaVersion, version)
261+
}
262+
263+
return nil
264+
}

0 commit comments

Comments
 (0)