Files
tajniak81andClaude Opus 5.5 b9be8be156
CI / go (Server) (push) Failing after 22m50s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m47s
CI / desktop (ubuntu-latest) (push) Successful in 2h56m50s
CI / mobile (push) Failing after 5m33s
CI / docker (push) Successful in 1h43m8s
Docs / build (push) Successful in 16m2s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Run docwell-db as a stock PocketBase and let the server set it up
docwell-db no longer needs a Docwell image: the stack runs
tajniak81/pocketbase-docker-root as it is, and docwell-server does what
the migrations and pb_hooks used to do inside PocketBase.

On start, docwell-server now:
- waits for PocketBase and imports Docwell's collections from an
  embedded snapshot (internal/pbschema). Existing collections and
  fields are matched by name, so their data is kept; the admin-managed
  users options (OAuth2 providers, magic link) are left untouched.
- writes the DOCWELL_APP_NAME, DOCWELL_WEB_URL, DOCWELL_MAIL_FROM,
  DOCWELL_SMTP_* and DOCWELL_S3_* settings into PocketBase, as the
  boot hook did.

It also replaces the hook's two routes: token checks use PocketBase's
auth-refresh (the organization name is read as the superuser), and
Docwell's own emails go out over SMTP from the server. PocketBase never
returns the SMTP password, so the server keeps a copy in the
superuser-only instance_settings collection; a password saved before
this change has to be entered once more in the admin panel.

Database/ keeps pb_migrations as the source of the schema;
export-schema.sh regenerates the snapshot from them. Database/Dockerfile
and pb_hooks are removed, the release no longer builds docwell-db, the
AIO image runs PocketBase without migrations or hooks, and the compose
files default DB_IMAGE to the stock image and pass the settings to the
server instead of the database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:09:38 +01:00

122 lines
3.7 KiB
Go

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