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.
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
func mlTestStore(t *testing.T, backend storeSQLBackend) (*Store, *Domain) {
|
|
t.Helper()
|
|
if backend != storeMySQL {
|
|
s := testNamespaceStore(t, backend)
|
|
d, e := s.GetDefaultDomain()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return s, d
|
|
}
|
|
database := mysqlTestDatabase(t)
|
|
ctx := context.Background()
|
|
repo := NewMySQLMLRepository(database)
|
|
status, e := repo.InspectSchema(ctx)
|
|
if e != nil || status.Applied != 0 || status.Pending != len(mysqlMLMigrations()) {
|
|
t.Fatal("fresh inspection", status, e)
|
|
}
|
|
if e = repo.PrepareSchema(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = repo.PrepareSchema(ctx); e != nil {
|
|
t.Fatal("repeat migration", e)
|
|
}
|
|
status, e = repo.InspectSchema(ctx)
|
|
if e != nil || status.Applied != len(mysqlMLMigrations()) || status.Dirty {
|
|
t.Fatal("applied inspection", status, e)
|
|
}
|
|
return repo.store, &Domain{UUID: uuid.NewString()}
|
|
}
|
|
|
|
func mysqlTestDatabase(t *testing.T) *sql.DB {
|
|
t.Helper()
|
|
dsn := os.Getenv("PAGESWIKI_TEST_MYSQL_DSN")
|
|
if dsn == "" {
|
|
t.Skip("set PAGESWIKI_TEST_MYSQL_DSN to a disposable MySQL server")
|
|
}
|
|
c, e := mysql.ParseDSN(dsn)
|
|
if e != nil {
|
|
t.Fatal("invalid test DSN")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
admin, e := db.OpenMySQL(ctx, db.MySQLOptions{DSN: dsn, AllowInsecure: true})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
name := "pages_ml_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
|
if _, e = admin.ExecContext(ctx, "CREATE DATABASE "+name+" CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"); e != nil {
|
|
admin.Close()
|
|
t.Fatal("create isolated MySQL database failed")
|
|
}
|
|
t.Cleanup(func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
if _, e := admin.ExecContext(ctx, "DROP DATABASE "+name); e != nil {
|
|
t.Error("remove test database failed")
|
|
}
|
|
admin.Close()
|
|
})
|
|
c.DBName = name
|
|
database, e := db.OpenMySQL(ctx, db.MySQLOptions{DSN: c.FormatDSN(), AllowInsecure: true})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
t.Cleanup(func() { database.Close() })
|
|
return database
|
|
}
|