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
@@ -0,0 +1,97 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// All helpers are called under the same transactional administrative lock as
|
||||
// SQLite/PostgreSQL. Never use MySQL RowsAffected to infer existence on UPSERT:
|
||||
// unchanged/update/insert can report 0/2/1 respectively.
|
||||
func (s *Store) ensureMySQLBuiltinGroups(ctx context.Context, tx *sql.Tx) error {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT uuid FROM domains ORDER BY uuid`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var domains []string
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
domains = append(domains, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
for _, domain := range domains {
|
||||
for _, key := range []string{"campus_admin", "domain_admin"} {
|
||||
inherit := 0
|
||||
if key == "campus_admin" {
|
||||
inherit = 1
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, "INSERT INTO `groups`(uuid,domain_uuid,name,display_name,description,visibility,state,builtin_key,inherit_members,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,'members','active',?,?,?,?) ON DUPLICATE KEY UPDATE builtin_key=?", uuid.NewString(), domain, key, strings.ReplaceAll(key, "_", " "), "Built-in administrative group", key, inherit, now, now, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO group_members(group_uuid,membership_uuid,member_role,source,created_at_ms)
|
||||
SELECT g.uuid,m.uuid,'owner','migration',? FROM `+"`groups`"+` g
|
||||
JOIN domain_memberships m ON m.domain_uuid=g.domain_uuid
|
||||
JOIN auth_role_bindings b ON b.membership_uuid=m.uuid
|
||||
JOIN auth_roles r ON r.uuid=b.role_uuid AND r.domain_uuid=m.domain_uuid
|
||||
JOIN auth_capability_grants c ON c.role_uuid=r.uuid AND c.capability='manage' AND c.target_type='domain' AND (c.target_id=m.domain_uuid OR c.target_id='*')
|
||||
WHERE g.domain_uuid=? AND g.builtin_key='domain_admin'
|
||||
ON DUPLICATE KEY UPDATE group_uuid=group_members.group_uuid`, now, domain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := s.seedCampusAdminRoleGrantsContext(ctx, tx, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.projectCampusAdminMembershipsContext(ctx, tx)
|
||||
}
|
||||
|
||||
func mysqlAddGroupMember(ctx context.Context, tx *sql.Tx, group, member, role string) error {
|
||||
var previous string
|
||||
err := tx.QueryRowContext(ctx, `SELECT member_role FROM group_members WHERE group_uuid=? AND membership_uuid=?`, group, member).Scan(&previous)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
if previous == "owner" && role != "owner" {
|
||||
return namespaceValidationError("owner role cannot be downgraded by a member update")
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO group_members(group_uuid,membership_uuid,member_role,source,created_at_ms) VALUES(?,?,?,'local',?) ON DUPLICATE KEY UPDATE member_role=?,source='local'`, group, member, role, time.Now().UnixMilli(), role)
|
||||
return err
|
||||
}
|
||||
|
||||
func mysqlRemoveGroupMember(ctx context.Context, tx *sql.Tx, group, member string, authority bool) error {
|
||||
var role string
|
||||
if err := tx.QueryRowContext(ctx, `SELECT member_role FROM group_members WHERE group_uuid=? AND membership_uuid=?`, group, member).Scan(&role); err != nil {
|
||||
return err
|
||||
}
|
||||
if !authority && role != "member" {
|
||||
return namespaceValidationError("member removal requires sufficient authority")
|
||||
}
|
||||
if role == "owner" {
|
||||
var other bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM group_members gm JOIN `+"`groups`"+` g ON g.uuid=gm.group_uuid JOIN domain_memberships m ON m.uuid=gm.membership_uuid AND m.domain_uuid=g.domain_uuid WHERE gm.group_uuid=? AND gm.membership_uuid!=? AND gm.member_role='owner')`, group, member).Scan(&other); err != nil {
|
||||
return err
|
||||
}
|
||||
if !other {
|
||||
return namespaceValidationError("member removal requires at least one remaining owner")
|
||||
}
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `DELETE FROM group_members WHERE group_uuid=? AND membership_uuid=?`, group, member)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user