storagenode: check db version before run
Change-Id: I912f63fd62f2bff10341346c28dfb92fcd683806
This commit is contained in:
parent
139ff3427b
commit
a840cb71e7
@ -186,6 +186,11 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
||||
return errs.New("Error creating tables for master database on storagenode: %+v", err)
|
||||
}
|
||||
|
||||
err = db.CheckVersion(ctx)
|
||||
if err != nil {
|
||||
return errs.New("Error checking version for storagenode database: %+v", err)
|
||||
}
|
||||
|
||||
preflightEnabled, err := cmd.Flags().GetBool("preflight.database-check")
|
||||
if err != nil {
|
||||
return errs.New("Cannot retrieve preflight.database-check flag: %+v", err)
|
||||
|
@ -118,14 +118,23 @@ func (migration *Migration) ValidateSteps() error {
|
||||
|
||||
// ValidateVersions checks that the version of the migration matches the state of the database.
|
||||
func (migration *Migration) ValidateVersions(ctx context.Context, log *zap.Logger) error {
|
||||
if err := migration.ValidateSteps(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
expectedVersions := make(map[tagsql.DB]int)
|
||||
for _, step := range migration.Steps {
|
||||
dbVersion, err := migration.getLatestVersion(ctx, log, step.DB)
|
||||
expectedVersions[step.DB] = step.Version
|
||||
}
|
||||
|
||||
for database, expectedVersion := range expectedVersions {
|
||||
currentVersion, err := migration.CurrentVersion(ctx, log, database)
|
||||
if err != nil {
|
||||
return ErrValidateVersionQuery.Wrap(err)
|
||||
}
|
||||
|
||||
if step.Version > dbVersion {
|
||||
return ErrValidateVersionMismatch.New("expected %d <= %d", step.Version, dbVersion)
|
||||
if expectedVersion != currentVersion {
|
||||
return ErrValidateVersionMismatch.New("expected %d != %d", expectedVersion, currentVersion)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -614,6 +614,11 @@ func (db *DB) migrateToDB(ctx context.Context, dbName string, tablesToKeep ...st
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckVersion that the version of the migration matches the state of the database.
|
||||
func (db *DB) CheckVersion(ctx context.Context) error {
|
||||
return db.Migration(ctx).ValidateVersions(ctx, db.log)
|
||||
}
|
||||
|
||||
// Migration returns table migrations.
|
||||
func (db *DB) Migration(ctx context.Context) *migrate.Migration {
|
||||
return &migrate.Migration{
|
||||
|
Loading…
Reference in New Issue
Block a user