Files
pages/cmd/pageswiki/mysql_ml_schema.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

100 lines
3.1 KiB
Go

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
}