package store import ( "context" _ "embed" "fmt" "strings" "pageswiki/internal/db" ) //go:embed mysql_ml_schema.sql var mysqlMLSchema string // All identifiers/signatures are owned constants. Check column shape, PK order, // engine and CHECK count. This is not a comparison of all constraint semantics. func mysqlMLTableCheck(table, columns, pk string, checks int) string { return fmt.Sprintf(`SELECT (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='%s' AND engine='InnoDB')=1 AND (SELECT GROUP_CONCAT(CONCAT(column_name,':',column_type,':',is_nullable,':',COALESCE(collation_name,'')) ORDER BY ordinal_position SEPARATOR '|') FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='%s')='%s' 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='PRIMARY')='%s' AND (SELECT COUNT(*) FROM information_schema.table_constraints WHERE constraint_schema=DATABASE() AND table_name='%s' AND constraint_type='CHECK')=%d`, table, table, columns, table, pk, table, checks) } var mysqlMLChecks = []string{ mysqlMLTableCheck("config", "domain_uuid:varchar(36):NO:ascii_bin|key:varchar(128):NO:ascii_bin|value:longtext:NO:utf8mb4_bin|updated_at:bigint:NO:", "domain_uuid,key", 0), mysqlMLTableCheck("ml_daily_budget", "domain_uuid:varchar(36):NO:ascii_bin|day:char(10):NO:ascii_bin|reserved_bytes:bigint:NO:", "domain_uuid,day", 1), mysqlMLTableCheck("ml_budget_receipts", "domain_uuid:varchar(36):NO:ascii_bin|request_uuid:varchar(36):NO:ascii_bin|day:char(10):NO:ascii_bin|input_hash:char(64):NO:ascii_bin|generation:varchar(36):NO:ascii_bin|reserved_bytes:bigint:NO:|state:varchar(16):NO:ascii_bin|actual_tokens:bigint:NO:", "domain_uuid,request_uuid", 3), } func mysqlMLMigrations() []db.MySQLMigration { var ops []db.MySQLOperation // This owned bundle contains three CREATE TABLE statements without stored // procedures or semicolon-containing literals. Not a general SQL parser. for _, statement := range strings.Split(mysqlMLSchema, ";") { if strings.TrimSpace(statement) != "" { ops = append(ops, db.MySQLOperation{SQL: strings.TrimSpace(statement), Check: mysqlMLChecks[len(ops)], ReplaySafe: true}) } } // Preserve the v1 checksum. Core configuration needs Unicode keys and // NO PAD comparisons ("key" and "key " must remain distinct). configV2 := mysqlMLTableCheck("config", "domain_uuid:varchar(36):NO:ascii_bin|key:varchar(255):NO:utf8mb4_0900_bin|value:longtext:NO:utf8mb4_bin|updated_at:bigint:NO:", "domain_uuid,key", 0) return []db.MySQLMigration{ {Version: 1, Name: "ML configuration and reservations", Operations: ops, Assertions: mysqlMLChecks}, {Version: 2, Name: "Unicode exact configuration keys", Operations: []db.MySQLOperation{{SQL: "ALTER TABLE config MODIFY COLUMN `key` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NOT NULL", Check: configV2, ReplaySafe: true}}, Assertions: []string{configV2, mysqlMLChecks[1], mysqlMLChecks[2]}}, } } func (r *MySQLMLRepository) PrepareSchema(ctx context.Context) error { return db.ApplyMySQLMigrations(ctx, r.store.DB, "machine-learning", mysqlMLMigrations()) } func (r *MySQLMLRepository) InspectSchema(ctx context.Context) (db.MySQLMigrationStatus, error) { return db.InspectMySQLMigrations(ctx, r.store.DB, "machine-learning", mysqlMLMigrations()) } func (r *MySQLMLRepository) ResumeSchema(ctx context.Context) error { return db.ResumeMySQLMigrations(ctx, r.store.DB, "machine-learning", mysqlMLMigrations()) }