-
Notifications
You must be signed in to change notification settings - Fork 12
/
00_dbdir.go
67 lines (56 loc) · 1.74 KB
/
00_dbdir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package migrations
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"go.uber.org/zap"
"github.com/spacemeshos/poet/db"
"github.com/spacemeshos/poet/logging"
"github.com/spacemeshos/poet/server"
)
func migrateDbDir(ctx context.Context, cfg *server.Config) error {
if err := migrateProofsDb(ctx, cfg); err != nil {
return fmt.Errorf("migrating proofs DB: %w", err)
}
if err := migrateRoundsDbs(ctx, cfg); err != nil {
return fmt.Errorf("migrating rounds DBs: %w", err)
}
return nil
}
func migrateRoundsDbs(ctx context.Context, cfg *server.Config) error {
roundsDataDir := filepath.Join(cfg.DataDir, "rounds")
// check if dir exists
if _, err := os.Stat(roundsDataDir); os.IsNotExist(err) {
return nil
}
logger := logging.FromContext(ctx)
logger.Info("migrating rounds DBs", zap.String("datadir", cfg.DataDir))
entries, err := os.ReadDir(roundsDataDir)
if err != nil {
return err
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
if _, err := strconv.ParseUint(entry.Name(), 10, 32); err != nil {
return fmt.Errorf("entry is not a uint32 %s", entry.Name())
}
dbdir := filepath.Join(cfg.DbDir, "rounds", entry.Name())
oldDbDir := filepath.Join(roundsDataDir, entry.Name(), "challengesDb")
if err := db.Migrate(ctx, dbdir, oldDbDir); err != nil {
return fmt.Errorf("migrating round DB from %s: %w", oldDbDir, err)
}
}
return nil
}
func migrateProofsDb(ctx context.Context, cfg *server.Config) error {
proofsDbPath := filepath.Join(cfg.DbDir, "proofs")
oldProofsDbPath := filepath.Join(cfg.DataDir, "proofs")
if err := db.Migrate(ctx, proofsDbPath, oldProofsDbPath); err != nil {
return fmt.Errorf("migrating proofs DB %s -> %s: %w", oldProofsDbPath, proofsDbPath, err)
}
return nil
}