-
Notifications
You must be signed in to change notification settings - Fork 0
/
phrases.go
90 lines (73 loc) · 1.93 KB
/
phrases.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"github.com/belak/nut"
"xorm.io/xorm"
seabird "github.com/belak/go-seabird"
)
// Phrase is the v1 xorm model for phrases
type Phrase struct {
ID int64
Name string `xorm:"index"`
Value string
Submitter string
Deleted bool
}
// phraseBucket is the old nut.DB phrase store
type phraseBucket struct {
Key string
Entries []struct {
Value string
Submitter string
Deleted bool
}
}
func migratePhrases(b *seabird.Bot, ndb *nut.DB, xdb *xorm.Engine) error {
l := seabird.CtxLogger(b.Context(), "migrate")
err := xdb.Sync(Phrase{})
if err != nil {
return err
}
rowCount, err := xdb.Count(Phrase{})
if err != nil {
return err
}
if rowCount != 0 {
l.Warn("Skipping phrases migration because target table is non-empty")
return nil
}
l.Info("Migrating phrases from nut to xorm")
// This is a bit gross, but it's the simplest way to get a transaction for both nut and xorm.
l.Info("Migrating phrases from nut to xorm")
// This is a bit gross, but it's the simplest way to get a transaction for both nut and xorm.
return ndb.View(func(tx *nut.Tx) error {
_, innerErr := xdb.Transaction(func(s *xorm.Session) (interface{}, error) {
bucket := tx.Bucket("phrases")
if bucket == nil {
l.Info("Skipping phrases migration because of missing bucket")
return nil, nil
}
data := &phraseBucket{}
c := bucket.Cursor()
for k, e := c.First(&data); e == nil; k, e = c.Next(&data) {
l.Infof("Migrating phrase entry for %s", data.Key)
if data.Key != k {
l.Warnf("Phrase name (%s) does not match key (%s)", data.Key, k)
}
for _, entry := range data.Entries {
phrase := Phrase{
Name: data.Key,
Value: entry.Value,
Submitter: entry.Submitter,
Deleted: entry.Deleted,
}
_, err = s.InsertOne(phrase)
if err != nil {
return nil, err
}
}
}
return nil, err
})
return innerErr
})
}