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:
1 parent
0c7897d695
commit
6a67fd9afe
40 files changed
+3087
-94
No files matched your search
@@ -15,6 +15,24 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "mysql-identity-schema" {
|
||||
if err := runMySQLSchemaCommand(os.Args[2:], os.Stdout, "identity"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "mysql-namespace-schema" {
|
||||
if err := runMySQLComponentSchema(os.Args[2:], os.Stdout, true); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "mysql-ml-schema" {
|
||||
if err := runMySQLMLSchema(os.Args[2:], os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate-postgres" {
|
||||
if err := runPostgreSQLMigration(os.Args[2:], os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func runMySQLMLSchema(args []string, out io.Writer) error {
|
||||
return runMySQLComponentSchema(args, out, false)
|
||||
}
|
||||
|
||||
func runMySQLComponentSchema(args []string, out io.Writer, namespace bool) error {
|
||||
component := "ml"
|
||||
if namespace {
|
||||
component = "namespace"
|
||||
}
|
||||
return runMySQLSchemaCommand(args, out, component)
|
||||
}
|
||||
func runMySQLSchemaCommand(args []string, out io.Writer, component string) error {
|
||||
command, scope, label := "mysql-ml-schema", "ml-only", "ML component ONLY"
|
||||
if component == "namespace" {
|
||||
command, scope, label = "mysql-namespace-schema", "namespace-pilot", "Namespace pilot ONLY (includes shared config/ML schema; no Domain mutations or authentication)"
|
||||
} else if component == "identity" {
|
||||
command, scope, label = "mysql-identity-schema", "identity-pilot", "Identity/group persistence pilot ONLY; not a complete authentication server"
|
||||
} else if component != "ml" {
|
||||
return errors.New("unknown MySQL component")
|
||||
}
|
||||
usage := "pageswiki " + command + " {status|check|apply|resume} [--timeout 2m] [--allow-insecure]\nUses PAGESWIKI_MYSQL_DSN. " + label + "; does not make Pages serve from MySQL. status/check are read-only. apply refuses interrupted migrations; resume explicitly verifies and continues."
|
||||
if len(args) == 0 || args[0] == "--help" {
|
||||
_, e := fmt.Fprintln(out, usage)
|
||||
return e
|
||||
}
|
||||
mode := args[0]
|
||||
if mode != "status" && mode != "check" && mode != "apply" && mode != "resume" {
|
||||
return errors.New("invalid MySQL component schema action")
|
||||
}
|
||||
f := flag.NewFlagSet(command, flag.ContinueOnError)
|
||||
f.SetOutput(out)
|
||||
timeout := f.Duration("timeout", 2*time.Minute, "operation deadline")
|
||||
insecure := f.Bool("allow-insecure", false, "isolated disposable database tests only")
|
||||
if e := f.Parse(args[1:]); e != nil {
|
||||
if errors.Is(e, flag.ErrHelp) {
|
||||
return nil
|
||||
}
|
||||
return e
|
||||
}
|
||||
if f.NArg() != 0 || *timeout <= 0 {
|
||||
return errors.New("invalid MySQL component schema arguments")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
||||
defer cancel()
|
||||
database, e := db.OpenMySQL(ctx, db.MySQLOptions{DSN: os.Getenv("PAGESWIKI_MYSQL_DSN"), AllowInsecure: *insecure})
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
defer database.Close()
|
||||
var repo interface {
|
||||
PrepareSchema(context.Context) error
|
||||
InspectSchema(context.Context) (db.MySQLMigrationStatus, error)
|
||||
ResumeSchema(context.Context) error
|
||||
} = store.NewMySQLMLRepository(database)
|
||||
if component == "namespace" {
|
||||
repo = store.NewMySQLNamespaceRepository(database)
|
||||
} else if component == "identity" {
|
||||
repo = store.NewMySQLIdentityRepository(database)
|
||||
}
|
||||
switch mode {
|
||||
case "apply":
|
||||
e = repo.PrepareSchema(ctx)
|
||||
case "resume":
|
||||
e = repo.ResumeSchema(ctx)
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
status, e := repo.InspectSchema(ctx)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if e = json.NewEncoder(out).Encode(struct {
|
||||
Scope string `json:"scope"`
|
||||
db.MySQLMigrationStatus
|
||||
}{scope, status}); e != nil {
|
||||
return e
|
||||
}
|
||||
if mode == "check" && (status.Dirty || status.Pending > 0) {
|
||||
return errors.New("MySQL component schema is pending or interrupted")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user