// SPDX-License-Identifier: AGPL-3.0-or-later // Package pbschema brings PocketBase's collections to the schema docwell-server // expects, so docwell-db can be a stock PocketBase image with no migrations or // hooks of its own. // // schema.json is the target state: every Docwell collection, plus the changes // Docwell makes to the built-in "users" auth collection. It is generated from // Database/pb_migrations by Database/export-schema.sh; edit the migrations, // not the JSON. // // Apply imports it through PocketBase's collections import, which runs in one // transaction. Collections and fields that already exist keep their ids // (matched by name), so their data stays; missing ones are created. Settings // the admin panel changes on "users" (OAuth2 providers, magic link) are left // out of the snapshot, and the import keeps their current values. package pbschema import ( "context" _ "embed" "encoding/json" "fmt" "net/http" "docwell/server/internal/store/pocketbase" ) //go:embed schema.json var snapshot []byte // Collections returns the target collections, decoded fresh on every call. func Collections() ([]map[string]any, error) { var cols []map[string]any if err := json.Unmarshal(snapshot, &cols); err != nil { return nil, fmt.Errorf("pbschema: schema.json: %w", err) } return cols, nil } type existingCollection struct { ID string `json:"id"` Name string `json:"name"` Fields []struct { ID string `json:"id"` Name string `json:"name"` } `json:"fields"` } // Apply imports the snapshot into PocketBase. Running it again is harmless. func Apply(ctx context.Context, pb *pocketbase.Client) error { want, err := Collections() if err != nil { return err } var have struct { Items []existingCollection `json:"items"` } if err := pb.Do(ctx, http.MethodGet, "/api/collections?perPage=1000&skipTotal=1&fields=id,name,fields", nil, &have); err != nil { return fmt.Errorf("pbschema: list collections: %w", err) } if err := remap(want, have.Items); err != nil { return err } body := map[string]any{"collections": want, "deleteMissing": false} if err := pb.Do(ctx, http.MethodPut, "/api/collections/import", body, nil); err != nil { return fmt.Errorf("pbschema: import collections: %w", err) } return nil } // remap rewrites the snapshot's collection and field ids to the ids of the // collections and fields PocketBase already has with the same names, and // points relation fields at the remapped collection ids. func remap(want []map[string]any, have []existingCollection) error { byName := make(map[string]existingCollection, len(have)) for _, c := range have { byName[c.Name] = c } ids := map[string]string{} // snapshot collection id -> live id for _, c := range want { id, _ := c["id"].(string) name, _ := c["name"].(string) if id == "" || name == "" { return fmt.Errorf("pbschema: schema.json: collection without id or name") } if live, ok := byName[name]; ok { ids[id] = live.ID } else { ids[id] = id } } for _, c := range want { name := c["name"].(string) c["id"] = ids[c["id"].(string)] liveFields := map[string]string{} for _, f := range byName[name].Fields { liveFields[f.Name] = f.ID } fields, _ := c["fields"].([]any) for _, raw := range fields { f, ok := raw.(map[string]any) if !ok { return fmt.Errorf("pbschema: schema.json: %s: malformed field", name) } if fieldName, _ := f["name"].(string); liveFields[fieldName] != "" { f["id"] = liveFields[fieldName] } if target, ok := f["collectionId"].(string); ok && target != "" { live, known := ids[target] if !known { return fmt.Errorf("pbschema: schema.json: %s.%v points at unknown collection %s", name, f["name"], target) } f["collectionId"] = live } } } return nil }