Files
chorus/migrations/mysql_integration_test.go
T

129 lines
5.5 KiB
Go

package migrations
import (
"context"
"database/sql"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
)
// This test resets only the explicitly designated disposable migration database.
// It is opt-in so normal package tests cannot erase a developer's local data.
func TestMVP1MigrationsUpDownUpMySQL(t *testing.T) {
if os.Getenv("CHORUS_RUN_MIGRATION_TESTS") != "1" {
t.Skip("set CHORUS_RUN_MIGRATION_TESTS=1 for the disposable MySQL migration database")
}
migrationURL := strings.TrimSpace(os.Getenv("CHORUS_MIGRATE_URL"))
dsn := strings.TrimSpace(os.Getenv("CHORUS_DSN"))
if migrationURL == "" || dsn == "" {
t.Fatal("CHORUS_MIGRATE_URL and CHORUS_DSN are required for migration integration tests")
}
if _, err := exec.LookPath("migrate"); err != nil {
t.Fatalf("migrate executable is required: %v", err)
}
db, err := sql.Open("mysql", dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
t.Fatalf("connect disposable migration database: %v", err)
}
runMigrate := func(args ...string) {
t.Helper()
commandArgs := append([]string{"-path", ".", "-database", migrationURL}, args...)
command := exec.CommandContext(ctx, "migrate", commandArgs...)
output, err := command.CombinedOutput()
if err != nil && !strings.Contains(string(output), "no change") {
t.Fatalf("migrate %s: %v\n%s", strings.Join(args, " "), err, output)
}
}
runMigrate("down", "-all")
runMigrate("goto", "1")
seedMVP0MigrationFixture(t, ctx, db)
runMigrate("up")
assertTableExists(t, ctx, db, "provider_credentials", true)
assertTableExists(t, ctx, db, "route_pools", true)
assertTableExists(t, ctx, db, "sys_user", true)
var credentialCount int
if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM provider_credentials WHERE provider_id = 1 AND credential_version = 1 AND key_id = 'migration-test-key'`).Scan(&credentialCount); err != nil {
t.Fatal(err)
}
if credentialCount != 1 {
t.Fatalf("credential backfill count = %d, want 1", credentialCount)
}
var activeCredential sql.NullInt64
var oldEnvelope sql.NullString
if err := db.QueryRowContext(ctx, `SELECT active_credential_id, api_key_enc FROM providers WHERE id = 1`).Scan(&activeCredential, &oldEnvelope); err != nil {
t.Fatal(err)
}
if !activeCredential.Valid || oldEnvelope.Valid {
t.Fatalf("provider credential migration did not activate the new credential safely")
}
assertCount(t, ctx, db, `SELECT COUNT(*) FROM provider_model_capabilities WHERE provider_model_id = 1 AND capability = 'text'`, 1)
assertCount(t, ctx, db, `SELECT COUNT(*) FROM sys_role WHERE role_key = 'chorus_operator'`, 1)
assertCount(t, ctx, db, `SELECT COUNT(*) FROM sys_api WHERE handle = 'chorus.providers.credential.rotate'`, 1)
runMigrate("down", "3")
assertTableExists(t, ctx, db, "provider_credentials", false)
assertTableExists(t, ctx, db, "sys_user", false)
assertCount(t, ctx, db, `SELECT COUNT(*) FROM providers WHERE id = 1 AND api_key_enc IS NOT NULL`, 1)
assertCount(t, ctx, db, `SELECT COUNT(*) FROM generations WHERE id = 1`, 1)
runMigrate("up")
assertTableExists(t, ctx, db, "provider_credentials", true)
assertCount(t, ctx, db, `SELECT COUNT(*) FROM provider_credentials WHERE provider_id = 1 AND credential_version = 1`, 1)
assertCount(t, ctx, db, `SELECT COUNT(*) FROM sys_role WHERE role_key = 'chorus_operator'`, 1)
}
func seedMVP0MigrationFixture(t *testing.T, ctx context.Context, db *sql.DB) {
t.Helper()
statements := []string{
`INSERT INTO users (id, email, password_hash, display_name, status) VALUES (1, 'migration-user@chorus.invalid', 'synthetic', 'Migration User', 'active')`,
`INSERT INTO providers (id, slug, name, base_url, auth_type, api_key_enc, enabled) VALUES (1, 'migration-provider', 'Migration Provider', 'https://provider.invalid', 'bearer', JSON_OBJECT('version', 1, 'key_id', 'migration-test-key', 'nonce', 'AA==', 'ciphertext', 'AA=='), FALSE)`,
`INSERT INTO provider_models (id, provider_id, name, model_id, api_type, kind, extra_body, timeout_ms, weight, enabled) VALUES (1, 1, 'Migration Model', 'migration-model', 'chat', 'text', JSON_OBJECT(), 30000, 100, TRUE)`,
`INSERT INTO prompt_templates (id, template_key, kind, api_type, name, version, template_text, enabled) VALUES (1, 'migration-template', 'text', 'chat', 'Migration Template', 1, '{{.UserPrompt}}', TRUE)`,
`INSERT INTO generations (id, user_id, provider_model_id, kind, status, idempotency_key, user_prompt, rendered_prompt, attempts, attempt_count) VALUES (1, 1, 1, 'text', 'pending', 'migration-key', 'migration input', 'migration input', JSON_ARRAY(), 0)`,
}
for _, statement := range statements {
if _, err := db.ExecContext(ctx, statement); err != nil {
t.Fatalf("seed MVP-0 fixture: %v", err)
}
}
}
func assertTableExists(t *testing.T, ctx context.Context, db *sql.DB, table string, want bool) {
t.Helper()
var count int
if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?`, table).Scan(&count); err != nil {
t.Fatal(err)
}
if (count == 1) != want {
t.Fatalf("table %s exists = %t, want %t", table, count == 1, want)
}
}
func assertCount(t *testing.T, ctx context.Context, db *sql.DB, query string, want int) {
t.Helper()
var got int
if err := db.QueryRowContext(ctx, query).Scan(&got); err != nil {
t.Fatal(err)
}
if got != want {
t.Fatal(fmt.Sprintf("query count = %d, want %d", got, want))
}
}