Files
pages/cmd/pageswiki/mysql_ml_schema_test.go
T
Anya Tokugawa 6a67fd9afe 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.
2026-11-30 23:03:24 +09:00

110 lines
3.1 KiB
Go

package main
import (
"bytes"
"context"
"os"
"strings"
"testing"
"time"
"github.com/go-sql-driver/mysql"
"github.com/google/uuid"
"pageswiki/internal/db"
)
func TestMySQLMLSchemaArguments(t *testing.T) {
t.Setenv("PAGESWIKI_MYSQL_DSN", "")
for _, args := range [][]string{{"bad"}, {"apply", "--timeout", "0s"}, {"resume", "unexpected"}, {"status"}} {
var out bytes.Buffer
if runMySQLMLSchema(args, &out) == nil {
t.Fatal("accepted invalid arguments", args)
}
}
var out bytes.Buffer
if e := runMySQLMLSchema([]string{"--help"}, &out); e != nil || !strings.Contains(out.String(), "ML component ONLY") {
t.Fatal(e)
}
}
func TestMySQLMLSchemaCommand(t *testing.T) {
for _, component := range []string{"ml", "namespace", "identity"} {
t.Run(component, func(t *testing.T) { testMySQLSchemaCommand(t, component) })
}
}
func testMySQLSchemaCommand(t *testing.T, component string) {
dsn := os.Getenv("PAGESWIKI_TEST_MYSQL_DSN")
if dsn == "" {
t.Skip("set disposable MySQL DSN")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
admin, e := db.OpenMySQL(ctx, db.MySQLOptions{DSN: dsn, AllowInsecure: true})
if e != nil {
t.Fatal(e)
}
defer admin.Close()
name := "pages_ml_cmd_" + strings.ReplaceAll(uuid.NewString(), "-", "")
if _, e = admin.ExecContext(ctx, "CREATE DATABASE "+name+" CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"); e != nil {
t.Fatal(e)
}
defer func() {
if _, e := admin.Exec("DROP DATABASE " + name); e != nil {
t.Error(e)
}
}()
c, e := mysql.ParseDSN(dsn)
if e != nil {
t.Fatal(e)
}
c.DBName = name
t.Setenv("PAGESWIKI_MYSQL_DSN", c.FormatDSN())
var out bytes.Buffer
run := func(action string) error {
return runMySQLSchemaCommand([]string{action, "--allow-insecure"}, &out, component)
}
pending := `"pending":2`
if component == "namespace" {
pending = `"pending":3`
} else if component == "identity" {
pending = `"pending":5`
}
if e = run("status"); e != nil || !strings.Contains(out.String(), pending) {
t.Fatal(out.String(), e)
}
var count int
if e = admin.QueryRowContext(ctx, `SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=?`, name).Scan(&count); e != nil || count != 0 {
t.Fatal("status wrote schema", count, e)
}
if e = run("check"); e == nil {
t.Fatal("pending check succeeded")
}
if e = run("apply"); e != nil {
t.Fatal(e)
}
if e = run("check"); e != nil {
t.Fatal(e)
}
if component != "ml" {
if e = admin.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+name+".domains").Scan(&count); e != nil || count != 0 {
t.Fatal("schema command created unauthorized Domain data", count, e)
}
}
ledger, version := "machine-learning", 2
if component == "namespace" {
ledger, version = "store-namespaces", 1
} else if component == "identity" {
ledger, version = "store-auth-core", 2
}
if _, e = admin.ExecContext(ctx, "UPDATE "+name+".schema_mysql_migrations SET state='running',next_operation=1 WHERE component=? AND version=?", ledger, version); e != nil {
t.Fatal(e)
}
if e = run("apply"); e == nil {
t.Fatal("dirty apply succeeded")
}
if e = run("resume"); e != nil {
t.Fatal(e)
}
}