-
Notifications
You must be signed in to change notification settings - Fork 912
/
migration.go
77 lines (63 loc) · 1.87 KB
/
migration.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
package customcommands
import (
"context"
"strconv"
"strings"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/common"
"github.com/mediocregopher/radix"
"github.com/volatiletech/sqlboiler/boil"
)
// contains stuff for migrating from redis to postgres based configs
func migrateFromRedis() {
common.RedisPool.Do(radix.WithConn("custom_commands:", func(conn radix.Conn) error {
scanner := radix.NewScanner(conn, radix.ScanOpts{
Command: "scan",
Pattern: "custom_commands:*",
})
// san over all the keys
var key string
for scanner.Next(&key) {
// retrieve the guild id from the key
split := strings.SplitN(key, ":", 2)
guildID, err := strconv.ParseInt(split[1], 10, 64)
if err != nil {
logger.WithError(err).WithField("str", key).Error("custom commands: failed migrating from redis, key is invalid")
continue
}
// perform the migration
err = migrateGuildConfig(conn, guildID)
if err != nil {
logger.WithError(err).WithField("str", key).Error("custom commands: failed migrating from redis")
continue
}
}
if err := scanner.Close(); err != nil {
logger.WithError(err).Error("failed scanning keys while migrating custom commands")
return err
}
return nil
}))
}
func migrateGuildConfig(rc radix.Client, guildID int64) error {
commands, _, err := LegacyGetCommands(guildID)
if err != nil || len(commands) < 1 {
return err
}
for _, cmd := range commands {
localID, err := common.GenLocalIncrID(guildID, "custom_command")
if err != nil {
return err
}
pqCommand := cmd.ToDBModel()
pqCommand.GuildID = guildID
pqCommand.LocalID = localID
err = pqCommand.InsertG(context.Background(), boil.Infer())
if err != nil {
return err
}
}
err = rc.Do(retryableredis.Cmd(nil, "DEL", KeyCommands(guildID)))
logger.Println("migrated ", len(commands), " custom commands from ", guildID)
return err
}