feat: keep the materials index in sync and add bm:materials-reindex

- the materials admin syncs after create and update and removes after
  delete, once the save transaction commits (UseMaterialIndex, set at Boot)
- POST and PUT /api/v1/bm/materials sync after the transaction, DELETE
  removes the document (its statement has no primary key, so no callback)
- bm:materials-reindex [--fresh] [--if-configured] ensures the index and
  syncs every material; it refuses without an engine and runs nightly
  at 04:15 with --if-configured
- TestMaterialSearchAgainstDatabase (Postgres) is written for the final
  verification phase
This commit is contained in:
Jakub Zych
2026-11-08 19:50:50 +01:00
parent 4ffce6e6ac
commit ffd19342b2
12 changed files with 624 additions and 12 deletions

View File

@@ -27,8 +27,8 @@ const (
)
// testConfig is the application config the tests boot with: the HTTP
// limits, the golem15.user JWT secret and the admin secret.
func testConfig(t *testing.T) *compass.Config {
// limits, the golem15.user JWT secret and the admin secret, plus env.
func testConfig(t *testing.T, env ...string) *compass.Config {
t.Helper()
dir := t.TempDir()
httpYAML := "body_limits:\n default_bytes: 1048576\n upload_bytes: 67108864\n"
@@ -37,11 +37,11 @@ func testConfig(t *testing.T) *compass.Config {
}
cfg, err := compass.Open(compass.Options{
Dir: dir,
Environ: []string{
Environ: append([]string{
"SUMMER_ENV=testing",
"SUMMER_GOLEM15__USER__JWT__SECRET=" + testUserSecret,
"SUMMER_ADMIN__JWT__SECRET=" + testAdminSecret,
},
}, env...),
})
if err != nil {
t.Fatal(err)
@@ -85,6 +85,20 @@ func TestPluginActivates(t *testing.T) {
if !slices.ContainsFunc(p.Commands(), func(c bonfire.Command) bool { return c.Name == "bm:seed" }) {
t.Error("bm:seed is not registered")
}
if !slices.ContainsFunc(p.Commands(), func(c bonfire.Command) bool { return c.Name == "bm:materials-reindex" }) {
t.Error("bm:materials-reindex is not registered")
}
var scheduled bool
for _, e := range p.Schedule() {
if e.Command == "bm:materials-reindex" && slices.Equal(e.Args, []string{"--if-configured"}) {
if h, m, daily := e.Cadence.At(); daily && h == 4 && m == 15 {
scheduled = true
}
}
}
if !scheduled {
t.Errorf("Schedule = %+v, want bm:materials-reindex --if-configured daily at 04:15", p.Schedule())
}
if got := app.Config.Int("jz.bm.materials_per_page"); got != 20 {
t.Errorf("jz.bm.materials_per_page = %d, want the default 20", got)
}