Add PocketBase v0.40.6 as a dependency together with the pieces the new backend is built from: the list of synced collections with their client-writable fields, the initial migration that creates the five per-user collections (entries, closed_days, closed_weeks, settings_history, balance_adjustments), and empty Register stubs for the tracking and legacy import packages. Clients only get read access to their own rows over REST; writes are meant to go through the sync push endpoint. web/build is now ignored except for a tracked placeholder so the built SPA can be embedded. The entry point (main.go) and the UI embedding (ui.go) are not part of this commit: the repo root is still the legacy "assets" package, and Go does not allow "package main" in the same directory.
116 lines
4.2 KiB
Go
116 lines
4.2 KiB
Go
package migrations
|
|
|
|
import (
|
|
"github.com/pocketbase/pocketbase/core"
|
|
m "github.com/pocketbase/pocketbase/migrations"
|
|
"github.com/pocketbase/pocketbase/tools/types"
|
|
)
|
|
|
|
const (
|
|
dayKeyPattern = `^\d{4}-\d{2}-\d{2}$`
|
|
weekKeyPattern = `^\d{4}-W\d{2}$`
|
|
)
|
|
|
|
func init() {
|
|
m.Register(up, down)
|
|
}
|
|
|
|
func ownerField(usersId string) *core.RelationField {
|
|
return &core.RelationField{Name: "owner", CollectionId: usersId, Required: true, CascadeDelete: true, MaxSelect: 1}
|
|
}
|
|
|
|
func autodates(c *core.Collection) {
|
|
c.Fields.Add(&core.AutodateField{Name: "created", OnCreate: true})
|
|
c.Fields.Add(&core.AutodateField{Name: "updated", OnCreate: true, OnUpdate: true})
|
|
}
|
|
|
|
// syncedCollection returns a base collection with the fields and rules shared by all synced collections.
|
|
// Clients can only read their own rows through the REST API; writes go through /api/sync/push.
|
|
func syncedCollection(name, usersId string, fields ...core.Field) *core.Collection {
|
|
c := core.NewBaseCollection(name)
|
|
c.Fields.Add(ownerField(usersId))
|
|
for _, f := range fields {
|
|
c.Fields.Add(f)
|
|
}
|
|
c.Fields.Add(&core.NumberField{Name: "client_updated_at", OnlyInt: true})
|
|
c.Fields.Add(&core.NumberField{Name: "rev", OnlyInt: true})
|
|
c.Fields.Add(&core.BoolField{Name: "deleted"})
|
|
autodates(c)
|
|
ownerRule := types.Pointer("owner = @request.auth.id")
|
|
c.ListRule = ownerRule
|
|
c.ViewRule = ownerRule
|
|
c.CreateRule = nil
|
|
c.UpdateRule = nil
|
|
c.DeleteRule = nil
|
|
c.AddIndex("idx_"+name+"_owner_updated", false, "owner, updated", "")
|
|
return c
|
|
}
|
|
|
|
func up(app core.App) error {
|
|
users, err := app.FindCollectionByNameOrId("users")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
entries := syncedCollection("entries", users.Id,
|
|
&core.NumberField{Name: "start_time", OnlyInt: true, Min: types.Pointer(0.0)},
|
|
&core.NumberField{Name: "end_time", OnlyInt: true, Min: types.Pointer(0.0)},
|
|
&core.BoolField{Name: "auto_stopped"},
|
|
&core.TextField{Name: "note", Max: 5000},
|
|
&core.TextField{Name: "day_key", Required: true, Pattern: dayKeyPattern},
|
|
)
|
|
entries.AddIndex("idx_entries_owner_day", false, "owner, day_key", "")
|
|
|
|
closedDays := syncedCollection("closed_days", users.Id,
|
|
&core.TextField{Name: "day_key", Required: true, Pattern: dayKeyPattern},
|
|
&core.NumberField{Name: "worked_ms", OnlyInt: true, Min: types.Pointer(0.0)},
|
|
&core.SelectField{Name: "kind", Values: []string{"work", "holiday", "vacation", "sick"}, Required: true},
|
|
&core.NumberField{Name: "closed_at", OnlyInt: true},
|
|
)
|
|
closedDays.AddIndex("idx_closed_days_owner_day_key", true, "owner, day_key", "")
|
|
|
|
closedWeeks := syncedCollection("closed_weeks", users.Id,
|
|
&core.TextField{Name: "week_key", Required: true, Pattern: weekKeyPattern},
|
|
&core.NumberField{Name: "expected_ms", OnlyInt: true},
|
|
&core.NumberField{Name: "worked_ms", OnlyInt: true},
|
|
&core.NumberField{Name: "delta_ms", OnlyInt: true},
|
|
&core.NumberField{Name: "closed_at", OnlyInt: true},
|
|
)
|
|
closedWeeks.AddIndex("idx_closed_weeks_owner_week_key", true, "owner, week_key", "")
|
|
|
|
settingsHistory := syncedCollection("settings_history", users.Id,
|
|
&core.TextField{Name: "effective_from", Required: true, Pattern: dayKeyPattern},
|
|
&core.NumberField{Name: "hours_per_week", Min: types.Pointer(0.01)},
|
|
&core.NumberField{Name: "workdays_mask", OnlyInt: true, Min: types.Pointer(1.0), Max: types.Pointer(127.0)},
|
|
&core.TextField{Name: "timezone", Required: true, Max: 100},
|
|
)
|
|
settingsHistory.AddIndex("idx_settings_history_owner_effective_from", false, "owner, effective_from", "")
|
|
|
|
balanceAdjustments := syncedCollection("balance_adjustments", users.Id,
|
|
&core.NumberField{Name: "delta_ms", OnlyInt: true},
|
|
&core.TextField{Name: "note", Max: 5000},
|
|
&core.NumberField{Name: "effective_at", OnlyInt: true},
|
|
)
|
|
balanceAdjustments.AddIndex("idx_balance_adjustments_owner_effective_at", false, "owner, effective_at", "")
|
|
|
|
for _, c := range []*core.Collection{entries, closedDays, closedWeeks, settingsHistory, balanceAdjustments} {
|
|
if err := app.Save(c); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func down(app core.App) error {
|
|
for _, name := range []string{"entries", "closed_days", "closed_weeks", "settings_history", "balance_adjustments"} {
|
|
c, err := app.FindCollectionByNameOrId(name)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if err := app.Delete(c); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|