Files
Redflag/aggregator-server/internal/database/migrations/migration024_test.go
jpetree331 ec0d880036 fix(database): B-1 schema integrity and migration fixes
- Fix migration 024 self-insert and bad column reference (F-B1-1, F-B1-2)
  Uses existing enabled/auto_run columns instead of non-existent deprecated
- Abort server on migration failure instead of warning (F-B1-11)
  main.go now calls log.Fatalf, prints [INFO] only on success
- Fix migration 018 scanner_config filename suffix (F-B1-3)
  Renumbered to 027 with .up.sql suffix
- Remove GRANT to non-existent role in scanner_config (F-B1-4)
- Resolve duplicate migration numbers 009 and 012 (F-B1-13)
  Renamed to 009b and 012b for unique lexical sorting
- Add IF NOT EXISTS to all non-idempotent migrations (F-B1-15)
  Fixed: 011, 012, 017, 023, 023a
- Replace N+1 dashboard stats loop with GetAllUpdateStats (F-B1-6)
  Single aggregate query replaces per-agent loop
- Add composite index on agent_commands(status, sent_at) (F-B1-5)
  New migration 028 with partial index for timeout service
- Add background refresh token cleanup goroutine (F-B1-10)
  24-hour ticker calls CleanupExpiredTokens
- ETHOS log format in migration runner (no emojis)

All 55 tests pass (41 server + 14 agent). No regressions.
See docs/B1_Fix_Implementation.md and DEV-025 through DEV-028.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 07:03:35 -04:00

83 lines
3.0 KiB
Go

package migrations_test
// migration024_test.go — Tests for migration 024 fixes.
//
// F-B1-1 FIXED: Self-insert into schema_migrations removed.
// F-B1-2 FIXED: Non-existent `deprecated` column reference removed.
// Migration now uses existing enabled/auto_run columns.
import (
"os"
"strings"
"testing"
)
func TestMigration024HasSelfInsert(t *testing.T) {
// POST-FIX: migration 024 must NOT contain self-insert
content, err := os.ReadFile("024_disable_updates_subsystem.up.sql")
if err != nil {
t.Fatalf("failed to read migration 024: %v", err)
}
if strings.Contains(string(content), "INSERT INTO schema_migrations") {
t.Error("[ERROR] [server] [database] F-B1-1: migration 024 still contains self-insert")
}
t.Log("[INFO] [server] [database] F-B1-1 FIXED: no self-insert in migration 024")
}
func TestMigration024ShouldNotHaveSelfInsert(t *testing.T) {
content, err := os.ReadFile("024_disable_updates_subsystem.up.sql")
if err != nil {
t.Fatalf("failed to read migration 024: %v", err)
}
if strings.Contains(string(content), "INSERT INTO schema_migrations") {
t.Errorf("[ERROR] [server] [database] migration 024 contains self-insert into schema_migrations.\n" +
"F-B1-1: the migration runner handles schema_migrations tracking.")
}
}
func TestMigration024ReferencesDeprecatedColumn(t *testing.T) {
// POST-FIX: migration 024 must NOT reference `deprecated` column
content, err := os.ReadFile("024_disable_updates_subsystem.up.sql")
if err != nil {
t.Fatalf("failed to read migration 024: %v", err)
}
// Check for "deprecated" as a column SET, not in comments
lines := strings.Split(string(content), "\n")
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "--") {
continue
}
if strings.Contains(strings.ToLower(trimmed), "deprecated") {
t.Error("[ERROR] [server] [database] F-B1-2: migration 024 still references deprecated column")
return
}
}
t.Log("[INFO] [server] [database] F-B1-2 FIXED: no deprecated column reference in migration 024")
}
func TestMigration024ColumnExistsInSchema(t *testing.T) {
// POST-FIX: migration 024 only uses columns that exist on agent_subsystems
// (enabled, auto_run, updated_at — all defined in migration 015)
content024, err := os.ReadFile("024_disable_updates_subsystem.up.sql")
if err != nil {
t.Fatalf("failed to read migration 024: %v", err)
}
content015, err := os.ReadFile("015_agent_subsystems.up.sql")
if err != nil {
t.Fatalf("failed to read migration 015: %v", err)
}
// Verify the columns 024 uses are in 015's CREATE TABLE
src015 := string(content015)
src024 := string(content024)
// 024 sets enabled, auto_run, updated_at — all must be in 015
for _, col := range []string{"enabled", "auto_run", "updated_at"} {
if strings.Contains(src024, col) && !strings.Contains(src015, col) {
t.Errorf("[ERROR] [server] [database] migration 024 uses column %q not defined in 015", col)
}
}
t.Log("[INFO] [server] [database] F-B1-2 FIXED: all columns used by 024 exist in schema")
}