Index.Sync names each object kind and what it reloads (level-up §9, §14 Phase 1 item 2)
A type or field event reloads that one type; view, file and draft events reload nothing; a kind nobody names still rebuilds everything, and a test counts rebuilds so a named kind that falls through fails it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+115
−2
internal/fichas/index.go+47 −2internal/fichas/schema.go+6 −0internal/fichas/sync_kinds_test.go+62 −0
diff --git a/internal/fichas/index.go b/internal/fichas/index.go| index f485571..e8308b2 100644 |
| --- a/internal/fichas/index.go |
| +++ b/internal/fichas/index.go |
| @@ -43,6 +43,9 @@ type Index struct { |
| loaded bool |
| seen int64 // highest event id reflected in the index |
| types map[int64]*typeIndex |
| + // fullLoads counts rebuilds after the first load, so a test can tell |
| + // a named event kind from one that fell through to the rebuild. |
| + fullLoads int |
| } |
| // typeIndex is one card type's cards and definitions. |
| @@ -107,16 +110,31 @@ func (ix *Index) Sync(ctx context.Context) error { |
| return fmt.Errorf("fichas: reading new events: %w", err) |
| } |
| cards := map[int64]bool{} |
| + types := map[int64]bool{} |
| for _, e := range events { |
| + // Each object kind is named, with what it reloads (level-up §9): |
| + // a kind this switch does not name rebuilds everything, the safe |
| + // default, and TestEveryObjectKindIsNamedInSync fails if a kind |
| + // Fichas writes reaches it. |
| switch e.ObjectKind { |
| case ObjectCard: |
| cards[e.ObjectID] = true |
| case ObjectEntry: |
| // The card-level entry_added event carries the card; the |
| // entry's own event adds nothing the index needs. |
| + case ObjectView, ObjectFile, ObjectDraft: |
| + // Nothing the index holds: views are read per request, files |
| + // and drafts are not in it. |
| + case ObjectType, ObjectGroup: |
| + types[e.ObjectID] = true |
| + case ObjectField: |
| + var fd FieldDef |
| + if err := g.Select("card_type_id").First(&fd, e.ObjectID).Error; err != nil { |
| + return fmt.Errorf("fichas: finding field %d's type: %w", e.ObjectID, err) |
| + } |
| + types[fd.CardTypeID] = true |
| default: |
| - // A type or field changed: labels, options or the field |
| - // set itself. Rare, so rebuild rather than patch. |
| + ix.fullLoads++ |
| if err := ix.loadAll(g); err != nil { |
| return err |
| } |
| @@ -124,10 +142,16 @@ func (ix *Index) Sync(ctx context.Context) error { |
| return nil |
| } |
| } |
| + for id := range types { |
| + if err := ix.reloadType(g, id); err != nil { |
| + return err |
| + } |
| + } |
| // Past a burst of this size, one batched full load is cheaper than |
| // reloading card by card (each reload is several queries); an import of |
| // calls from Calls would arrive as exactly such a burst. |
| if len(cards) > reloadBurst { |
| + ix.fullLoads++ |
| if err := ix.loadAll(g); err != nil { |
| return err |
| } |
| @@ -216,6 +240,27 @@ func (ix *Index) loadType(g *gorm.DB, ct CardType) (*typeIndex, error) { |
| } |
| // reloadCard refreshes one card, or drops it if it is gone. |
| +// reloadType reloads one type: its labels, fields and cards. A type |
| +// created since the last load is added; one that no longer exists, or |
| +// is not a card type, is dropped. |
| +func (ix *Index) reloadType(g *gorm.DB, id int64) error { |
| + var ct CardType |
| + err := g.First(&ct, id).Error |
| + if err == gorm.ErrRecordNotFound || (err == nil && ct.Scope != ScopeCard) { |
| + delete(ix.types, id) |
| + return nil |
| + } |
| + if err != nil { |
| + return fmt.Errorf("fichas: reloading type %d: %w", id, err) |
| + } |
| + ti, err := ix.loadType(g, ct) |
| + if err != nil { |
| + return err |
| + } |
| + ix.types[id] = ti |
| + return nil |
| +} |
| + |
| func (ix *Index) reloadCard(g *gorm.DB, id int64) error { |
| var c Card |
| err := g.First(&c, id).Error |
diff --git a/internal/fichas/schema.go b/internal/fichas/schema.go| index b7e0f53..18f35b9 100644 |
| --- a/internal/fichas/schema.go |
| +++ b/internal/fichas/schema.go |
| @@ -34,6 +34,12 @@ import ( |
| const ( |
| ObjectType = "type" |
| ObjectField = "field" |
| + // Object kinds later phases write (level-up §9), named now so the |
| + // index's feed knows each before anything writes it. |
| + ObjectView = "view" |
| + ObjectFile = "file" |
| + ObjectDraft = "draft" |
| + ObjectGroup = "group" |
| ) |
| // Limits, stated so a form can say them. |
diff --git a/internal/fichas/sync_kinds_test.go b/internal/fichas/sync_kinds_test.go| new file mode 100644 |
| index 0000000..e4bf806 |
| --- /dev/null |
| +++ b/internal/fichas/sync_kinds_test.go |
| @@ -0,0 +1,62 @@ |
| +package fichas |
| + |
| +import "testing" |
| + |
| +// TestEveryObjectKindIsNamedInSync is LU12's feed half: every object |
| +// kind Fichas writes is named in Sync's switch, so none falls through |
| +// to the full rebuild, and a view, file or draft event reloads nothing. |
| +// A kind nobody names still rebuilds, the safe default. |
| +// |
| +// Mutation, 2026-11-17: deleting the ObjectField case (so a field event |
| +// fell through) failed this test at the first fullLoads check. |
| +func TestEveryObjectKindIsNamedInSync(t *testing.T) { |
| + g, s := seeded(t) |
| + ix := NewIndex(g, s) |
| + card, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), map[string]string{FieldName: "Cnoc"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + mustQuery(t, ix, Query{}) |
| + |
| + ct, err := CreateType(ctx(), g, s, Person(1), "Project", "Projects") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fd, err := AddField(ctx(), g, s, TypeCustomer, Person(1), FieldSpec{Label: "Budget", Kind: KindNumber, Tier: TierSealed}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := UpdateField(ctx(), g, s, fd.Key, Person(1), "Budget (EUR)", nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, kind := range []string{ObjectView, ObjectFile, ObjectDraft, ObjectGroup} { |
| + if err := g.Create(&Event{Kind: EventUpdated, ObjectKind: kind, ObjectID: ct.ID, ChangedFields: "[]", Nonce: 1}).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + UpdateCard(ctx(), g, s, card.ID, Person(1), map[string]string{FieldName: "Cnoc Teoranta"}) |
| + mustQuery(t, ix, Query{}) |
| + if ix.fullLoads != 0 { |
| + t.Fatalf("a named object kind rebuilt the whole index (%d rebuilds)", ix.fullLoads) |
| + } |
| + fields, _ := ix.Fields(TypeCustomer) |
| + found := false |
| + for _, f := range fields { |
| + found = found || f.Label == "Budget (EUR)" |
| + } |
| + if !found { |
| + t.Fatal("a field's rename did not reach the index") |
| + } |
| + if _, err := ix.Fields(ct.Key); err != nil { |
| + t.Fatalf("a new type did not reach the index: %v", err) |
| + } |
| + if got := names(mustQuery(t, ix, Query{})); len(got) != 1 || got[0] != "Cnoc Teoranta" { |
| + t.Fatalf("a card edit after other kinds: %v", got) |
| + } |
| + |
| + g.Create(&Event{Kind: EventUpdated, ObjectKind: "something-new", ChangedFields: "[]", Nonce: 1}) |
| + mustQuery(t, ix, Query{}) |
| + if ix.fullLoads != 1 { |
| + t.Fatalf("an unnamed kind did not rebuild (%d rebuilds)", ix.fullLoads) |
| + } |
| +} |