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

@@ -425,11 +425,13 @@ const (
ENTRIES_TABLE = "entries"
CONTENTS_TABLE = "contents"
ITEMS_TABLE = "items"
SESSIONS_TABLE = "sessions"
ID_FIELD = "id"
CREATED_FIELD = "created"
UPDATED_FIELD = "updated"
ANNOTATION_FIELD = "annotation"
EDITOR_FIELD = "editor"
MUSENALMID_FIELD = "musenalm_id"
EDITSTATE_FIELD = "edit_state"
@@ -493,4 +495,16 @@ const (
ITEMS_MEDIA_FIELD = "media"
ITEMS_CONDITION_FIELD = "condition"
ITEMS_IDENTIFIER_FIELD = "identifier"
SESSIONS_TOKEN_FIELD = "token"
SESSIONS_USER_FIELD = "user"
SESSIONS_IP_FIELD = "ip"
SESSIONS_USER_AGENT_FIELD = "agent"
SESSIONS_EXPIRES_FIELD = "expires"
SESSIONS_LAST_ACCESS_FIELD = "accessed"
SESSIONS_STATUS_FIELD = "status"
SESSIONS_PERSIST_FIELD = "persist"
USERS_TABLE = "users"
USERS_SETTINGS_FIELD = "settings"
)

View File

@@ -0,0 +1,89 @@
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 {
usersCollection, err := app.FindCollectionByNameOrId("users")
if err != nil {
app.Logger().Error("Failed to find 'users' collection for sessionTokens migration", "error", err)
return err
}
collection := sessionTokensTable()
fields := sessionTokensFields(usersCollection.Id)
dbmodels.SetCreatedUpdatedFields(&fields)
collection.Fields = fields
dbmodels.AddIndex(collection, dbmodels.SESSIONS_TOKEN_FIELD, true)
dbmodels.AddIndex(collection, dbmodels.SESSIONS_USER_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.SESSIONS_EXPIRES_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.SESSIONS_LAST_ACCESS_FIELD, false)
return app.Save(collection)
}, func(app core.App) error {
collection, err := app.FindCollectionByNameOrId(dbmodels.SESSIONS_TABLE)
if err != nil {
if strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "no rows in result set") {
return nil
}
app.Logger().Error("Failed to find collection for deletion", "collection", dbmodels.SESSIONS_TABLE, "error", err)
return err
}
return app.Delete(collection)
})
}
func sessionTokensTable() *core.Collection {
collection := core.NewBaseCollection(dbmodels.SESSIONS_TABLE)
return collection
}
func sessionTokensFields(usersCollectionId string) core.FieldsList {
fields := core.NewFieldsList(
&core.TextField{
Name: dbmodels.SESSIONS_TOKEN_FIELD,
Required: true,
Presentable: false,
},
&core.RelationField{
Name: dbmodels.SESSIONS_USER_FIELD,
Required: true,
CollectionId: usersCollectionId,
CascadeDelete: true,
Presentable: true,
},
&core.DateField{
Name: dbmodels.SESSIONS_EXPIRES_FIELD,
Required: true,
Presentable: true,
},
&core.DateField{
Name: dbmodels.SESSIONS_LAST_ACCESS_FIELD,
Presentable: false,
},
&core.TextField{
Name: dbmodels.SESSIONS_IP_FIELD,
Presentable: false,
},
&core.TextField{
Name: dbmodels.SESSIONS_USER_AGENT_FIELD,
Presentable: false,
},
&core.BoolField{
Name: dbmodels.SESSIONS_PERSIST_FIELD,
Required: true,
Presentable: true,
},
)
return fields
}

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)
})
}

View File

@@ -0,0 +1,146 @@
package migrations
import (
"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 {
agents_coll, err := app.FindCollectionByNameOrId(dbmodels.AGENTS_TABLE)
if err != nil {
return err
}
places_coll, err := app.FindCollectionByNameOrId(dbmodels.PLACES_TABLE)
if err != nil {
return err
}
series_coll, err := app.FindCollectionByNameOrId(dbmodels.SERIES_TABLE)
if err != nil {
return err
}
entries_coll, err := app.FindCollectionByNameOrId(dbmodels.ENTRIES_TABLE)
if err != nil {
return err
}
contents_coll, err := app.FindCollectionByNameOrId(dbmodels.CONTENTS_TABLE)
if err != nil {
return err
}
items_coll, err := app.FindCollectionByNameOrId(dbmodels.ITEMS_TABLE)
if err != nil {
return err
}
users_coll, err := app.FindCollectionByNameOrId(dbmodels.USERS_TABLE)
if err != nil {
return err
}
// add edited field to users collection
addEditedField(agents_coll, users_coll.Id)
addEditedField(places_coll, users_coll.Id)
addEditedField(series_coll, users_coll.Id)
addEditedField(entries_coll, users_coll.Id)
addEditedField(contents_coll, users_coll.Id)
addEditedField(items_coll, users_coll.Id)
dbmodels.AddIndex(agents_coll, dbmodels.EDITOR_FIELD, false)
dbmodels.AddIndex(places_coll, dbmodels.EDITOR_FIELD, false)
dbmodels.AddIndex(series_coll, dbmodels.EDITOR_FIELD, false)
dbmodels.AddIndex(entries_coll, dbmodels.EDITOR_FIELD, false)
dbmodels.AddIndex(contents_coll, dbmodels.EDITOR_FIELD, false)
dbmodels.AddIndex(items_coll, dbmodels.EDITOR_FIELD, false)
err_agents := app.Save(agents_coll)
if err_agents != nil {
return err_agents
}
err_places := app.Save(places_coll)
if err_places != nil {
return err_places
}
err_series := app.Save(series_coll)
if err_series != nil {
return err_series
}
err_entries := app.Save(entries_coll)
if err_entries != nil {
return err_entries
}
err_contents := app.Save(contents_coll)
if err_contents != nil {
return err_contents
}
err_items := app.Save(items_coll)
if err_items != nil {
return err_items
}
return nil
}, func(app core.App) error {
agents_coll, err := app.FindCollectionByNameOrId(dbmodels.AGENTS_TABLE)
if err == nil {
removeEditedField(agents_coll)
_ = app.Save(agents_coll)
}
places_coll, err := app.FindCollectionByNameOrId(dbmodels.PLACES_TABLE)
if err == nil {
removeEditedField(places_coll)
_ = app.Save(places_coll)
}
series_coll, err := app.FindCollectionByNameOrId(dbmodels.SERIES_TABLE)
if err == nil {
removeEditedField(series_coll)
_ = app.Save(series_coll)
}
entries_coll, err := app.FindCollectionByNameOrId(dbmodels.ENTRIES_TABLE)
if err == nil {
removeEditedField(entries_coll)
_ = app.Save(entries_coll)
}
contents_coll, err := app.FindCollectionByNameOrId(dbmodels.CONTENTS_TABLE)
if err == nil {
removeEditedField(contents_coll)
_ = app.Save(contents_coll)
}
items_coll, err := app.FindCollectionByNameOrId(dbmodels.ITEMS_TABLE)
if err == nil {
removeEditedField(items_coll)
_ = app.Save(items_coll)
}
return nil
})
}
func removeEditedField(collection *core.Collection) {
collection.Fields.RemoveByName(dbmodels.EDITOR_FIELD)
}
func addEditedField(collection *core.Collection, usersid string) {
editedField := &core.RelationField{
Name: dbmodels.EDITOR_FIELD,
Required: false,
System: false,
CollectionId: usersid,
}
collection.Fields.Add(editedField)
}

3
scripts/build.sh Normal file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
go build -tags=dev,fts5,sqlite_icu -o ./tmp/musenalm .