-
Notifications
You must be signed in to change notification settings - Fork 927
/
migrate_to_psql.go
174 lines (147 loc) · 4.24 KB
/
migrate_to_psql.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package reddit
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/reddit/models"
"github.com/mediocregopher/radix/v3"
"github.com/volatiletech/sqlboiler/boil"
)
// migrateLegacyRedisFormatToPostgres migrates all feeds from all servers to postgres from the old legacy redis format
func migrateLegacyRedisFormatToPostgres() {
common.RedisPool.Do(radix.WithConn("guild_subreddit_watch:", func(conn radix.Conn) error {
scanner := radix.NewScanner(conn, radix.ScanOpts{
Command: "scan",
Pattern: "guild_subreddit_watch:*",
})
// 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("reddit: failed migrating from redis, key is invalid")
continue
}
// perform the migration
err = migrateGuildConfig(guildID)
if err != nil {
logger.WithError(err).WithField("str", key).Error("reddit: failed migrating from redis")
continue
}
logger.Info("migrating reddit config for ", guildID)
}
if err := scanner.Close(); err != nil {
logger.WithError(err).Error("failed scanning keys while migrating reddit")
return err
}
return nil
}))
}
func migrateGuildConfig(guildID int64) error {
key := "guild_subreddit_watch:" + strconv.FormatInt(guildID, 10)
config, err := GetLegacyConfig(key)
if err != nil {
return errors.WrapIff(err, "[%d].getconfig", guildID)
}
tx, err := common.PQ.Begin()
if err != nil {
return errors.WrapIf(err, "pq.begin")
}
// create a new row for each feed
for _, item := range config {
cID, _ := strconv.ParseInt(item.Channel, 10, 64)
if cID == 0 {
continue
}
m := &models.RedditFeed{
GuildID: guildID,
ChannelID: cID,
Subreddit: strings.ToLower(item.Sub),
UseEmbeds: item.UseEmbeds,
}
err := m.Insert(context.Background(), tx, boil.Infer())
if err != nil {
tx.Rollback()
return errors.WrapIff(err, "[%d:%d].insert", guildID, item.ID)
}
}
err = tx.Commit()
if err != nil {
return errors.WrapIff(err, "[%d].commit", guildID)
}
err = common.RedisPool.Do(radix.Cmd(nil, "RENAME", key, "backup_"+key))
if err != nil {
return errors.WrapIff(err, "[%d].rename", guildID)
}
return nil
}
type LegacySubredditWatchItem struct {
Sub string `json:"sub"`
Guild string `json:"guild"`
Channel string `json:"channel"`
ID int `json:"id"`
UseEmbeds bool `json:"use_embeds"`
}
func FindLegacyWatchItem(source []*LegacySubredditWatchItem, id int) *LegacySubredditWatchItem {
for _, c := range source {
if c.ID == id {
return c
}
}
return nil
}
func (item *LegacySubredditWatchItem) Set() error {
serialized, err := json.Marshal(item)
if err != nil {
return err
}
guild := item.Guild
err = common.RedisPool.Do(radix.Pipeline(
radix.FlatCmd(nil, "HSET", "guild_subreddit_watch:"+guild, item.ID, serialized),
radix.FlatCmd(nil, "HSET", "global_subreddit_watch:"+strings.ToLower(item.Sub), fmt.Sprintf("%s:%d", guild, item.ID), serialized),
))
return err
}
func (item *LegacySubredditWatchItem) Remove() error {
guild := item.Guild
err := common.RedisPool.Do(radix.Pipeline(
radix.FlatCmd(nil, "HDEL", "guild_subreddit_watch:"+guild, item.ID),
radix.FlatCmd(nil, "HDEL", "global_subreddit_watch:"+strings.ToLower(item.Sub), fmt.Sprintf("%s:%d", guild, item.ID)),
))
return err
}
func GetLegacyConfig(key string) ([]*LegacySubredditWatchItem, error) {
var rawItems map[string]string
err := common.RedisPool.Do(radix.Cmd(&rawItems, "HGETALL", key))
if err != nil {
return nil, err
}
out := make([]*LegacySubredditWatchItem, len(rawItems))
i := 0
for k, raw := range rawItems {
var decoded *LegacySubredditWatchItem
err := json.Unmarshal([]byte(raw), &decoded)
if err != nil {
return nil, err
}
if err != nil {
id, _ := strconv.ParseInt(k, 10, 32)
out[i] = &LegacySubredditWatchItem{
Sub: "ERROR",
Channel: "ERROR DECODING",
ID: int(id),
}
logger.WithError(err).Error("Failed decoding reddit watch item")
} else {
out[i] = decoded
}
i++
}
return out, nil
}