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.
100 lines
7.9 KiB
Go
100 lines
7.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
// These definitions are owned native MySQL DDL, not translations of SQLite
|
|
// migrations. NO PAD binary collation keeps identifiers and Path scopes exact.
|
|
type mysqlAuthColumn struct{ name, kind, defaultSQL string }
|
|
type mysqlAuthTable struct {
|
|
name string
|
|
columns []mysqlAuthColumn
|
|
keys []string // first is primary; remaining keys are unique
|
|
indexes []string
|
|
}
|
|
|
|
func mysqlAuthMigrations() []db.MySQLMigration {
|
|
const id = "varchar(36)"
|
|
const text = "longtext"
|
|
const state = "varchar(32)"
|
|
const number = "bigint"
|
|
tables := []mysqlAuthTable{
|
|
{"principals", []mysqlAuthColumn{{"uuid", id, ""}, {"display_name", text, "('')"}, {"email", text, "('')"}, {"state", state, "'active'"}, {"avatar", "longblob", "(X'')"}, {"avatar_content_type", "varchar(128)", "''"}, {"created_at_ms", number, ""}, {"updated_at_ms", number, ""}}, []string{"uuid"}, nil},
|
|
{"principal_identities", []mysqlAuthColumn{{"uuid", id, ""}, {"principal_uuid", id, ""}, {"provider_type", "varchar(32)", ""}, {"provider_uuid", id, "''"}, {"issuer", "varchar(255)", "''"}, {"subject", "varchar(255)", ""}, {"attributes_json", text, "('{}')"}, {"created_at_ms", number, ""}}, []string{"uuid", "provider_type,provider_uuid,issuer,subject"}, []string{"principal_uuid,provider_type,subject"}},
|
|
{"domain_memberships", []mysqlAuthColumn{{"uuid", id, ""}, {"domain_uuid", id, ""}, {"principal_uuid", id, ""}, {"state", state, "'active'"}, {"created_at_ms", number, ""}}, []string{"uuid", "domain_uuid,principal_uuid"}, []string{"principal_uuid,domain_uuid"}},
|
|
{"auth_roles", []mysqlAuthColumn{{"uuid", id, ""}, {"domain_uuid", id, ""}, {"name", "varchar(128)", ""}, {"description", text, "('')"}, {"created_at_ms", number, ""}}, []string{"uuid", "domain_uuid,name"}, nil},
|
|
{"auth_role_bindings", []mysqlAuthColumn{{"membership_uuid", id, ""}, {"role_uuid", id, ""}, {"created_at_ms", number, ""}}, []string{"membership_uuid,role_uuid"}, nil},
|
|
{"auth_capability_grants", []mysqlAuthColumn{{"uuid", id, ""}, {"role_uuid", id, ""}, {"capability", "varchar(32)", ""}, {"target_type", "varchar(32)", ""}, {"target_id", "varchar(512)", "'*'"}, {"created_at_ms", number, ""}}, []string{"uuid", "role_uuid,capability,target_type,target_id"}, nil},
|
|
{"groups", []mysqlAuthColumn{{"uuid", id, ""}, {"domain_uuid", id, ""}, {"name", "varchar(64)", ""}, {"display_name", text, "('')"}, {"description", text, "('')"}, {"visibility", "varchar(32)", "'members'"}, {"state", state, "'active'"}, {"builtin_key", "varchar(32)", "''"}, {"inherit_members", "int", "0"}, {"created_at_ms", number, ""}, {"updated_at_ms", number, ""}}, []string{"uuid", "domain_uuid,name"}, nil},
|
|
{"group_members", []mysqlAuthColumn{{"group_uuid", id, ""}, {"membership_uuid", id, ""}, {"member_role", "varchar(32)", "'member'"}, {"source", "varchar(128)", "'local'"}, {"created_at_ms", number, ""}}, []string{"group_uuid,membership_uuid"}, []string{"membership_uuid,group_uuid"}},
|
|
{"campus_admin_grants", []mysqlAuthColumn{{"group_uuid", id, ""}, {"membership_uuid", id, ""}, {"source", "varchar(128)", ""}, {"member_role", "varchar(32)", ""}, {"created_at_ms", number, ""}}, []string{"group_uuid,membership_uuid,source"}, []string{"membership_uuid"}},
|
|
{"campus_admin_membership_origins", []mysqlAuthColumn{{"membership_uuid", id, ""}, {"domain_uuid", id, ""}, {"principal_uuid", id, ""}}, []string{"membership_uuid", "domain_uuid,principal_uuid"}, nil},
|
|
{"campus_admin_migrations", []mysqlAuthColumn{{"key", "varchar(128)", ""}}, []string{"key"}, nil},
|
|
{"auth_sessions", []mysqlAuthColumn{{"uuid", id, ""}, {"token_hash", "varchar(128)", ""}, {"principal_uuid", id, ""}, {"domain_uuid", id, ""}, {"auth_time_ms", number, ""}, {"expires_at_ms", number, ""}, {"last_seen_at_ms", number, ""}, {"mfa_level", "int", "0"}, {"revoked_at_ms", number, "0"}}, []string{"uuid", "token_hash"}, []string{"domain_uuid,principal_uuid,revoked_at_ms"}},
|
|
{"group_permissions", []mysqlAuthColumn{{"uuid", id, ""}, {"group_uuid", id, ""}, {"scope_path", "varchar(512)", ""}, {"permission", "varchar(32)", ""}, {"created_at_ms", number, ""}}, []string{"uuid", "group_uuid,scope_path"}, nil},
|
|
{"auth_credentials", []mysqlAuthColumn{{"uuid", id, ""}, {"principal_uuid", id, ""}, {"kind", "varchar(32)", ""}, {"identifier", "varchar(255)", "''"}, {"secret_hash", text, ""}, {"parameters_json", text, "('{}')"}, {"enabled", "int", "1"}, {"created_at_ms", number, ""}, {"updated_at_ms", number, ""}}, []string{"uuid", "kind,identifier"}, nil},
|
|
}
|
|
steps := []db.MySQLMigration{
|
|
mysqlAuthTableMigration(1, "identity membership and group foundation", tables),
|
|
mysqlAuthTableMigration(2, "authentication challenges and provider configuration", []mysqlAuthTable{
|
|
{"auth_challenges", []mysqlAuthColumn{{"uuid", id, ""}, {"domain_uuid", id, ""}, {"principal_uuid", id, "''"}, {"kind", state, ""}, {"token_hash", "varchar(128)", ""}, {"payload_json", text, "('{}')"}, {"expires_at_ms", number, ""}, {"consumed_at_ms", number, "0"}, {"attempt_count", "int", "0"}, {"created_at_ms", number, ""}}, []string{"uuid", "token_hash"}, []string{"expires_at_ms"}},
|
|
{"auth_providers", []mysqlAuthColumn{{"uuid", id, ""}, {"domain_uuid", id, ""}, {"kind", state, ""}, {"config_json", text, "('{}')"}, {"secret_encrypted", text, "('')"}, {"enabled", "int", "0"}, {"created_at_ms", number, ""}, {"updated_at_ms", number, ""}}, []string{"uuid", "domain_uuid,kind"}, nil},
|
|
}),
|
|
}
|
|
// Inspect validates the latest contract only, since later versions may
|
|
// replace older columns. This additive version must retain all v1 checks.
|
|
steps[1].Assertions = append(append([]string{}, steps[0].Assertions...), steps[1].Assertions...)
|
|
return steps
|
|
}
|
|
|
|
// Keep generated version 1 operations/assertions byte-identical: deployed
|
|
// journals checksum the plan. Extend with new versions, never rewrite history.
|
|
func mysqlAuthTableMigration(version int, name string, tables []mysqlAuthTable) db.MySQLMigration {
|
|
var ops []db.MySQLOperation
|
|
var checks []string
|
|
for _, table := range tables {
|
|
var definitions, signature []string
|
|
for _, col := range table.columns {
|
|
definition := "`" + col.name + "` " + col.kind + " NOT NULL"
|
|
if col.defaultSQL != "" {
|
|
definition += " DEFAULT " + col.defaultSQL
|
|
}
|
|
definitions = append(definitions, definition)
|
|
collation := ""
|
|
if strings.Contains(col.kind, "char") || strings.Contains(col.kind, "text") {
|
|
collation = "utf8mb4_0900_bin"
|
|
}
|
|
signature = append(signature, col.name+":"+col.kind+":NO:"+collation)
|
|
}
|
|
definitions = append(definitions, "PRIMARY KEY("+mysqlAuthIdentifiers(table.keys[0])+")")
|
|
check := mysqlMLTableCheck(table.name, strings.Join(signature, "|"), table.keys[0], 0)
|
|
for i, key := range table.keys[1:] {
|
|
name := fmt.Sprintf("uq_%d", i)
|
|
definitions = append(definitions, "UNIQUE KEY "+name+"("+mysqlAuthIdentifiers(key)+")")
|
|
check += fmt.Sprintf(` AND (SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',') FROM information_schema.statistics WHERE table_schema=DATABASE() AND table_name='%s' AND index_name='%s' AND non_unique=0)='%s'`, table.name, name, key)
|
|
}
|
|
for i, key := range table.indexes {
|
|
definitions = append(definitions, fmt.Sprintf("INDEX ix_%d(%s)", i, mysqlAuthIdentifiers(key)))
|
|
}
|
|
statement := "CREATE TABLE `" + table.name + "`(" + strings.Join(definitions, ",") + ") ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin"
|
|
ops = append(ops, db.MySQLOperation{SQL: statement, Check: check, ReplaySafe: true})
|
|
checks = append(checks, check)
|
|
}
|
|
return db.MySQLMigration{Version: version, Name: name, Operations: ops, Assertions: checks}
|
|
}
|
|
func mysqlAuthIdentifiers(names string) string {
|
|
return "`" + strings.ReplaceAll(names, ",", "`,`") + "`"
|
|
}
|
|
|
|
func (s *Store) ensureMySQLAuthFoundation(ctx context.Context) error {
|
|
if err := NewMySQLNamespaceRepository(s.DB).PrepareSchema(ctx); err != nil {
|
|
return err
|
|
}
|
|
return db.ApplyMySQLMigrations(ctx, s.DB, "store-auth-core", mysqlAuthMigrations())
|
|
}
|