-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb_migrations_code.go
More file actions
197 lines (173 loc) · 3.43 KB
/
db_migrations_code.go
File metadata and controls
197 lines (173 loc) · 3.43 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package server
import (
"context"
"crypto/rand"
// "time"
)
// create entries for `network_client.device_id`
func migration_20240124_PopulateDevice(ctx context.Context) {
Tx(ctx, func(tx PgTx) {
result, err := tx.Query(
ctx,
`
SELECT
client_id,
network_id,
description,
device_spec
FROM network_client
WHERE
device_id IS NULL
`,
)
type Device struct {
deviceId Id
networkId Id
deviceName string
deviceSpec string
}
devices := map[Id]*Device{}
WithPgResult(result, err, func() {
for result.Next() {
var clientId Id
device := &Device{
deviceId: NewId(),
}
Raise(result.Scan(
&clientId,
&device.networkId,
&device.deviceName,
&device.deviceSpec,
))
devices[clientId] = device
}
})
createTime := NowUtc()
for clientId, device := range devices {
RaisePgResult(tx.Exec(
ctx,
`
INSERT INTO device (
device_id,
network_id,
device_name,
device_spec,
create_time
) VALUES ($1, $2, $3, $4, $5)
`,
device.deviceId,
device.networkId,
device.deviceName,
device.deviceSpec,
createTime,
))
RaisePgResult(tx.Exec(
ctx,
`
UPDATE network_client
SET
device_id = $2
WHERE
client_id = $1
`,
clientId,
device.deviceId,
))
}
})
}
func migration_20240725_PopulateNetworkReferralCodes(ctx context.Context) {
Tx(ctx, func(tx PgTx) {
result, err := tx.Query(
ctx,
`
SELECT
network_id
FROM network
`,
)
networkIds := []Id{}
WithPgResult(result, err, func() {
for result.Next() {
var networkId Id
Raise(result.Scan(
&networkId,
))
networkIds = append(networkIds, networkId)
}
})
for _, networkId := range networkIds {
code := NewId()
RaisePgResult(tx.Exec(
ctx,
`
INSERT INTO network_referral_code (
network_id,
referral_code
) VALUES ($1, $2)
`,
networkId,
code,
))
}
})
}
func migration_20240802_AccountPaymentPopulateCircleWalletId(ctx context.Context) {
Tx(ctx, func(tx PgTx) {
RaisePgResult(tx.Exec(
ctx,
`
UPDATE account_wallet
SET circle_wallet_id = wallet_id
`,
))
})
}
func migration_20250402_ReferralCodeToAlphaNumeric(ctx context.Context) {
Tx(ctx, func(tx PgTx) {
result, err := tx.Query(
ctx,
`
SELECT network_id FROM network_referral_code
`,
)
networkIds := []Id{}
WithPgResult(result, err, func() {
for result.Next() {
var networkId Id
Raise(result.Scan(
&networkId,
))
networkIds = append(
networkIds,
networkId,
)
}
})
for _, networkId := range networkIds {
code := generateAlphanumericCode(6)
RaisePgResult(tx.Exec(
ctx,
`
UPDATE network_referral_code
SET referral_code = $2
WHERE network_id = $1
`,
networkId,
code,
))
}
})
}
func generateAlphanumericCode(length int) string {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
code := make([]byte, length)
randomBytes := make([]byte, length)
if _, err := rand.Read(randomBytes); err != nil {
panic(err)
}
for i := range randomBytes {
code[i] = charset[randomBytes[i]%byte(len(charset))]
}
return string(code)
}