forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1717233558_v0.23_migrate3.go
145 lines (120 loc) · 3.07 KB
/
1717233558_v0.23_migrate3.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
package migrations
import (
"hash/crc32"
"strconv"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
)
// note: this migration will be deleted in future version
func collectionIdChecksum(typ, name string) string {
return "pbc_" + strconv.Itoa(int(crc32.ChecksumIEEE([]byte(typ+name))))
}
func fieldIdChecksum(typ, name string) string {
return typ + strconv.Itoa(int(crc32.ChecksumIEEE([]byte(name))))
}
// normalize system collection and field ids
func init() {
core.SystemMigrations.Register(func(txApp core.App) error {
collections := []*core.Collection{}
err := txApp.CollectionQuery().
AndWhere(dbx.In(
"name",
core.CollectionNameMFAs,
core.CollectionNameOTPs,
core.CollectionNameExternalAuths,
core.CollectionNameAuthOrigins,
core.CollectionNameSuperusers,
)).
All(&collections)
if err != nil {
return err
}
for _, c := range collections {
var needUpdate bool
references, err := txApp.FindCollectionReferences(c, c.Id)
if err != nil {
return err
}
authOrigins, err := txApp.FindAllAuthOriginsByCollection(c)
if err != nil {
return err
}
mfas, err := txApp.FindAllMFAsByCollection(c)
if err != nil {
return err
}
otps, err := txApp.FindAllOTPsByCollection(c)
if err != nil {
return err
}
originalId := c.Id
// normalize collection id
if checksum := collectionIdChecksum(c.Type, c.Name); c.Id != checksum {
c.Id = checksum
needUpdate = true
}
// normalize system fields
for _, f := range c.Fields {
if !f.GetSystem() {
continue
}
if checksum := fieldIdChecksum(f.Type(), f.GetName()); f.GetId() != checksum {
f.SetId(checksum)
needUpdate = true
}
}
if !needUpdate {
continue
}
rawExport, err := c.DBExport(txApp)
if err != nil {
return err
}
_, err = txApp.DB().Update("_collections", rawExport, dbx.HashExp{"id": originalId}).Execute()
if err != nil {
return err
}
// make sure that the cached collection id is also updated
cached, err := txApp.FindCachedCollectionByNameOrId(c.Name)
if err != nil {
return err
}
cached.Id = c.Id
// update collection references
for refCollection, fields := range references {
for _, f := range fields {
relationField, ok := f.(*core.RelationField)
if !ok || relationField.CollectionId == originalId {
continue
}
relationField.CollectionId = c.Id
}
if err = txApp.SaveNoValidate(refCollection); err != nil {
return err
}
}
// update mfas references
for _, item := range mfas {
item.SetCollectionRef(c.Id)
if err = txApp.SaveNoValidate(item); err != nil {
return err
}
}
// update otps references
for _, item := range otps {
item.SetCollectionRef(c.Id)
if err = txApp.SaveNoValidate(item); err != nil {
return err
}
}
// update authOrigins references
for _, item := range authOrigins {
item.SetCollectionRef(c.Id)
if err = txApp.SaveNoValidate(item); err != nil {
return err
}
}
}
return nil
}, nil)
}