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:
Anya Tokugawa committed 2026-11-30 23:03:24 +09:00
1 parent 0c7897d695
commit 6a67fd9afe
40 files changed
+3087 -94

No files matched your search

+38
View File
@@ -18,6 +18,8 @@ type storeSQLBackend uint8
const (
storeSQLite storeSQLBackend = iota
storePostgreSQL
// Restricted to ported component facades; never used for serving Pages.
storeMySQL
)
var ErrPostgreSQLComponentNotReady = errors.New("PostgreSQL component dependencies are not ported")
@@ -41,6 +43,26 @@ func (s *Store) sqlValues(n int) string {
}
return strings.Join(parts, ",")
}
func (s *Store) sqlConfigKey() string {
if s.sqlBackend == storeMySQL {
return "`key`"
}
return "key"
}
func (s *Store) sqlGroupTable() string {
if s.sqlBackend == storeMySQL {
return "`groups`"
}
return "groups"
}
// Owned identifiers only. This is a statement suffix, not SQL rewriting.
func (s *Store) sqlConflictDoNothing(table, primary, conflict string) string {
if s.sqlBackend == storeMySQL {
return " ON DUPLICATE KEY UPDATE `" + primary + "`=`" + table + "`.`" + primary + "`"
}
return " ON CONFLICT(" + conflict + ") DO NOTHING"
}
func (s *Store) sqlHasDot(expression string) string {
if s.sqlBackend == storePostgreSQL {
return "strpos(" + expression + ", '.') > 0"
@@ -48,6 +70,15 @@ func (s *Store) sqlHasDot(expression string) string {
return "instr(" + expression + ", '.') > 0"
}
// Only owned SQL expressions, never user input. MySQL's || is logical OR
// without a special sql_mode; do not change session modes to emulate SQLite.
func (s *Store) sqlConcat(expressions ...string) string {
if s.sqlBackend == storeMySQL {
return "CONCAT(" + strings.Join(expressions, ",") + ")"
}
return strings.Join(expressions, " || ")
}
// Owned expressions only. C/BINARY ordering makes '%' and '_' literal and
// keeps segment ranges case-sensitive, independent of database locale.
func (s *Store) pathScopeSQL(parent, child string) string {
@@ -70,6 +101,9 @@ func (s *Store) sqlStringPosition(expression, literal string) string {
}
func (s *Store) sqlError(operation string, err error) error {
if s.sqlBackend == storeMySQL {
return mysqlStoreError(operation, err)
}
if s.sqlBackend != storePostgreSQL || err == nil {
return err
}
@@ -143,6 +177,10 @@ func (s *Store) administrativeWrite(ctx context.Context, sqliteReservation strin
defer tx.Rollback()
if s.sqlBackend == storePostgreSQL {
_, err = tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653, 1853058931)`)
} else if s.sqlBackend == storeMySQL {
// A real row lock, held until commit/rollback. A WHERE 0 update does
// not reserve a writer on InnoDB. Insert also covers first-use races.
_, err = tx.ExecContext(ctx, `INSERT INTO namespace_write_lock(id) VALUES(1) ON DUPLICATE KEY UPDATE id=1`)
} else {
_, err = tx.ExecContext(ctx, sqliteReservation)
}