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