41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package migrations
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestMigrationPairsAndProductionTables(t *testing.T) {
|
|
upFiles, err := filepath.Glob("*.up.sql")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
downFiles, err := filepath.Glob("*.down.sql")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(upFiles) == 0 || len(upFiles) != len(downFiles) {
|
|
t.Fatalf("migration pairs mismatch: up=%d down=%d", len(upFiles), len(downFiles))
|
|
}
|
|
|
|
content, err := os.ReadFile(upFiles[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sql := string(content)
|
|
for _, table := range []string{"users", "providers", "provider_models", "prompt_templates", "generations", "generation_inputs", "generation_outputs"} {
|
|
pattern := regexp.MustCompile(`(?i)CREATE TABLE\s+` + regexp.QuoteMeta(table) + `\s*\(`)
|
|
if !pattern.MatchString(sql) {
|
|
t.Errorf("migration does not create %s", table)
|
|
}
|
|
}
|
|
for _, required := range []string{"rendered_prompt", "attempts", "error_code", "error_message", "lease_token", "uq_generations_user_idempotency", "idx_generations_queue"} {
|
|
if !strings.Contains(sql, required) {
|
|
t.Errorf("migration is missing %s", required)
|
|
}
|
|
}
|
|
}
|