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.
167 lines
7.2 KiB
Go
167 lines
7.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
func mysqlProbe(name string) db.MySQLOperation {
|
|
return db.MySQLOperation{SQL: "CREATE TABLE " + name + "(id BIGINT PRIMARY KEY) ENGINE=InnoDB", Check: "SELECT COUNT(*)=1 FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='" + name + "'", ReplaySafe: true}
|
|
}
|
|
|
|
func TestMySQLMigrationRecovery(t *testing.T) {
|
|
s, _ := mlTestStore(t, storeMySQL)
|
|
ctx := context.Background()
|
|
a, b := mysqlProbe("migration_a"), mysqlProbe("migration_b")
|
|
// Second operation fails until an operator repairs the missing prerequisite.
|
|
b.SQL = "CREATE TABLE migration_b(id BIGINT PRIMARY KEY, parent BIGINT, FOREIGN KEY(parent) REFERENCES migration_parent(id)) ENGINE=InnoDB"
|
|
steps := []db.MySQLMigration{{Version: 1, Name: "recovery", Operations: []db.MySQLOperation{a, b}, Assertions: []string{a.Check, b.Check}}}
|
|
status, e := db.InspectMySQLMigrations(ctx, s.DB, "recovery", steps)
|
|
if e != nil || status.Applied != 0 || status.Pending != 1 {
|
|
t.Fatal(status, e)
|
|
}
|
|
var count int
|
|
if e = s.DB.QueryRow(`SELECT COUNT(*) FROM schema_mysql_migrations WHERE component='recovery'`).Scan(&count); e != nil || count != 0 {
|
|
t.Fatal("inspection wrote history", e)
|
|
}
|
|
if e = db.ApplyMySQLMigrations(ctx, s.DB, "recovery", steps); e == nil {
|
|
t.Fatal("expected DDL failure")
|
|
}
|
|
status, e = db.InspectMySQLMigrations(ctx, s.DB, "recovery", steps)
|
|
if e != nil || !status.Dirty || status.NextOperation != 1 {
|
|
t.Fatal("missing dirty progress", status, e)
|
|
}
|
|
if e = db.ApplyMySQLMigrations(ctx, s.DB, "recovery", steps); !errors.Is(e, db.ErrMySQLMigrationDirty) {
|
|
t.Fatal("silent retry", e)
|
|
}
|
|
if _, e = s.DB.Exec(`CREATE TABLE migration_parent(id BIGINT PRIMARY KEY) ENGINE=InnoDB`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = db.ResumeMySQLMigrations(ctx, s.DB, "recovery", steps); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = db.ApplyMySQLMigrations(ctx, s.DB, "recovery", steps); e != nil {
|
|
t.Fatal("repeat apply", e)
|
|
}
|
|
// Simulate loss of journal acknowledgement after DDL committed. Resume must
|
|
// verify the postcondition and skip CREATE, not blindly replay it.
|
|
if _, e = s.DB.Exec(`UPDATE schema_mysql_migrations SET state='running',next_operation=0 WHERE component='recovery'`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = db.ResumeMySQLMigrations(ctx, s.DB, "recovery", steps); e != nil {
|
|
t.Fatal("DDL-before-journal recovery", e)
|
|
}
|
|
changed := append([]db.MySQLMigration(nil), steps...)
|
|
changed[0].Name = "changed"
|
|
if e = db.ApplyMySQLMigrations(ctx, s.DB, "recovery", changed); !errors.Is(e, db.ErrMySQLMigrationMismatch) {
|
|
t.Fatal("checksum mismatch accepted", e)
|
|
}
|
|
// Unknown future version must not be silently downgraded.
|
|
if _, e = s.DB.Exec(`UPDATE schema_mysql_migrations SET version=9 WHERE component='recovery'`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = db.InspectMySQLMigrations(ctx, s.DB, "recovery", steps); !errors.Is(e, db.ErrMySQLMigrationMismatch) {
|
|
t.Fatal("future version accepted", e)
|
|
}
|
|
}
|
|
|
|
func TestMySQLMigrationConcurrencyAndDrift(t *testing.T) {
|
|
s, _ := mlTestStore(t, storeMySQL)
|
|
ctx := context.Background()
|
|
op := mysqlProbe("migration_concurrent")
|
|
steps := []db.MySQLMigration{{Version: 1, Name: "concurrent", Operations: []db.MySQLOperation{op}, Assertions: []string{op.Check}}}
|
|
start := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
for i := 0; i < 2; i++ {
|
|
go func() { <-start; results <- db.ApplyMySQLMigrations(ctx, s.DB, "concurrent", steps) }()
|
|
}
|
|
close(start)
|
|
for i := 0; i < 2; i++ {
|
|
if e := <-results; e != nil {
|
|
t.Fatal("concurrent apply", e)
|
|
}
|
|
}
|
|
if _, e := s.DB.Exec(`ALTER TABLE config DROP PRIMARY KEY`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := NewMySQLMLRepository(s.DB).InspectSchema(ctx); e == nil {
|
|
t.Fatal("schema drift accepted")
|
|
}
|
|
// Error responses must not include full SQL/driver data.
|
|
bad := mysqlProbe("secret_probe")
|
|
bad.SQL = "INVALID SQL private-secret"
|
|
broken := []db.MySQLMigration{{Version: 1, Name: "bad", Operations: []db.MySQLOperation{bad}, Assertions: []string{bad.Check}}}
|
|
if e := db.ApplyMySQLMigrations(ctx, s.DB, "bad", broken); e == nil || strings.Contains(e.Error(), "private-secret") {
|
|
t.Fatal("unsafe error", e)
|
|
}
|
|
}
|
|
|
|
func TestMySQLMigrationRefusesUnsafeResumeAndAdoption(t *testing.T) {
|
|
s, _ := mlTestStore(t, storeMySQL)
|
|
ctx := context.Background()
|
|
op := mysqlProbe("migration_existing")
|
|
steps := []db.MySQLMigration{{Version: 1, Name: "existing", Operations: []db.MySQLOperation{op}, Assertions: []string{op.Check}}}
|
|
if _, e := s.DB.Exec(op.SQL); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := db.ApplyMySQLMigrations(ctx, s.DB, "adopt", steps); !errors.Is(e, db.ErrMySQLMigrationMismatch) {
|
|
t.Fatal("implicit adoption", e)
|
|
}
|
|
unsafe := mysqlProbe("migration_unsafe")
|
|
unsafe.SQL = "INVALID SQL"
|
|
unsafe.ReplaySafe = false
|
|
steps = []db.MySQLMigration{{Version: 1, Name: "unsafe", Operations: []db.MySQLOperation{unsafe}, Assertions: []string{unsafe.Check}}}
|
|
if e := db.ApplyMySQLMigrations(ctx, s.DB, "unsafe", steps); e == nil {
|
|
t.Fatal("expected failure")
|
|
}
|
|
if e := db.ResumeMySQLMigrations(ctx, s.DB, "unsafe", steps); !errors.Is(e, db.ErrMySQLMigrationDirty) {
|
|
t.Fatal("unsafe replay", e)
|
|
}
|
|
cancelCtx, cancel := context.WithCancel(ctx)
|
|
cancel()
|
|
if e := db.ApplyMySQLMigrations(cancelCtx, s.DB, "unsafe", steps); e == nil {
|
|
t.Fatal("cancel ignored")
|
|
}
|
|
// A canceled caller must not retain the session lock in the connection pool.
|
|
if _, e := db.InspectMySQLMigrations(ctx, s.DB, "unsafe", steps); e != nil {
|
|
t.Fatal("lock not released", e)
|
|
}
|
|
}
|
|
|
|
func TestMySQLMigrationVersionUpgrade(t *testing.T) {
|
|
s, _ := mlTestStore(t, storeMySQL)
|
|
ctx := context.Background()
|
|
op := mysqlProbe("migration_versioned")
|
|
firstCheck := `SELECT COUNT(*)=1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='migration_versioned'`
|
|
steps := []db.MySQLMigration{{Version: 1, Name: "initial", Operations: []db.MySQLOperation{op}, Assertions: []string{firstCheck}}}
|
|
if e := db.ApplyMySQLMigrations(ctx, s.DB, "versioned", steps); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
added := db.MySQLOperation{SQL: `ALTER TABLE migration_versioned ADD COLUMN label VARCHAR(64) NOT NULL DEFAULT ''`, Check: `SELECT COUNT(*)=1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='migration_versioned' AND column_name='label'`, ReplaySafe: true}
|
|
steps = append(steps, db.MySQLMigration{Version: 2, Name: "label", Operations: []db.MySQLOperation{added}, Assertions: []string{op.Check, added.Check}})
|
|
if e := db.ApplyMySQLMigrations(ctx, s.DB, "versioned", steps); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
// Version 1's shape assertion is intentionally obsolete after the upgrade.
|
|
status, e := db.InspectMySQLMigrations(ctx, s.DB, "versioned", steps)
|
|
if e != nil || status.Applied != 2 || status.Pending != 0 || status.Dirty {
|
|
t.Fatal(status, e)
|
|
}
|
|
if e := db.ApplyMySQLMigrations(ctx, s.DB, "versioned", steps); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := db.InspectMySQLMigrations(ctx, s.DB, "versioned", steps[:1]); !errors.Is(e, db.ErrMySQLMigrationMismatch) {
|
|
t.Fatal("downgrade accepted", e)
|
|
}
|
|
if _, e := s.DB.Exec(`ALTER TABLE schema_mysql_migrations DROP PRIMARY KEY`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := db.InspectMySQLMigrations(ctx, s.DB, "versioned", steps); !errors.Is(e, db.ErrMySQLMigrationMismatch) {
|
|
t.Fatal("damaged journal accepted", e)
|
|
}
|
|
}
|