Skip to content

Commit

Permalink
Require juno version to be compatible with db (#1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored Jan 12, 2024
1 parent 2af3b3b commit f561ae5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ func migrateIfNeeded(ctx context.Context, targetDB db.DB, network utils.Network,
return err
}

for i := metadata.Version; i < uint64(len(migrations)); i++ {
currentVersion := uint64(len(migrations))
if metadata.Version > currentVersion {
return errors.New("db is from a newer, incompatible version of Juno; upgrade to use this database")
}

for i := metadata.Version; i < currentVersion; i++ {
if err = ctx.Err(); err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions migration/migration_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,23 @@ func TestMigrateIfNeededInternal(t *testing.T) {
}
require.ErrorContains(t, migrateIfNeeded(context.Background(), testDB, utils.Mainnet, utils.NewNopZapLogger(), migrations), "foo")
})

t.Run("error if using new db on old version of juno", func(t *testing.T) {
testDB := pebble.NewMemTest(t)
migrations := []Migration{
testMigration{
exec: func(context.Context, db.Transaction, utils.Network) ([]byte, error) {
return nil, nil
},
before: func([]byte) error {
return nil
},
},
}
require.NoError(t, migrateIfNeeded(context.Background(), testDB, utils.Mainnet, utils.NewNopZapLogger(), migrations))
want := "db is from a newer, incompatible version of Juno"
require.ErrorContains(t, migrateIfNeeded(context.Background(), testDB, utils.Mainnet, utils.NewNopZapLogger(), []Migration{}), want)
})
}

func TestChangeStateDiffStructEmptyDB(t *testing.T) {
Expand Down

0 comments on commit f561ae5

Please sign in to comment.