forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 15
/
1702134272_set_default_json_max_size.go
51 lines (42 loc) · 1.19 KB
/
1702134272_set_default_json_max_size.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
package migrations
import (
"github.com/AlperRehaYAZGAN/postgresbase/daos"
"github.com/AlperRehaYAZGAN/postgresbase/models"
"github.com/AlperRehaYAZGAN/postgresbase/models/schema"
"github.com/pocketbase/dbx"
)
// Update all collections with json fields to have a default MaxSize json field option.
func init() {
AppMigrations.Register(func(db dbx.Builder) error {
dao := daos.New(db)
// note: update even the view collections to prevent
// unnecessary change detections during the automigrate
collections := []*models.Collection{}
if err := dao.CollectionQuery().All(&collections); err != nil {
return err
}
for _, collection := range collections {
var needSave bool
for _, f := range collection.Schema.Fields() {
if f.Type != schema.FieldTypeJson {
continue
}
options, _ := f.Options.(*schema.JsonOptions)
if options == nil {
options = &schema.JsonOptions{}
}
options.MaxSize = 2000000 // 2mb
f.Options = options
needSave = true
}
if !needSave {
continue
}
// save only the collection model without updating its records table
if err := dao.Save(collection); err != nil {
return err
}
}
return nil
}, nil)
}