Every attachment — a document scan, a fuel receipt, a workshop invoice, a
photo of a part's box — has lived inside pb_data, in a directory beside the
SQLite file. One volume held both, so neither could be sized, backed up or
moved without the other. PocketBase can keep those bytes in an S3 bucket
instead, and now it is told to.
Nothing on the way to a client changes, because an attachment was never a
storage URL to begin with: it is fetched from GET /api/{records}/{id}/file,
which re-checks car access and asks PocketBase for the bytes as the service
account. PocketBase streams from the bucket through that same endpoint rather
than redirecting to it, so the web app, the phone and the plugin cannot tell
which side of the switch they are on.
The bootstrap that already creates the collections now writes PocketBase's
files-storage settings too, from PB_S3_*, on every boot and only when they
differ from what is already there — then asks PocketBase to prove it can reach
the bucket, and says so in the log either way. Two asymmetries are deliberate.
A read of the settings masks the stored secret, so a rotation of the secret
alone is invisible from here and needs another PB_S3_* to move with it. And it
never turns S3 back off: files already written to a bucket are reachable only
while PocketBase still points at it, so dropping the configuration would strand
them rather than undo anything.
Each deployment shape is one compose file with an .env example of the same
name, not a base plus an overlay to remember — six of each per folder, for
Docker and Docker-AIO alike: the plain one, .seaweedfs, .s3, and the three prod
twins. The SeaweedFS files run master, volume, filer and gateway as one process
and a one-shot init container beside it, because PocketBase never issues a
CreateBucket and SeaweedFS will not conjure one on first upload. The credentials
do double duty there — the gateway's only identity is also what PocketBase
authenticates with. In the all-in-one that gateway is a second container rather
than a fourth process under supervisord: keeping the object store inside the
image, on the volume the files are being moved off, would have defeated the
point and would have meant rebuilding.
Files uploaded before the switch are not carried across; PocketBase copies
nothing, and both READMEs say so where an operator will read it.
The TLS overlay and its Caddyfile go. The section they served stays, without
them: nothing in the stack terminates TLS any more, so it now names the four
variables to set in front of whichever proxy already does — TRUST_FORWARDED_PROTO
being the one that decides whether a charger is believed about how it arrived.
Unexercised: this was written on a machine without Docker, so the pinned
SeaweedFS image, the bucket-create and the settings write have not been run
against a live stack. The Go side builds, vets and tests clean.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
133 lines
4.8 KiB
Go
133 lines
4.8 KiB
Go
package bootstrap
|
|
|
|
import "testing"
|
|
|
|
// TestRenderFieldModern locks the v0.23+ ("fields") wire shape for each field
|
|
// type against what PocketBase expects.
|
|
func TestRenderFieldModern(t *testing.T) {
|
|
idByName := map[string]string{"cars": "col_cars"}
|
|
|
|
rel := renderField(fRelation("car", "cars", true, true), "fields", idByName)
|
|
if rel["collectionId"] != "col_cars" || rel["cascadeDelete"] != true ||
|
|
rel["maxSelect"] != 1 || rel["minSelect"] != 0 || rel["required"] != true {
|
|
t.Fatalf("relation rendered wrong: %#v", rel)
|
|
}
|
|
|
|
nonCascade := renderField(fRelation("owner", "users", false, false), "fields", map[string]string{"users": "u"})
|
|
if nonCascade["cascadeDelete"] != false {
|
|
t.Fatalf("non-cascading relation should keep cascadeDelete=false: %#v", nonCascade)
|
|
}
|
|
|
|
sel := renderField(fSelect("result", []string{"passed", "failed"}, false), "fields", idByName)
|
|
if sel["maxSelect"] != 1 {
|
|
t.Fatalf("select maxSelect: %#v", sel)
|
|
}
|
|
if vals, ok := sel["values"].([]string); !ok || len(vals) != 2 {
|
|
t.Fatalf("select values: %#v", sel["values"])
|
|
}
|
|
|
|
ad := renderField(fAutodate("created", true, false), "fields", idByName)
|
|
if ad["onCreate"] != true || ad["onUpdate"] != false {
|
|
t.Fatalf("autodate: %#v", ad)
|
|
}
|
|
|
|
file := renderField(attachment(), "fields", idByName)
|
|
if file["maxSize"] != 10485760 || file["maxSelect"] != 1 {
|
|
t.Fatalf("file: %#v", file)
|
|
}
|
|
if mt, ok := file["mimeTypes"].([]string); !ok || len(mt) != 5 {
|
|
t.Fatalf("file mimeTypes: %#v", file["mimeTypes"])
|
|
}
|
|
|
|
j := renderField(fJSON("params", 10000), "fields", idByName)
|
|
if j["maxSize"] != 10000 {
|
|
t.Fatalf("json: %#v", j)
|
|
}
|
|
}
|
|
|
|
// TestRenderFieldLegacy checks that the legacy ("schema") layout nests options.
|
|
func TestRenderFieldLegacy(t *testing.T) {
|
|
rel := renderField(fRelation("car", "cars", true, true), "schema", map[string]string{"cars": "col_cars"})
|
|
opts, ok := rel["options"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("legacy relation should nest options: %#v", rel)
|
|
}
|
|
if opts["collectionId"] != "col_cars" || opts["cascadeDelete"] != true {
|
|
t.Fatalf("legacy relation options: %#v", opts)
|
|
}
|
|
}
|
|
|
|
// TestSameValues covers the select-reconcile comparison (order-insensitive).
|
|
func TestSameValues(t *testing.T) {
|
|
if !sameValues([]any{"a", "b"}, []string{"b", "a"}) {
|
|
t.Fatal("same set in different order should compare equal")
|
|
}
|
|
if sameValues([]any{"a"}, []string{"a", "b"}) {
|
|
t.Fatal("differing lengths should not be equal")
|
|
}
|
|
if sameValues([]any{"a", "c"}, []string{"a", "b"}) {
|
|
t.Fatal("differing members should not be equal")
|
|
}
|
|
if sameValues(nil, []string{"a"}) {
|
|
t.Fatal("nil current should not equal non-empty want")
|
|
}
|
|
}
|
|
|
|
// TestSchemaConsistency guards the ordered lists against the schema map.
|
|
func TestSchemaConsistency(t *testing.T) {
|
|
for _, name := range createOrder {
|
|
if _, ok := collectionsSchema[name]; !ok {
|
|
t.Errorf("createOrder references unknown collection %q", name)
|
|
}
|
|
if name == "users" {
|
|
t.Errorf("users must not be in createOrder (built-in auth collection)")
|
|
}
|
|
}
|
|
for _, name := range reconcileOrder {
|
|
if _, ok := collectionsSchema[name]; !ok {
|
|
t.Errorf("reconcileOrder references unknown collection %q", name)
|
|
}
|
|
}
|
|
for name := range indexes {
|
|
if _, ok := collectionsSchema[name]; !ok {
|
|
t.Errorf("indexes references unknown collection %q", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestSameExceptSecret covers the decision ensureFileStorage makes on every
|
|
// boot: write the settings, or leave them alone. The secret is excluded because
|
|
// PocketBase masks it on read — comparing it would make every boot a write.
|
|
func TestSameExceptSecret(t *testing.T) {
|
|
want := storageSettings{
|
|
Enabled: true,
|
|
Bucket: "drivervault",
|
|
Region: "us-east-1",
|
|
Endpoint: "http://seaweedfs:8333",
|
|
AccessKey: "key",
|
|
Secret: "secret",
|
|
ForcePathStyle: true,
|
|
}
|
|
|
|
cases := []struct {
|
|
name string
|
|
current storageSettings
|
|
same bool
|
|
}{
|
|
{"identical", want, true},
|
|
{"masked secret", func() storageSettings { s := want; s.Secret = ""; return s }(), true},
|
|
{"rotated secret only", func() storageSettings { s := want; s.Secret = "other"; return s }(), true},
|
|
{"changed endpoint", func() storageSettings { s := want; s.Endpoint = "http://elsewhere:8333"; return s }(), false},
|
|
{"changed bucket", func() storageSettings { s := want; s.Bucket = "other"; return s }(), false},
|
|
{"changed access key", func() storageSettings { s := want; s.AccessKey = "other"; return s }(), false},
|
|
{"still disabled", func() storageSettings { s := want; s.Enabled = false; return s }(), false},
|
|
{"path style off", func() storageSettings { s := want; s.ForcePathStyle = false; return s }(), false},
|
|
{"untouched settings", storageSettings{}, false},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := tc.current.sameExceptSecret(want); got != tc.same {
|
|
t.Errorf("%s: sameExceptSecret = %v, want %v", tc.name, got, tc.same)
|
|
}
|
|
}
|
|
}
|