Move database#DB.CheckSchema() to icingadb#CheckSchema()

This commit is contained in:
Eric Lippmann 2024-05-21 11:51:34 +02:00
parent aec0bdada4
commit 490c18fd5f
4 changed files with 60 additions and 49 deletions

View file

@ -79,7 +79,7 @@ func main() {
ido, idb := connectAll(c)
if err := idb.CheckSchema(context.Background()); err != nil {
if err := icingadb.CheckSchema(context.Background(), idb); err != nil {
log.Fatalf("%+v", err)
}

View file

@ -71,7 +71,7 @@ func run() int {
}
}
if err := db.CheckSchema(context.Background()); err != nil {
if err := icingadb.CheckSchema(context.Background(), db); err != nil {
logger.Fatalf("%+v", err)
}

View file

@ -91,53 +91,6 @@ func NewDb(db *sqlx.DB, logger *logging.Logger, options *Options) *DB {
}
}
const (
expectedMysqlSchemaVersion = 5
expectedPostgresSchemaVersion = 3
)
// CheckSchema asserts the database schema of the expected version being present.
func (db *DB) CheckSchema(ctx context.Context) error {
var expectedDbSchemaVersion uint16
switch db.DriverName() {
case MySQL:
expectedDbSchemaVersion = expectedMysqlSchemaVersion
case PostgreSQL:
expectedDbSchemaVersion = expectedPostgresSchemaVersion
}
var version uint16
err := retry.WithBackoff(
ctx,
func(ctx context.Context) (err error) {
query := "SELECT version FROM icingadb_schema ORDER BY id DESC LIMIT 1"
err = db.QueryRowxContext(ctx, query).Scan(&version)
if err != nil {
err = database.CantPerformQuery(err, query)
}
return
},
retry.Retryable,
backoff.NewExponentialWithJitter(128*time.Millisecond, 1*time.Minute),
db.getDefaultRetrySettings())
if err != nil {
return errors.Wrap(err, "can't check database schema version")
}
if version != expectedDbSchemaVersion {
// Since these error messages are trivial and mostly caused by users, we don't need
// to print a stack trace here. However, since errors.Errorf() does this automatically,
// we need to use fmt instead.
return fmt.Errorf(
"unexpected database schema version: v%d (expected v%d), please make sure you have applied all database"+
" migrations after upgrading Icinga DB", version, expectedDbSchemaVersion,
)
}
return nil
}
// BuildColumns returns all columns of the given struct.
func (db *DB) BuildColumns(subject interface{}) []string {
fields := db.Mapper.TypeMap(reflect.TypeOf(subject)).Names

58
pkg/icingadb/schema.go Normal file
View file

@ -0,0 +1,58 @@
package icingadb
import (
"context"
"fmt"
"github.com/icinga/icingadb/pkg/backoff"
"github.com/icinga/icingadb/pkg/database"
"github.com/icinga/icingadb/pkg/retry"
"github.com/pkg/errors"
"time"
)
const (
expectedMysqlSchemaVersion = 5
expectedPostgresSchemaVersion = 3
)
// CheckSchema asserts the database schema of the expected version being present.
func CheckSchema(ctx context.Context, db *DB) error {
var expectedDbSchemaVersion uint16
switch db.DriverName() {
case MySQL:
expectedDbSchemaVersion = expectedMysqlSchemaVersion
case PostgreSQL:
expectedDbSchemaVersion = expectedPostgresSchemaVersion
}
var version uint16
err := retry.WithBackoff(
ctx,
func(ctx context.Context) (err error) {
query := "SELECT version FROM icingadb_schema ORDER BY id DESC LIMIT 1"
err = db.QueryRowxContext(ctx, query).Scan(&version)
if err != nil {
err = database.CantPerformQuery(err, query)
}
return
},
retry.Retryable,
backoff.NewExponentialWithJitter(128*time.Millisecond, 1*time.Minute),
db.getDefaultRetrySettings())
if err != nil {
return errors.Wrap(err, "can't check database schema version")
}
if version != expectedDbSchemaVersion {
// Since these error messages are trivial and mostly caused by users, we don't need
// to print a stack trace here. However, since errors.Errorf() does this automatically,
// we need to use fmt instead.
return fmt.Errorf(
"unexpected database schema version: v%d (expected v%d), please make sure you have applied all database"+
" migrations after upgrading Icinga DB", version, expectedDbSchemaVersion,
)
}
return nil
}