Skip to content

Commit b4e59bb

Browse files
committed
migrate snapshot v0 to v1 on load
1 parent d496ef3 commit b4e59bb

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

local/snapshot.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
164164
if err := os.MkdirAll(snapshotDir, os.ModePerm); err != nil {
165165
return "", err
166166
}
167-
// save db
167+
// save data dir
168168
for _, nodeConfig := range nodesConfig {
169169
sourceDataDir, ok := nodesDataDir[nodeConfig.Name]
170170
if !ok {
171-
return "", fmt.Errorf("failure obtaining datadir path for node %q", nodeConfig.Name)
171+
return "", fmt.Errorf("failure obtaining data dir path for node %q", nodeConfig.Name)
172172
}
173173
targetDataDir := filepath.Join(snapshotDir, nodeConfig.Name)
174174
if err := dircopy.Copy(sourceDataDir, targetDataDir); err != nil {
@@ -229,7 +229,6 @@ func (ln *localNetwork) loadSnapshot(
229229
defer ln.lock.Unlock()
230230

231231
snapshotDir := filepath.Join(ln.snapshotsDir, snapshotPrefix+snapshotName)
232-
snapshotDBDir := filepath.Join(snapshotDir, defaultDBSubdir)
233232
_, err := os.Stat(snapshotDir)
234233
if err != nil {
235234
if errors.Is(err, os.ErrNotExist) {
@@ -262,9 +261,30 @@ func (ln *localNetwork) loadSnapshot(
262261
networkConfig.NodeConfigs[i].Flags[k] = v
263262
}
264263
}
265-
// load db
264+
// auto migrate v0 to v1
265+
v0SnapshotDBDir := filepath.Join(snapshotDir, defaultDBSubdir)
266+
if isV0, err := utils.PathExists(v0SnapshotDBDir); err != nil {
267+
return err
268+
} else if isV0 {
269+
for _, nodeConfig := range networkConfig.NodeConfigs {
270+
sourceDBDir := filepath.Join(v0SnapshotDBDir, nodeConfig.Name)
271+
targetDataDir := filepath.Join(snapshotDir, nodeConfig.Name)
272+
if err := os.MkdirAll(targetDataDir, os.ModePerm); err != nil {
273+
return err
274+
}
275+
targetDBDir := filepath.Join(targetDataDir, defaultDBSubdir)
276+
if err := os.Rename(sourceDBDir, targetDBDir); err != nil {
277+
return err
278+
}
279+
}
280+
if err := os.RemoveAll(v0SnapshotDBDir); err != nil {
281+
return err
282+
}
283+
}
284+
return fmt.Errorf("PEPE")
285+
// load data dir
266286
for _, nodeConfig := range networkConfig.NodeConfigs {
267-
sourceDBDir := filepath.Join(snapshotDBDir, nodeConfig.Name)
287+
sourceDBDir := filepath.Join(v0SnapshotDBDir, nodeConfig.Name)
268288
targetDBDir := filepath.Join(filepath.Join(ln.rootDir, nodeConfig.Name), defaultDBSubdir)
269289
if err := dircopy.Copy(sourceDBDir, targetDBDir); err != nil {
270290
return fmt.Errorf("failure loading node %q db dir: %w", nodeConfig.Name, err)

0 commit comments

Comments
 (0)