Plugins: create the settings collection instead of waiting for it forever

01a8fec fixed the advice that led operators into this, but advice is not a
guard: a stack still running PB_BOOTSTRAP=false gets no app_settings
collection on upgrade, and the plugin panel sits at 503 while the retry
loop reads a collection that does not exist.

The fix is not to soften the reading. A missing collection stays "not
ready" rather than "no plugins configured", because the alternative lets
the first save write a fresh document over settings the server merely
failed to find - the failure this whole line of work exists to prevent.
Instead the server now fixes the cause: on a missing collection it creates
that collection and reads again.

Three pieces:

bootstrap.EnsureCollection creates one named collection from the desired
schema if absent, and nothing else. Deliberately narrower than Run - no
field reconcile elsewhere, no super-admin - so it is safe to call on a
deployment that turned the full bootstrap off. It creates the collection
the server cannot start without, not the schema the operator declined.

The store tells a missing collection apart from an outage. A 404 from a
list means the collection itself is gone: an existing but empty one answers
200 with no items. That is tagged errNoCollection, which wraps errNotReady
so every write is still refused, and IsMissingCollection narrows it. The
distinction matters because the remedies are opposites - creating
collections against a flaky database is exactly the wrong reflex, and a
test pins that an outage does not trigger it.

loadPlugins acts on the tag once, then re-reads. Failing to create is
reported as the original read error rather than the repair's, so the log
names the real problem.

Six tests: the tag and its negative in internal/plugins, and three in
internal/api against a fake PocketBase covering the collection being
created exactly once, an existing collection not being recreated, and an
outage creating nothing.

Docs from 01a8fec are corrected in the same pass - they said the panel
would answer 503 forever, which is no longer true. They now say what still
depends on the bootstrap (every other collection and field) and what does
not (app_settings alone).

go build, go vet and go test ./... pass; compose files still parse. Not
verified: no Docker CLI here, so the repair has not been exercised against
a real PocketBase, only the fake.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tajniak81
2026-08-21 17:07:18 +02:00
co-authored by Claude Opus 5
parent 01a8fecf40
commit 660af5736a
18 changed files with 331 additions and 61 deletions
+28 -2
View File
@@ -127,6 +127,7 @@ import (
"sync"
"time"
"drivervault/apiserver/internal/bootstrap"
"drivervault/apiserver/internal/config"
"drivervault/apiserver/internal/ocpp"
"drivervault/apiserver/internal/pb"
@@ -222,9 +223,34 @@ func (s *Server) StartPlugins() error {
return err
}
// loadPlugins imports a pre-PocketBase plugins.json if one is still lying around
// and the database holds no settings yet, then reads the settings.
// loadPlugins reads the settings, creating the collection they live in if it
// turns out not to exist.
//
// That happens on a stack upgraded with PB_BOOTSTRAP off: the on-boot schema
// pass never ran, so app_settings was never created, and because a missing
// collection is read as "not ready" (never as "no plugins configured", which
// would let the first save overwrite settings the server merely failed to find)
// the retry below would spin forever with the plugin panel stuck at 503. So
// create just that one collection — not a full schema reconcile, which an
// operator who turned the bootstrap off has not asked for — and read again.
func (s *Server) loadPlugins(ctx context.Context) error {
err := s.readPlugins(ctx)
if err == nil || !plugins.IsMissingCollection(err) {
return err
}
log.Printf("plugins: the %s collection does not exist; creating it", colAppSettings)
if repairErr := bootstrap.EnsureCollection(ctx, s.pb, colAppSettings); repairErr != nil {
log.Printf("plugins: could not create %s: %v", colAppSettings, repairErr)
return err // report the original problem, not the repair's
}
log.Printf("plugins: created %s", colAppSettings)
return s.readPlugins(ctx)
}
// readPlugins imports a pre-PocketBase plugins.json if one is still lying around
// and the database holds no settings yet, then reads the settings.
func (s *Server) readPlugins(ctx context.Context) error {
switch migrated, err := plugins.MigrateLegacyFile(ctx, s.pluginStore, s.legacyPlugins); {
case err != nil:
// Not fatal: the read below reports the real problem if there is one.