fix(stateadmin): distinguish a missing collection from a failed lookup

Both collection lookups treated any error as "not present". For the users
collection that returned nil on a real failure (caught by nilerr); for the
views it was worse but unflagged, because creating a fresh view collection on
the strength of a transient lookup error is a schema change made for the wrong
reason. Only sql.ErrNoRows now means absent.
This commit is contained in:
sam committed 2026-09-23 00:56:24 +10:00
1 parent 3ccc57d445
commit ad52528e04
1 file changed
+16 -4
@@ -26,6 +26,7 @@
package stateadmin
import (
"database/sql"
"errors"
"fmt"
"net/http"
@@ -134,9 +135,15 @@ func connectTo(dbPath string) core.DBConnectFunc {
func registerViews(app core.App) error {
for _, v := range views {
collection, err := app.FindCollectionByNameOrId(v.name)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
collection = core.NewViewCollection(v.name)
} else if !collection.IsView() {
case err != nil:
// A lookup that failed for any other reason must not be read as
// "absent": creating a second collection over a live one would
// be a schema change made on the strength of a transient error.
return fmt.Errorf("stateadmin: look up view %q: %w", v.name, err)
case !collection.IsView():
return fmt.Errorf("stateadmin: collection %q exists and is not a view", v.name)
}
collection.ViewQuery = v.query
@@ -157,8 +164,13 @@ func registerViews(app core.App) error {
// that is not there cannot be loosened again by a later PocketBase default.
func removeDefaultAuthCollection(app core.App) error {
collection, err := app.FindCollectionByNameOrId("users")
if err != nil {
return nil // already absent, including on every restart after the first
switch {
case errors.Is(err, sql.ErrNoRows):
// Already absent: every restart after the first, and any deployment
// whose PocketBase version stops shipping the default.
return nil
case err != nil:
return fmt.Errorf("stateadmin: look up default users collection: %w", err)
}
if err := app.Delete(collection); err != nil {
return fmt.Errorf("stateadmin: remove default users collection: %w", err)