tables for sessions + user settings + editor

This commit is contained in:
Simon Martens
2025-05-21 23:21:50 +02:00
parent 6e2aa51092
commit 2316da4435
5 changed files with 307 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
package migrations
import (
"strings"
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
)
func init() {
m.Register(func(app core.App) error {
collection, err := app.FindCollectionByNameOrId(dbmodels.USERS_TABLE)
if err != nil {
app.Logger().Error("Failed to find 'users' collection to add 'settings' field", "id", dbmodels.USERS_TABLE, "error", err)
return err
}
settingsField := &core.JSONField{
Name: dbmodels.USERS_SETTINGS_FIELD,
Required: false,
Presentable: false,
}
collection.Fields.Add(settingsField)
app.Logger().Info("Adding 'settings' JSON field to 'users' collection", "collectionId", collection.Id)
return app.Save(collection)
}, func(app core.App) error {
collection, err := app.FindCollectionByNameOrId(dbmodels.USERS_TABLE)
if err != nil {
if strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "no rows in result set") {
app.Logger().Warn("Users collection not found during rollback of 'settings' field, assuming already handled.", "id", dbmodels.USERS_TABLE)
return nil
}
app.Logger().Error("Failed to find 'users' collection for 'settings' field rollback", "id", dbmodels.USERS_TABLE, "error", err)
return err
}
collection.Fields.RemoveByName(dbmodels.USERS_SETTINGS_FIELD)
if err := app.Save(collection); err != nil {
app.Logger().Warn("Failed to remove 'settings' field during rollback (it might not exist)",
"collectionId", collection.Id,
"field", dbmodels.USERS_SETTINGS_FIELD,
"error", err,
)
} else {
app.Logger().Info("Removed 'settings' JSON field from 'users' collection", "collectionId", collection.Id)
}
return app.Save(collection)
})
}