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.
69 lines
5.0 KiB
Go
69 lines
5.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
func mysqlNamespaceMigrations() []db.MySQLMigration {
|
|
// config remains owned by the existing machine-learning migration ledger;
|
|
// do not adopt it into a second history or change the published v1 checksum.
|
|
statements := []string{
|
|
`CREATE TABLE campuses(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,uuid VARCHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL UNIQUE,name VARCHAR(255) NOT NULL UNIQUE,title LONGTEXT NOT NULL,default_lang VARCHAR(64) NOT NULL DEFAULT 'en',authority_mode VARCHAR(32) NOT NULL DEFAULT 'standalone',fqdn VARCHAR(253) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'localhost',controller_transport VARCHAR(8) NOT NULL DEFAULT 'https',last_contact_at BIGINT NOT NULL DEFAULT 0,last_contact_error TEXT NOT NULL DEFAULT (''),enabled INTEGER NOT NULL DEFAULT 1,created_at BIGINT NOT NULL) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin`,
|
|
`CREATE TABLE domains(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,uuid VARCHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL UNIQUE,name VARCHAR(255) NOT NULL UNIQUE,title LONGTEXT NOT NULL,domain VARCHAR(253) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',enabled INTEGER NOT NULL DEFAULT 1,created_at BIGINT NOT NULL,campus_uuid VARCHAR(36) CHARACTER SET ascii COLLATE ascii_bin,placement VARCHAR(32) NOT NULL DEFAULT 'local',instance_fqdn VARCHAR(253) NOT NULL DEFAULT '',instance_fingerprint VARCHAR(255) NOT NULL DEFAULT '',INDEX idx_domains_campus_uuid(campus_uuid)) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin`,
|
|
`CREATE TABLE namespace_write_lock(id INTEGER NOT NULL PRIMARY KEY,CHECK(id=1)) ENGINE=InnoDB`,
|
|
}
|
|
// The postconditions cover columns, keys, identities and the lock table.
|
|
checks := []string{
|
|
mysqlNamespaceTableCheck("campuses", "id:bigint:NO:|uuid:varchar(36):NO:ascii_bin|name:varchar(255):NO:utf8mb4_0900_bin|title:longtext:NO:utf8mb4_0900_bin|default_lang:varchar(64):NO:utf8mb4_0900_bin|authority_mode:varchar(32):NO:utf8mb4_0900_bin|fqdn:varchar(253):NO:ascii_bin|controller_transport:varchar(8):NO:utf8mb4_0900_bin|last_contact_at:bigint:NO:|last_contact_error:text:NO:utf8mb4_0900_bin|enabled:int:NO:|created_at:bigint:NO:"),
|
|
mysqlNamespaceTableCheck("domains", "id:bigint:NO:|uuid:varchar(36):NO:ascii_bin|name:varchar(255):NO:utf8mb4_0900_bin|title:longtext:NO:utf8mb4_0900_bin|domain:varchar(253):NO:ascii_bin|enabled:int:NO:|created_at:bigint:NO:|campus_uuid:varchar(36):YES:ascii_bin|placement:varchar(32):NO:utf8mb4_0900_bin|instance_fqdn:varchar(253):NO:utf8mb4_0900_bin|instance_fingerprint:varchar(255):NO:utf8mb4_0900_bin"),
|
|
mysqlMLTableCheck("namespace_write_lock", "id:int:NO:", "id", 1),
|
|
}
|
|
var ops []db.MySQLOperation
|
|
for i, statement := range statements {
|
|
ops = append(ops, db.MySQLOperation{SQL: statement, Check: checks[i], ReplaySafe: true})
|
|
}
|
|
return []db.MySQLMigration{{Version: 1, Name: "Campus and Domain read model", Operations: ops, Assertions: checks}}
|
|
}
|
|
|
|
// Owned identifiers only. No user SQL is interpolated here.
|
|
func mysqlNamespaceTableCheck(table, columns string) string {
|
|
return mysqlMLTableCheck(table, columns, "id", 0) + ` AND (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='` + table + `' AND column_name='id' AND extra='auto_increment')=1
|
|
AND (SELECT COUNT(*) FROM (SELECT index_name FROM information_schema.statistics WHERE table_schema=DATABASE() AND table_name='` + table + `' AND non_unique=0 GROUP BY index_name HAVING COUNT(*)=1 AND MAX(column_name) IN ('uuid','name')) AS unique_keys)=2`
|
|
}
|
|
func (r *MySQLNamespaceRepository) PrepareSchema(ctx context.Context) error {
|
|
if err := NewMySQLMLRepository(r.store.DB).PrepareSchema(ctx); err != nil {
|
|
return err
|
|
}
|
|
return db.ApplyMySQLMigrations(ctx, r.store.DB, "store-namespaces", mysqlNamespaceMigrations())
|
|
}
|
|
func (r *MySQLNamespaceRepository) InspectSchema(ctx context.Context) (db.MySQLMigrationStatus, error) {
|
|
base, err := NewMySQLMLRepository(r.store.DB).InspectSchema(ctx)
|
|
if err != nil {
|
|
return db.MySQLMigrationStatus{}, err
|
|
}
|
|
namespace, err := db.InspectMySQLMigrations(ctx, r.store.DB, "store-namespaces", mysqlNamespaceMigrations())
|
|
if err != nil {
|
|
return namespace, err
|
|
}
|
|
// Counts include dependencies; identify the first component to repair.
|
|
combined := db.MySQLMigrationStatus{Applied: base.Applied + namespace.Applied, Pending: base.Pending + namespace.Pending, Dirty: base.Dirty || namespace.Dirty}
|
|
if base.Dirty {
|
|
combined.DirtyComponent, combined.NextOperation = base.DirtyComponent, base.NextOperation
|
|
} else if namespace.Dirty {
|
|
combined.DirtyComponent, combined.NextOperation = namespace.DirtyComponent, namespace.NextOperation
|
|
}
|
|
return combined, nil
|
|
}
|
|
func (r *MySQLNamespaceRepository) ResumeSchema(ctx context.Context) error {
|
|
status, err := NewMySQLMLRepository(r.store.DB).InspectSchema(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status.Dirty || status.Pending != 0 {
|
|
return db.ErrMySQLMigrationMismatch
|
|
}
|
|
return db.ResumeMySQLMigrations(ctx, r.store.DB, "store-namespaces", mysqlNamespaceMigrations())
|
|
}
|