Files
Docwell/Server/cmd/pbschema-export/main.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

76 lines
2.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
// Command pbschema-export writes internal/pbschema/schema.json from a running
// PocketBase that has applied Database/pb_migrations. Database/export-schema.sh
// starts that PocketBase and runs this; there is no need to call it directly.
//
// pbschema-export -url http://127.0.0.1:18070 -email ... -password ... -out internal/pbschema/schema.json
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"docwell/server/internal/store/pocketbase"
)
func main() {
url := flag.String("url", "http://127.0.0.1:18070", "PocketBase URL")
email := flag.String("email", "", "superuser email")
password := flag.String("password", "", "superuser password")
out := flag.String("out", "internal/pbschema/schema.json", "output file")
flag.Parse()
if err := run(*url, *email, *password, *out); err != nil {
fmt.Fprintln(os.Stderr, "pbschema-export:", err)
os.Exit(1)
}
}
func run(url, email, password, out string) error {
pb := pocketbase.New(url, email, password)
var res struct {
Items []map[string]any `json:"items"`
}
if err := pb.Do(context.Background(), http.MethodGet, "/api/collections?perPage=1000&skipTotal=1&sort=created", nil, &res); err != nil {
return err
}
var cols []map[string]any
for _, c := range res.Items {
name, _ := c["name"].(string)
// PocketBase's own system collections (_superusers, _otps, ...).
if strings.HasPrefix(name, "_") {
continue
}
delete(c, "created")
delete(c, "updated")
if name == "users" {
// Managed in the admin panel; the import keeps whatever is set.
delete(c, "oauth2")
if otp, ok := c["otp"].(map[string]any); ok {
delete(otp, "enabled")
}
}
cols = append(cols, c)
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false) // keep the email templates readable in diffs
enc.SetIndent("", " ")
if err := enc.Encode(cols); err != nil {
return err
}
if err := os.WriteFile(out, buf.Bytes(), 0o644); err != nil {
return err
}
fmt.Printf("wrote %d collections to %s\n", len(cols), out)
return nil
}