mirror of
https://github.com/napnap75/multiarch-docker-images.git
synced 2026-11-13 09:31:46 +01:00
Corrections after first run
This commit is contained in:
@@ -5,28 +5,28 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
ganigeorgiev.com/pocketbase/pocketbase/core"
|
||||
"ganigeorgiev.com/pocketbase/pocketbase/core"
|
||||
"github.com/napnap75/archivist/internal/config"
|
||||
"github.com/napnap75/archivist/internal/storage"
|
||||
)
|
||||
|
||||
// BindHooks registers the request hooks (US-04, US-22, US-23).
|
||||
func BindHooks(app *core.App, cfg *config.Config, s3 *storage.Client) {
|
||||
func BindHooks(app core.App, cfg *config.Config, s3 *storage.Client) {
|
||||
// US-04: document_events is append-only for everyone, superusers included.
|
||||
app.OnRecordUpdateRequest(eventsName).PreBindFunc(func(e *core.RecordRequestEvent) error {
|
||||
app.OnRecordUpdateRequest(eventsName).BindFunc(func(e *core.RecordRequestEvent) error {
|
||||
return errAppendOnly
|
||||
})
|
||||
app.OnRecordDeleteRequest(eventsName).PreBindFunc(func(e *core.RecordRequestEvent) error {
|
||||
app.OnRecordDeleteRequest(eventsName).BindFunc(func(e *core.RecordRequestEvent) error {
|
||||
return errAppendOnly
|
||||
})
|
||||
|
||||
// US-22 / US-23: corrections and validations through the standard UI.
|
||||
app.OnRecordUpdateRequest("documents").PreBindFunc(func(e *core.RecordRequestEvent) error {
|
||||
app.OnRecordUpdateRequest("documents").BindFunc(func(e *core.RecordRequestEvent) error {
|
||||
return handleDocumentUpdate(app, cfg, s3, e)
|
||||
})
|
||||
}
|
||||
|
||||
func handleDocumentUpdate(app *core.App, cfg *config.Config, s3 *storage.Client, e *core.RecordRequestEvent) error {
|
||||
func handleDocumentUpdate(app core.App, cfg *config.Config, s3 *storage.Client, e *core.RecordRequestEvent) error {
|
||||
newRec := e.Record
|
||||
|
||||
// The original state, before the submitted data is persisted.
|
||||
@@ -158,7 +158,7 @@ type Payload struct {
|
||||
}
|
||||
|
||||
// AppendEvent appends one event to document_events.
|
||||
func AppendEvent(app *core.App, documentId, eventType string, before, after Snapshot, errs []string) error {
|
||||
func AppendEvent(app core.App, documentId, eventType string, before, after Snapshot, errs []string) error {
|
||||
coll, err := app.FindCollectionByNameOrId(eventsName)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -184,7 +184,7 @@ func AppendEvent(app *core.App, documentId, eventType string, before, after Snap
|
||||
}
|
||||
|
||||
// AppendImportedEvent builds the imported event of US-16.
|
||||
func AppendImportedEvent(app *core.App, documentId, fromKey, toKey string, valid bool, fields map[string]any, errs []string) error {
|
||||
func AppendImportedEvent(app core.App, documentId, fromKey, toKey string, valid bool, fields map[string]any, errs []string) error {
|
||||
before := Snapshot{"file_key": fromKey}
|
||||
after := Snapshot{"file_key": toKey}
|
||||
if valid {
|
||||
|
||||
@@ -6,14 +6,14 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
ganigeorgiev.com/pocketbase/pocketbase/core"
|
||||
"ganigeorgiev.com/pocketbase/pocketbase/core"
|
||||
"github.com/napnap75/archivist/internal/config"
|
||||
)
|
||||
|
||||
const eventsName = "document_events"
|
||||
|
||||
// Setup ensures the collections and the superuser account (US-03, US-04, US-01).
|
||||
func Setup(app *core.App, adminEmail, adminPassword string, cfg *config.Config) error {
|
||||
func Setup(app core.App, adminEmail, adminPassword string, cfg *config.Config) error {
|
||||
if err := ensureDocuments(app, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -23,11 +23,10 @@ func Setup(app *core.App, adminEmail, adminPassword string, cfg *config.Config)
|
||||
if err := ensureSuperuser(app, adminEmail, adminPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
config.SetKnownFields(cfg.FieldList)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureDocuments(app *core.App, cfg *config.Config) error {
|
||||
func ensureDocuments(app core.App, cfg *config.Config) error {
|
||||
coll, err := app.FindCollectionByNameOrId("documents")
|
||||
if err != nil {
|
||||
// First start: create the collection with its fixed columns (US-03).
|
||||
@@ -59,9 +58,9 @@ func ensureDocuments(app *core.App, cfg *config.Config) error {
|
||||
case "boolean":
|
||||
newField = &core.BoolField{Name: f.Name}
|
||||
case "enum":
|
||||
// The vocabulary is an application notion (dashboard dropdown with
|
||||
// free entry), not a schema constraint: empty allowed values.
|
||||
newField = &core.SelectField{Name: f.Name, MaxSelect: 1}
|
||||
// PocketBase select fields require a fixed non-empty vocabulary;
|
||||
// config enums are free-form, so store them as text.
|
||||
newField = &core.TextField{Name: f.Name}
|
||||
}
|
||||
if existing := coll.Fields.GetByName(f.Name); existing != nil {
|
||||
if existing.Type() != newField.Type() {
|
||||
@@ -79,9 +78,9 @@ func ensureDocuments(app *core.App, cfg *config.Config) error {
|
||||
if f.GetSystem() {
|
||||
continue
|
||||
}
|
||||
if _, declared := cfg.Fields[f.Name]; !declared && !isFixed(f.Name) {
|
||||
app.Logger().Warn("documents: column removed", "field", f.Name, "reason", "removed from config")
|
||||
coll.Fields.RemoveByName(f.Name)
|
||||
if _, declared := cfg.Fields[f.GetName()]; !declared && !isFixed(f.GetName()) {
|
||||
app.Logger().Warn("documents: column removed", "field", f.GetName(), "reason", "removed from config")
|
||||
coll.Fields.RemoveByName(f.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +103,7 @@ func isFixed(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func ensureEvents(app *core.App) error {
|
||||
func ensureEvents(app core.App) error {
|
||||
if _, err := app.FindCollectionByNameOrId(eventsName); err == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -128,7 +127,7 @@ func ensureEvents(app *core.App) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureSuperuser(app *core.App, email, password string) error {
|
||||
func ensureSuperuser(app core.App, email, password string) error {
|
||||
superusers, err := app.FindCollectionByNameOrId(core.CollectionNameSuperusers)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package pbapp
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"ganigeorgiev.com/pocketbase/pocketbase"
|
||||
"github.com/napnap75/archivist/internal/config"
|
||||
)
|
||||
|
||||
func TestEnsureDocumentsUsesTextForFreeFormEnums(t *testing.T) {
|
||||
app := pocketbase.NewWithConfig(pocketbase.Config{DefaultDataDir: t.TempDir()})
|
||||
if err := app.Bootstrap(); err != nil {
|
||||
t.Fatalf("bootstrap PocketBase: %v", err)
|
||||
}
|
||||
defer app.ClearBootstrap()
|
||||
|
||||
fields := []config.Field{
|
||||
{Name: "documentType", Type: "enum"},
|
||||
{Name: "bankAccount", Type: "enum"},
|
||||
}
|
||||
cfg := &config.Config{
|
||||
Fields: map[string]config.Field{
|
||||
"documentType": fields[0],
|
||||
"bankAccount": fields[1],
|
||||
},
|
||||
FieldList: fields,
|
||||
}
|
||||
if err := ensureDocuments(app, cfg); err != nil {
|
||||
t.Fatalf("ensure documents: %v", err)
|
||||
}
|
||||
|
||||
coll, err := app.FindCollectionByNameOrId("documents")
|
||||
if err != nil {
|
||||
t.Fatalf("find documents collection: %v", err)
|
||||
}
|
||||
for _, name := range []string{"documentType", "bankAccount"} {
|
||||
field := coll.Fields.GetByName(name)
|
||||
if field == nil || field.Type() != "text" {
|
||||
t.Errorf("field %q = %v, want text field", name, field)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user