feat(storage): add native MySQL namespace and identity pilots

Add resumable checked migrations and explicit schema administration commands. Port ML budgets, namespaces, identity and group persistence with three-backend contracts. Keep MySQL unavailable as the full application backend.
This commit is contained in:
Anya Tokugawa committed 2026-11-30 23:03:24 +09:00
1 parent 0c7897d695
commit 6a67fd9afe
40 files changed
+3087 -94

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
package store
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/google/uuid"
"pageswiki/internal/machinelearning"
)
func TestMachineLearningPolicyPersistence(t *testing.T) {
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL, storeMySQL} {
t.Run(fmt.Sprint(backend), func(t *testing.T) { testMachineLearningPolicyPersistence(t, backend) })
}
}
func testMachineLearningPolicyPersistence(t *testing.T, backend storeSQLBackend) {
s, domain := mlTestStore(t, backend)
ctx := context.Background()
empty, err := s.MachineLearningPolicy(ctx, domain.UUID)
if err != nil || empty.Enabled || empty.Generation != "" {
t.Fatal("not disabled by default", err)
}
p := machinelearning.Policy{Enabled: true, ScopeUUID: uuid.NewString(), Provider: "doc2vec", Model: strings.Repeat("a", 64), Dimensions: 8, Prefixes: []string{"/lab"}, MaxBatchBytes: 6000}
saved, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, "", p)
if err != nil {
t.Fatal(err)
}
if saved.Generation == "" || saved.Generation == p.Generation {
t.Fatal("generation not assigned")
}
if _, err = s.SaveMachineLearningPolicy(ctx, domain.UUID, "", p); !errors.Is(err, ErrMachineLearningConflict) {
t.Fatal("lost update", err)
}
loaded, err := s.MachineLearningPolicy(ctx, domain.UUID)
if err != nil || loaded.Generation != saved.Generation {
t.Fatal("roundtrip", err)
}
other, err := s.MachineLearningPolicy(ctx, uuid.NewString())
if err != nil || other.Enabled {
t.Fatal("cross domain leak", err)
}
p.Enabled = false
disabled, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, saved.Generation, p)
if err != nil {
t.Fatal(err)
}
if disabled.Generation == saved.Generation || disabled.Allows("/lab", saved.Generation) {
t.Fatal("disable did not invalidate")
}
p.Enabled = true
reenabled, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, disabled.Generation, p)
if err != nil {
t.Fatal(err)
}
if reenabled.Allows("/lab", saved.Generation) {
t.Fatal("old consent resurrected")
}
start := make(chan struct{})
results := make(chan error, 2)
for i := 0; i < 2; i++ {
go func() {
<-start
_, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, reenabled.Generation, p)
results <- err
}()
}
close(start)
winners := 0
for i := 0; i < 2; i++ {
err := <-results
if err == nil {
winners++
} else if !errors.Is(err, ErrMachineLearningConflict) {
t.Fatal(err)
}
}
if winners != 1 {
t.Fatal("concurrent configuration lost update", winners)
}
if _, err = s.DB.ExecContext(ctx, `UPDATE config SET value=`+s.sqlParam(1)+` WHERE domain_uuid=`+s.sqlParam(2)+` AND `+s.sqlConfigKey()+`=`+s.sqlParam(3), `{"enabled":true}`, domain.UUID, machineLearningConfigKey); err != nil {
t.Fatal(err)
}
if _, err = s.MachineLearningPolicy(ctx, domain.UUID); !errors.Is(err, machinelearning.ErrPolicy) {
t.Fatal("corrupt policy did not fail closed", err)
}
}