Files
QuoteForge/internal/models/sql_migration_verify_test.go
T
Mikhail ChusavitinandClaude Opus 5.5 44a09d4420 fix(sync): stop MariaDB connection leak and cut per-cycle sync waste
- ConnectionManager reuses a stale-but-healthy pool instead of dialing a new
  one every 30s, and closes every pool it replaces (one client held ~30
  connections). Pool capped at 2 connections, idle released after 1 min.
- Background sync every 1 min with ±30% jitter, scheduled from cycle start;
  users-status online threshold = interval × 1.3. One heartbeat per cycle.
- Partnumber books: read headers only, fetch partnumbers_json per book when
  new/incomplete; empty book counts as complete; an incomplete book is
  re-pulled once per process (server book lists BOT_AGIBOT_G2_PERF missing
  from qt_partnumber_book_items).
- NeedSync in one UNION ALL query; SyncQtSettings skips unchanged rewrite.
- Migration 035: server-maintained server_updated_at on qt_configurations and
  qt_projects. Clients pull only changed rows after one full pull per process;
  servers without 035 and old clients keep full pulls.
- Migrator: fresh GORM statement per verify query and history insert; files
  with two verify checks failed to record.

ADL: bible-local/decisions/2026-10-26-mariadb-single-pool.md,
bible-local/decisions/2026-10-26-incremental-config-project-pull.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 20:46:59 +03:00

71 lines
2.7 KiB
Go

package models
import (
"path/filepath"
"strings"
"testing"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
func TestMigrationVerification(t *testing.T) {
for _, tt := range []struct{ name, content, wantError string }{
{"valid", "-- verify: bad value | SELECT 1 FROM example WHERE value <> 1\nCREATE TEMP TABLE example (value INT); INSERT INTO example VALUES (1);", ""},
{"failed", "-- verify: bad value | SELECT 1 FROM example WHERE value <> 1\nCREATE TEMP TABLE example (value INT); INSERT INTO example VALUES (2);", "bad value"},
{"query error", "-- verify: missing table | SELECT 1 FROM missing\nCREATE TEMP TABLE example (value INT);", "missing table"},
{"malformed", "-- verify: no query\nCREATE TABLE example (value INT);", "invalid migration verify header"},
{"two checks pass", "-- verify: first | SELECT 1 WHERE 0\n-- verify: second | SELECT 1 WHERE 0\nCREATE TEMP TABLE example (value INT);", ""},
{"second check fails", "-- verify: first | SELECT 1 WHERE 0\n-- verify: second | SELECT 1\nCREATE TEMP TABLE example (value INT);", "second"},
{"execution failure", "-- verify: none | SELECT 1 WHERE 0\nINVALID SQL;", "exec migration"},
} {
t.Run(tt.name, func(t *testing.T) {
db, err := gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "migration.db")), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
sqlDB, _ := db.DB()
defer sqlDB.Close()
if err := db.AutoMigrate(&SQLSchemaMigration{}); err != nil {
t.Fatal(err)
}
err = applySQLMigrationFile(db, "001_example.sql", tt.content)
if tt.wantError != "" && (err == nil || !strings.Contains(err.Error(), tt.wantError)) {
t.Fatalf("want %q, got %v", tt.wantError, err)
}
if tt.wantError == "" && err != nil {
t.Fatal(err)
}
var count int64
if err := db.Model(&SQLSchemaMigration{}).Count(&count).Error; err != nil {
t.Fatal(err)
}
if (count == 1) != (tt.wantError == "") {
t.Fatalf("incorrect history count=%d", count)
}
if tt.name == "malformed" && db.Migrator().HasTable("example") {
t.Fatal("malformed header mutated database")
}
})
}
}
func TestMigrationErrorAllowlist(t *testing.T) {
for _, tt := range []struct {
message string
want bool
}{
{"Duplicate column name 'example'", true},
{"Duplicate key name 'example'", true},
{"Table 'example' already exists", true},
{"Can't create table example (errno: 150 Foreign key constraint is incorrectly formed)", false},
{"Can't create table example (errno: 121 Duplicate key)", false},
{"Duplicate foreign key constraint name 'example'", false},
{"Access denied", false},
} {
if got := isIgnorableMigrationError(tt.message); got != tt.want {
t.Errorf("%s: got %v", tt.message, got)
}
}
}