diff --git a/cmd/icingadb-migrate/main.go b/cmd/icingadb-migrate/main.go index 7019d7ed..6d19cb9e 100644 --- a/cmd/icingadb-migrate/main.go +++ b/cmd/icingadb-migrate/main.go @@ -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) } diff --git a/cmd/icingadb/main.go b/cmd/icingadb/main.go index 4e165eb9..84d0efac 100644 --- a/cmd/icingadb/main.go +++ b/cmd/icingadb/main.go @@ -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) } diff --git a/pkg/icingadb/db.go b/pkg/icingadb/db.go index 2be2bc60..7b252078 100644 --- a/pkg/icingadb/db.go +++ b/pkg/icingadb/db.go @@ -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 diff --git a/pkg/icingadb/schema.go b/pkg/icingadb/schema.go new file mode 100644 index 00000000..279feced --- /dev/null +++ b/pkg/icingadb/schema.go @@ -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 +}