Files
Docwell/Server/internal/pbschema/pbschema_test.go
T
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

88 lines
2.6 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package pbschema
import (
"encoding/json"
"testing"
)
func TestSnapshot(t *testing.T) {
cols, err := Collections()
if err != nil {
t.Fatal(err)
}
names := map[string]bool{}
for _, c := range cols {
name := c["name"].(string)
if name[0] == '_' {
t.Errorf("system collection %s in the snapshot", name)
}
names[name] = true
if name == "users" {
if _, ok := c["oauth2"]; ok {
t.Error("users.oauth2 is admin-managed and must not be in the snapshot")
}
if _, ok := c["otp"].(map[string]any)["enabled"]; ok {
t.Error("users.otp.enabled is admin-managed and must not be in the snapshot")
}
}
}
for _, want := range []string{"users", "organizations", "pages", "instance_settings"} {
if !names[want] {
t.Errorf("snapshot has no %s collection", want)
}
}
// Every relation must point at a collection in the snapshot.
if err := remap(cols, nil); err != nil {
t.Fatal(err)
}
}
func TestRemap(t *testing.T) {
var want []map[string]any
_ = json.Unmarshal([]byte(`[
{"id": "snap_orgs", "name": "organizations", "fields": [
{"id": "f_name", "name": "name", "type": "text"},
{"id": "f_new", "name": "slug", "type": "text"}]},
{"id": "snap_pages", "name": "pages", "fields": [
{"id": "f_org", "name": "organization", "type": "relation", "collectionId": "snap_orgs"}]}
]`), &want)
have := []existingCollection{{ID: "live_orgs", Name: "organizations"}}
have[0].Fields = append(have[0].Fields, struct {
ID string `json:"id"`
Name string `json:"name"`
}{ID: "live_name", Name: "name"})
if err := remap(want, have); err != nil {
t.Fatal(err)
}
orgs, pages := want[0], want[1]
if orgs["id"] != "live_orgs" {
t.Errorf("existing collection id = %v, want live_orgs", orgs["id"])
}
fields := orgs["fields"].([]any)
if id := fields[0].(map[string]any)["id"]; id != "live_name" {
t.Errorf("existing field id = %v, want live_name", id)
}
if id := fields[1].(map[string]any)["id"]; id != "f_new" {
t.Errorf("new field id = %v, want f_new", id)
}
if pages["id"] != "snap_pages" {
t.Errorf("new collection id = %v, want snap_pages", pages["id"])
}
rel := pages["fields"].([]any)[0].(map[string]any)
if rel["collectionId"] != "live_orgs" {
t.Errorf("relation target = %v, want live_orgs", rel["collectionId"])
}
}
func TestRemapUnknownRelation(t *testing.T) {
var want []map[string]any
_ = json.Unmarshal([]byte(`[{"id": "a", "name": "a", "fields": [
{"id": "r", "name": "r", "type": "relation", "collectionId": "missing"}]}]`), &want)
if err := remap(want, nil); err == nil {
t.Fatal("expected an error for a relation to an unknown collection")
}
}