Files
event-driven-shoppinglist/migrations.go
2025-09-29 12:22:01 +02:00

126 lines
3.4 KiB
Go

package main
import (
"fmt"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/types"
)
// setupCollections creates the required PocketBase collections for the event store
func setupCollections(app core.App) error {
fmt.Println("Creating PocketBase collections...")
// Create events collection if it doesn't exist
if _, err := app.FindCollectionByNameOrId("events"); err != nil {
fmt.Println("Creating events collection...")
eventsCollection := core.NewBaseCollection("events")
// Admin only access for events
eventsCollection.ListRule = nil
eventsCollection.ViewRule = nil
eventsCollection.CreateRule = nil
eventsCollection.UpdateRule = nil
eventsCollection.DeleteRule = nil
// Add fields - ONE EVENT = ONE OPERATION
eventsCollection.Fields.Add(&core.NumberField{
Name: "seq",
Required: true,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "hash",
Required: true,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "item_id",
Required: true,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "event_id",
Required: true,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "collection",
Required: true,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "operation",
Required: true,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "path",
Required: true,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "value",
Required: false,
})
eventsCollection.Fields.Add(&core.TextField{
Name: "from",
Required: false,
})
eventsCollection.Fields.Add(&core.DateField{
Name: "timestamp",
Required: true,
})
// Add index on sequence number
eventsCollection.AddIndex("idx_events_seq", false, "seq", "")
if err := app.Save(eventsCollection); err != nil {
return fmt.Errorf("failed to create events collection: %w", err)
}
fmt.Println("✅ Events collection created successfully")
} else {
fmt.Println("Events collection already exists")
}
// Create shopping_items collection if it doesn't exist
if _, err := app.FindCollectionByNameOrId("shopping_items"); err != nil {
fmt.Println("Creating shopping_items collection...")
itemsCollection := core.NewBaseCollection("shopping_items")
// Public access rules
itemsCollection.ListRule = types.Pointer("")
itemsCollection.ViewRule = types.Pointer("")
itemsCollection.CreateRule = types.Pointer("")
itemsCollection.UpdateRule = types.Pointer("")
itemsCollection.DeleteRule = types.Pointer("")
// Add static fields only - no arbitrary fields allowed
itemsCollection.Fields.Add(&core.TextField{
Name: "content",
Required: false,
})
itemsCollection.Fields.Add(&core.TextField{
Name: "priority",
Required: false,
})
itemsCollection.Fields.Add(&core.DateField{
Name: "created_at",
Required: false,
})
itemsCollection.Fields.Add(&core.DateField{
Name: "updated_at",
Required: false,
})
itemsCollection.Fields.Add(&core.DateField{
Name: "deleted_at",
Required: false,
})
if err := app.Save(itemsCollection); err != nil {
return fmt.Errorf("failed to create shopping_items collection: %w", err)
}
fmt.Println("✅ Shopping_items collection created successfully")
} else {
fmt.Println("Shopping_items collection already exists")
}
fmt.Println("✅ PocketBase collections setup complete")
return nil
}