1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-23 14:07:13 +02:00
pocketbase/migrations/1702134272_set_default_json_max_size.go
Gani Georgiev 28d15e86eb fixed optional migration condition
note: practically even the previous version should work ok because the json field didn't have previous options anyway and if it was nil the migration will fail
2024-01-05 20:35:09 +02:00

52 lines
1.2 KiB
Go

package migrations
import (
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
)
// 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)
}