Disallow db version downgrade (#3657)

* save

* save
This commit is contained in:
Alex Sharov 2022-03-08 10:02:35 +07:00 committed by GitHub
parent 16cea91422
commit 191a3f9232
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 12 deletions

View File

@ -116,20 +116,10 @@ func (m *Migrator) PendingMigrations(tx kv.Tx) ([]Migration, error) {
return pending, nil return pending, nil
} }
func (m *Migrator) Apply(db kv.RwDB, datadir string) error { func (m *Migrator) VerifyVersion(db kv.RwDB) error {
if len(m.Migrations) == 0 {
return nil
}
var applied map[string][]byte
var existingVersion []byte
if err := db.View(context.Background(), func(tx kv.Tx) error { if err := db.View(context.Background(), func(tx kv.Tx) error {
var err error var err error
applied, err = AppliedMigrations(tx, false) existingVersion, err := tx.GetOne(kv.DatabaseInfo, kv.DBSchemaVersionKey)
if err != nil {
return fmt.Errorf("reading applied migrations: %w", err)
}
existingVersion, err = tx.GetOne(kv.DatabaseInfo, kv.DBSchemaVersionKey)
if err != nil { if err != nil {
return fmt.Errorf("reading DB schema version: %w", err) return fmt.Errorf("reading DB schema version: %w", err)
} }
@ -154,6 +144,19 @@ func (m *Migrator) Apply(db kv.RwDB, datadir string) error {
} }
return nil return nil
}); err != nil { }); err != nil {
return fmt.Errorf("migrator.VerifyVersion: %w", err)
}
return nil
}
func (m *Migrator) Apply(db kv.RwDB, datadir string) error {
if len(m.Migrations) == 0 {
return nil
}
var applied map[string][]byte
if err := m.VerifyVersion(db); err != nil {
return err return err
} }

View File

@ -537,6 +537,10 @@ func OpenDatabase(config *Config, logger log.Logger, label kv.Label) (kv.RwDB, e
return nil, err return nil, err
} }
migrator := migrations.NewMigrator(label) migrator := migrations.NewMigrator(label)
if err := migrator.VerifyVersion(db); err != nil {
return nil, err
}
has, err := migrator.HasPendingMigrations(db) has, err := migrator.HasPendingMigrations(db)
if err != nil { if err != nil {
return nil, err return nil, err