-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathupgrade.go
208 lines (189 loc) · 5.5 KB
/
upgrade.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package upgrademanager
import (
"time"
"github.com/lbryio/chainquery/daemon/jobs"
"github.com/lbryio/chainquery/lbrycrd"
"github.com/lbryio/chainquery/model"
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
const (
appVersion = 13
apiVersion = 13
dataVersion = 13
)
// RunUpgradesForVersion - Migrations are for structure of the data. Upgrade Manager scripts are for the data itself.
// Since it could take hours to rebuild the data chainquery is designed to reprocess data so
// that there is no downtime of data availability since many system will be using it.
func RunUpgradesForVersion() {
var appStatus *model.ApplicationStatus
var err error
if !model.ApplicationStatusExistsGP(1) {
appStatus = &model.ApplicationStatus{AppVersion: appVersion, APIVersion: apiVersion, DataVersion: dataVersion}
if err := appStatus.InsertG(boil.Infer()); err != nil {
err := errors.Prefix("App Status Error", err)
panic(err)
}
} else {
appStatus, err = model.FindApplicationStatusG(1)
if err != nil {
logrus.Error("Application cannot be upgraded: ", err)
return
}
////Run all upgrades and let it determine if they should execute
//
////upgrade123(appStatus.appVersion)
upgradeFrom1(appStatus.AppVersion)
upgradeFrom2(appStatus.AppVersion)
upgradeFrom3(appStatus.AppVersion)
upgradeFrom4(appStatus.AppVersion)
upgradeFrom5(appStatus.AppVersion)
upgradeFrom6(appStatus.AppVersion)
upgradeFrom7(appStatus.AppVersion)
upgradeFrom8(appStatus.AppVersion)
upgradeFrom9(appStatus.AppVersion)
upgradeFrom10(appStatus.AppVersion)
upgradeFrom11(appStatus.AppVersion)
upgradeFrom12(appStatus.AppVersion)
////Increment and save
//
logrus.Debug("Upgrading app status version to App-", appVersion, " Data-", dataVersion, " Api-", apiVersion)
appStatus.AppVersion = appVersion
appStatus.DataVersion = dataVersion
appStatus.APIVersion = apiVersion
}
if err := appStatus.UpdateG(boil.Infer()); err != nil {
err := errors.Prefix("App Status Error", err)
panic(err)
}
logrus.Debug("All necessary upgrades are finished!")
}
//
//func upgradeFrom123(version int){
// util.TimeTrack(time.Now(),"script DoThis","always")
// if version < 123{
// scriptDoThis()
// }
//}
func upgradeFrom1(version int) {
if version < 2 {
logrus.Info("Re-Processing all claim outputs")
reProcessAllClaims()
}
}
func upgradeFrom2(version int) {
if version < 3 {
logrus.Info("Re-Processing all claim outputs")
reProcessAllClaims()
}
}
func upgradeFrom3(version int) {
if version < 4 {
logrus.Info("Re-Processing all claim outputs")
reProcessAllClaims()
}
}
func upgradeFrom4(version int) {
if version < 5 {
logrus.Info("Updating Claims with ClaimAddress")
setClaimAddresses()
}
}
func upgradeFrom5(version int) {
if version < 6 {
logrus.Info("Deleting top 50 blocks to ensure consistency for release")
highestBlock, err := model.Blocks(qm.OrderBy(model.BlockColumns.Height + " DESC")).OneG()
if err != nil {
logrus.Error(err)
return
}
blocks, err := model.Blocks(qm.Where(model.BlockColumns.Height+">= ?", highestBlock.Height-50)).AllG()
if err != nil {
logrus.Error(err)
return
}
if err := blocks.DeleteAllG(); err != nil {
logrus.Error(err)
return
}
}
}
func upgradeFrom6(version int) {
if version < 7 {
logrus.Info("Setting the height of all claims")
setBlockHeightOnAllClaims()
}
}
func upgradeFrom7(version int) {
if version < 8 {
block, err := model.Blocks(qm.OrderBy(model.BlockColumns.Height+" DESC"), qm.Limit(1)).OneG()
if err != nil {
logrus.Error(err)
return
}
if block != nil && block.Height < 400155 {
logrus.Info("Reprocessing all claims equal to or above height 400155")
reProcessAllClaimsFromHeight(400155) // https://github.com/lbryio/lbrycrd/pull/137 - expiration hardfork.
}
}
}
func upgradeFrom8(version int) {
if version < 9 {
logrus.Info("Re-Processing all claim outputs")
reProcessAllClaims()
}
}
func upgradeFrom9(version int) {
if version < 10 {
//Get blockheight for calculating expired status
count, err := lbrycrd.GetBlockCount()
if err != nil {
logrus.Error("Upgrade 9-10: Error getting block height", err)
return
}
expiredClaims, err := model.Claims(qm.Where(model.ClaimColumns.BidState+"=?", "Expired")).AllG()
if err != nil {
logrus.Error("Upgrade 9-10: ", err)
}
for _, expiredClaim := range expiredClaims {
if !jobs.GetIsExpiredAtHeight(expiredClaim.Height, uint(*count)) {
expiredClaim.BidState = "Active" //Will update it to go through the claimtrie sync
expiredClaim.ModifiedAt = time.Now()
err = expiredClaim.UpdateG(boil.Infer())
if err != nil {
logrus.Error("Upgrade 9-10: ", err)
}
}
}
}
}
func upgradeFrom10(version int) {
if version < 10 {
logrus.Info("Re-Processing all claim outputs")
reProcessAllClaims()
}
}
func upgradeFrom11(version int) {
if version < 12 {
logrus.Info("Re-Processing all claim outputs")
go reProcessAllClaims()
}
}
func upgradeFrom12(version int) {
if version < 13 {
jobStatus := model.TableNames.JobStatus
state := model.JobStatusColumns.State
jobName := model.JobStatusColumns.JobName
startingJSON := `{\"JobStatus\": null, \"previous_sync\": \"2019-02-13T16:35:14Z\", \"last_height\": 530000}`
_, err := boil.GetDB().Exec(`
UPDATE `+jobStatus+`
SET `+state+`= '`+startingJSON+`'
WHERE `+jobName+`= ?
`, "claimtriesyncjob")
if err != nil {
logrus.Error("Upgrading to version 13:", err)
}
}
}